Skip to content

Object mapping standard

This page sets the rules for mapping between contracts, domain inputs and outputs, and entities in ConnectSoft services and libraries. It is written for engineers who add or review mapping profiles and the code that calls them. The mapper choice is settled in ADR-0106 section 2; how mapping is configured is explained in Object mapping.

Providers

Package Position
ConnectSoft.Extensions.ObjectMapping with .Abstractions Required abstraction (IObjectMapper)
ConnectSoft.Extensions.ObjectMapping.AutoMapper (AutoMapper 17) Default, where the AutoMapper licence is wired
ConnectSoft.Extensions.ObjectMapping.Mapster Supported licence-free option

With the Mapster provider, register profile assemblies on the mapping options that are already registered. A second AddMappingProfiles call after the template's own call is ignored, because the options are registered once as a single instance, so the second assembly's profiles never load.

Rules

CS-MAP-001: Depend on the object mapping abstraction

Severity: major · Applies to: service, library, template · Enforced by: manual · Status: active

Why

Code that injects a mapper package's own interface cannot move between AutoMapper and Mapster, and it ties every class to a package whose licence can change. ADR-0106 section 2 requires the abstraction.

Do

  • Inject ConnectSoft.Extensions.ObjectMapping.Abstractions.IObjectMapper in controllers, service implementations, use cases and processors.
  • Keep provider-specific code (AutoMapper Profile classes, Mapster IRegister classes and the provider registration) in mapping and application-model projects.

Don't

  • Inject AutoMapper.IMapper or MapsterMapper.IMapper into application code.
  • Add using AutoMapper; or using Mapster; to a file that is not a profile or a registration.

Example

public sealed class OrdersController : ControllerBase
{
    private readonly IObjectMapper mapper;

    public OrdersController(IObjectMapper mapper)
    {
        this.mapper = mapper;
    }
}

How to check

Search the change for IMapper and for using AutoMapper; outside profile and registration files. The consumer services that still inject IMapper directly have a planned migration under ENF-028.

CS-MAP-002: Never cross AutoMapper 15 without the licence wiring

Severity: blocker · Applies to: service, library, template · Enforced by: manual · Status: active

Why

AutoMapper 15 and later are commercially licensed. A repository that upgrades past version 15 without its licence wiring runs unlicensed software. ADR-0103 section 8 holds AutoMapper at 17.0.0 only where the licence is wired.

Do

  • Keep a repository without the licence wiring on its current AutoMapper major, or move it to the Mapster provider.
  • Upgrade to AutoMapper 15 or later only in the same change that wires the licence.

Don't

  • Accept an automated upgrade that moves AutoMapper from 14 or lower to 15 or higher.

How to check

When Directory.Packages.props changes the AutoMapper version across 15, check that the change also configures the licence.

CS-MAP-003: Test every mapping configuration for validity

Severity: major · Applies to: service, library, template · Enforced by: test · Status: active

Why

A destination member that no source member fills is mapped to its default value without any error. A configuration test finds it when the profile is written, not when a field is empty in production.

Do

  • Add a unit test per profile that loads the profile through the real registration and asserts that the configuration is valid, for example AssertConfigurationIsValid() for AutoMapper.
  • Add tests that map a fully populated source and assert the fields that are renamed, computed or ignored.

Don't

  • Turn off unmapped-member validation globally to make the test pass.

Example

[TestMethod]
public void OrderMappingProfileConfigurationIsValid()
{
    // Arrange
    var configuration = new MapperConfiguration(config => config.AddProfile<OrderMappingProfile>());

    // Act and Assert
    configuration.AssertConfigurationIsValid();
}

How to check

  • Each new or changed profile has a configuration test in the unit test project.
  • Each new or changed profile also has a test that maps a fully populated source and asserts the fields that are renamed, computed or ignored.

CS-MAP-004: Keep business logic and I/O out of mappings

Severity: major · Applies to: service, library, template · Enforced by: manual · Status: active

Why

Mappings run wherever a mapper is called and are not logged, validated or wrapped in a transaction. A rule or a lookup hidden in a profile runs at the wrong time and is invisible to readers of the domain service.

Do

  • Map values, rename members and convert types in profiles.
  • Compute business values and look up data in the domain service, then map the result.

Don't

  • Call repositories, clients or services from MapFrom, value resolvers, AfterMap or BeforeMap.
  • Make decisions such as status transitions in a profile.

How to check

Read new resolvers and MapFrom expressions. They use only the source object.

CS-MAP-005: Ignore a destination member explicitly, and only that member

Severity: major · Applies to: service, library, template · Enforced by: manual · Status: active

Why

Some destination members must never come from the source, for example a field that the controller sets from the caller's token. An explicit Ignore documents that, and the configuration test still checks every other member. A global "tolerate unmapped members" setting hides real gaps.

Do

  • Add .ForMember(destination => destination.Member, options => options.Ignore()) for each member that is set elsewhere.
  • Add a comment that says where the member is set.

Don't

  • Turn off unmapped-member validation for a profile or globally.
  • Ignore a member to make a configuration test pass without knowing where its value comes from.

Example

this.CreateMap<CancelOrderRequest, CancelOrderInput>()
    // Set by the controller from the caller's token, never from the request body.
    .ForMember(destination => destination.CancelledByUserId, options => options.Ignore());

How to check

Every Ignore() in the change names a member that is set in another place, and the place is stated.

CS-MAP-006: Keep "null means keep" collections intact through every mapping hop

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Why

By default AutoMapper maps a null source collection to an empty collection. When null in a request means "leave this collection unchanged", the empty collection means "remove everything". A request that passes through a gateway and a service is mapped at least twice. Fixing only one hop leaves the end-to-end path broken while each unit test passes.

Do

  • For each collection where null has a meaning, add .ForMember(destination => destination.Items, options => options.AllowNull()) on every map along the path, from the gateway to the service.
  • Add a guard test per profile that checks every such collection keeps null.
  • Make sure the test fixture fills the collection, so that "kept" is not proven by comparing two empty lists.

Don't

  • Fix the mapping in one service only.
  • Map a full-replace update through a display model that does not carry every field. A field the model drops is cleared on save.

How to check

  • For a new nullable collection on a request, find every profile that maps it and check for AllowNull().
  • Each of those profiles has a guard test that asserts the collection keeps null, and the test fixture fills the collection.
  • Check for an end-to-end test that sends null and verifies the stored collection is unchanged.