简体   繁体   中英

Automapper inject dependencies into custom type converter using Simple injector (Ioc)

i am using automapper to map from dtos to domain and vice versa; i am using custom type converter to do the conversion but i want to inject dependencies into my converter class using simple injector ioc; i can't do that. please tell me how to achieve that?

public class DtoToEntityConverter : ITypeConverter<Dto, Entity>
{
    private readonly IEntityRepository _entityRepository;

    public DtoToEntityConverter (IEntityRepository entityRepository )
    {
        _entityRepository = entityRepository ;
    }

    public Entity Convert(ResolutionContext context)
    {

    }     


}

You'll need to configure services via AutoMapper:

var container = ConfigureSimpleInjectorContainer();

Mapper.Initialize(cfg => {
    cfg.ConstructServicesUsing(type => container.GetInstance(type));
    // The rest of your initialization
});

I used this approach every time my container cannot reach some of my code like the question you posted.

public class DtoToEntityConverter : ITypeConverter<Dto, Entity>
{
    private readonly IEntityRepository _entityRepository;

    public DtoToEntityConverter (IEntityRepository entityRepository )
    {
        _entityRepository = entityRepository ;
    }

    public Entity Convert(ResolutionContext context)
    {

    }     
}

How would you register it:

Mapper.CreateMap<From, To>().ConvertUsing(new DtoToEntityConverter(Ioc.Resolve<IEntityRepository>()));

The Ioc static class should hold the IUnitContainer where you register all your dependency.

public static class IoC
{
    public static IUnityContainer Unity { get; private set; }

    public static T Resolve<T>()
    {
        return Unity.Resolve<T>();
    }

    public static T Resolve<T>(string key)
    {
        return Unity.Resolve<T>(key);
    }

    public static void Initialize(IUnityContainer unity)
    {
        Unity = unity;
    }
}

Note : Make it sure that you already created the UnityContainer prior of mapping. Your problem is not typical and you may consider to redesigning it and leave the mapping (Automapper) for mapping of model to model only . But may be in some point you have weird requirement you can consider this approach.

I'm using Automapper 6.1.1 and Microsoft.Extensions.DependencyInjection and this worked for me... Create the type converter, eg

    public class ChargesConverter :
            ITypeConverter<ChargesQueryResult, IEnumerable<Charge>>
        {
            private readonly IToggleFeatures features;
            private readonly ITierThresholds tierThresholds;
    
            public ProductWrapperChargesConverter(IToggleFeatures features, ITierThresholds tierThresholds)
            {
                this.features = features;
                this.tierThresholds = tierThresholds;
            }
   // other functionality
}

In the mapping profile

this.CreateMap<ProductWrapperChargesQueryResultAdapter, IEnumerable<Charge>>().ConvertUsing<ProductWrapperChargesConverter>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM