Reputation: 1021
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)
{
}
}
Upvotes: 4
Views: 7173
Reputation: 378
In .NET 6, you can do something like this:
builder.Services.AddAutoMapper((provider, opt) =>
{
opt.ConstructServicesUsing(t => ActivatorUtilities.CreateInstance(provider, t));
}, Assembly.GetAssembly(typeof(Program)));
Upvotes: 0
Reputation: 2498
I'm using Automapper 6.1.1 and Microsoft.Extensions.DependencyInjection and this worked for me... Create the type converter, e.g.
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>();
Upvotes: -1
Reputation: 2535
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.
Upvotes: 2
Reputation: 26765
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
});
Upvotes: 10