0

I have a TypeConverter in which I'm using context.Mapper.Map() to map two subproperties.

The properties are the same Type and use the same (another) TypeConverter. However in one the properties I need to pass some IMappingOperationsOptions.

It looks like this (simplified):

public class MyTypeConverter : ITypeConverter<A, B>
{
    public B Convert(A, B, ResolutionContext context)
    {
        var subProp1 = context.Mapper.Map<C>(B.SomeProp);
        var subProp2 = context.Mapper.Map<C>(B.SomeOtherProp, ops => ops.Items["someOption"] = "someValue");
    
        return new B
        {
            SubProp1 = subProp1,
            SubProp2 = subProp2 
        };
    }
}

This was working fine in AutoMapper 8.0.0 but I'm upgrading to AutoMapper 10.1.1 (last version with .NET framework support). In this newer version of AutoMapper the overload method to pass IMappingOperationsOptions does not exist anymore.

I could (theoretically) solve this by injecting IMapper in the constructor of the TypeResolver and use that instead of the ResolutionContext's Mapper but that doesn't feel right.

At the moment I solved the issue by temporarily updating the ResolutionContext options, but that also doesn't really feel right.

var subProp1 = context.Mapper.Map<C>(B.SomeProp);

context.Options.Items["someOption"] = "someValue";
var subProp2 = context.Mapper.Map<C>(B.SomeOtherProp);
context.Options.Remove("someOption");

Casting ((IMapper)context.Mapper).Map() crashes so that's not an option either. Is there a more elegant way to achieve this?

Rob
  • 4,927
  • 12
  • 49
  • 54
  • You shoud use `context.Items`, but other than that, that's what you have to do. I think it would be cleaner to use some other way to pass state, for example in the source or destination objects. – Lucian Bargaoanu Dec 07 '22 at 14:46

0 Answers0