Question

I would like to use Automapper to map my model objects to database objects. Let say database object is over 30 fields and I want map 10 out of 20 properties from my model. To make it more complex I need to map different properties when I update record than when I insert new record to database.

The solution I'm using is to create 2 generic classes Insert and Update and mapping profile that specifies both mappings.

The example below:

public abstract class SyncMappingWrapper<TFrom> where TFrom : class
{
    protected SyncMappingWrapper(TFrom model)
    {
        Model = model;
    }

    public TFrom Model { get; private set; }
}

public class Update<TFrom> : SyncMappingWrapper<TFrom> where TFrom : class
{
    public Update(TFrom model)
        : base(model)
    {
    }
}

public class Insert<TFrom> : SyncMappingWrapper<TFrom> where TFrom : class
{
    public Insert(TFrom model)
        : base(model)
    {
    }
}

The mapping, however, is getting nasty as cyclomatic complexity goes in to the sky (over 50) as I need to define Ignore() for all properties I don't map:

CreateMap<Update<OracleModel>, LiveModel>()
            .ForMember(des => des.ApprovedBy, opt => opt.Ignore())
            .ForMember(des => des.ApprovedDate, opt => opt.Ignore())
            ...
            .ForMember(des => des.UNSPSC, opt => opt.Ignore())
            .ForMember(des => des.BaseUnit, opt => opt.MapFrom(src => src.Model.UOM.BaseUOM.PerSalesUnit))
            .ForMember(des => des.BaseUOM, opt => opt.MapFrom(src => src.Model.UOM.BaseUOM.UnitOfMeasure.Code))
            .ForMember(des => des.SalesUnit, opt => opt.MapFrom(src => src.Model.UOM.SalesUOM.PerSalesUnit))
            .ForMember(des => des.SalesUOM, opt => opt.MapFrom(src => src.Model.UOM.SalesUOM.UnitOfMeasure.Code))
            .ForMember(des => des.OrderUnit, opt => opt.MapFrom(src => src.Model.UOM.OrderUOM.PerSalesUnit))
            .ForMember(des => des.OrderUOM, opt => opt.MapFrom(src => src.Model.UOM.OrderUOM.UnitOfMeasure.Code))
            .ForMember(des => des.SalesPrice, opt => opt.MapFrom(src => src.Model.Price.Value))
            .ForMember(des => des.Alternate, opt => opt.Ignore())
            .ForMember(des => des.ManufacturerID, opt => opt.Ignore())
            .ForMember(des => des.ProductCode, opt => opt.MapFrom(src => src.Model.ProductCode))
            .ForMember(des => des.ProductName, opt => opt.MapFrom(src => src.Model.ProductName))
            .ForMember(des => des.ProductHTML, opt => opt.Ignore())
            .ForMember(des => des.Version, opt => opt.Ignore())
            ...
            .ForMember(des => des.UnitsOfMeasure2, opt => opt.Ignore())
            .ForMember(des => des.Manufacturer, opt => opt.Ignore());

I've solved the problem for inserting new record by creating new object:

CreateMap<Insert<OracleModel>, LiveModel>()
            .ConstructUsing(x => new LiveModel
                {
                    BaseUnit = x.Model.UOM.BaseUOM.PerSalesUnit,
                    BaseUOM = x.Model.UOM.BaseUOM.UnitOfMeasure.Code,
                    SalesUnit = x.Model.UOM.SalesUOM.PerSalesUnit,
                    SalesUOM = x.Model.UOM.SalesUOM.UnitOfMeasure.Code,
                    OrderUnit = x.Model.UOM.OrderUOM.PerSalesUnit,
                    OrderUOM = x.Model.UOM.OrderUOM.UnitOfMeasure.Code,
                    SalesPrice = x.Model.Price.Value,
                    LeadTime = x.Model.LeadTime,
                    ProductCode = x.Model.ProductCode,
                    ProductName = x.Model.ProductName,
                    SupplierCode = x.Model.SupplierCode,
                    Weight = x.Model.Weight
                })
            .ForAllMembers(xc => xc.Ignore());

But it doesn't work for updates where I want to map properties to an existing object and not new instance:

        Mapper.Map(update, existingRecord);

I would prefer to avoid DynamicMap() to keep full control over the mapping (so I will not map a random property by mistake). My target is to sort out cyclomatic complexity problem. Please do not suggest ValueInjecter or any other methods. I'm looking for solution within AutoMapper.

Was it helpful?

Solution

It's five years later, but here's a suggestion to reduce this mapping complexity problem. You can create an extension that will help you ignore all members at first:

public static class AutoMapperExtension {
    public static IMappingExpression<TSource, TDest> IgnoreAllMembers<TSource, TDest>(this IMappingExpression<TSource, TDest> expression) {
        expression.ForAllMembers(opt => opt.Ignore());
        return expression;
    }
}

Then you use it to define only the mappings you want to perform:

Mapper.CreateMap<Insert<OracleModel>, LiveModel>()
      .IgnoreAllMembers()
      .ForMember(d => d.BaseUnit, o => o.MapFrom(s => s.Model.UOM.BaseUOM.PerSalesUnit))
      /* Mapping for other members here. */;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top