Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting AutoMapper instance

I have an ASP.NET MVC application with all mappings registered during the bootstrap in this way:

Mapper.CreateMap<AdvicesModel, Advices>();

So far we've used it the "old" way statically:

Mapper.Map<Advice>(adviceDto)

So far so good.

After the update to version 4 of AutoMapper I've seen that it is recommended to use it constructing an Instance. Can someone point me how to properly instruct Castle to Inject an instance of AutoMapper to my dependencies and user it not statically.

What I imagine is something like this:

var viewModel = mapper.Map<CartViewModel>(cart);

with IMapper instance injected.

like image 253
Aleksander Bethke Avatar asked Oct 28 '25 01:10

Aleksander Bethke


2 Answers

I don't think services.AddSingleton<> is Castle Windsor so below is my CastleWindsor installer.

        private static void RegisterProfilesAndResolvers(IWindsorContainer container)
    {
        // register value resolvers
        container.Register(Types.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IValueResolver>());

        // register profiles
        container.Register(Types.FromThisAssembly().BasedOn<Profile>().WithServiceBase().Configure(c => c.Named(c.Implementation.FullName)).LifestyleTransient());

        var profiles = container.ResolveAll<Profile>();

        var config = new MapperConfiguration(cfg =>
        {
            foreach (var profile in profiles)
            {
                cfg.AddProfile(profile);
            }
        });

        container.Register(Component.For<MapperConfiguration>()
            .UsingFactoryMethod(() => config));

        container.Register(
            Component.For<IMapper>().UsingFactoryMethod(ctx => ctx.Resolve<MapperConfiguration>().CreateMapper(ctx.Resolve)));
    }

I'm not sure if this is ideal/optimal etc but it does seem to work :-)

like image 147
Ian Barrett Avatar answered Oct 30 '25 15:10

Ian Barrett


For the native ASP.net 5 DI:

  1. create a new class extending AutoMapper.Profile, with a single overrided Configure, create all the maps in the Configure.
public class YourProfile : Profile
{
   protected override void Configure(){
       //CreateMap<T, TDto>().ForMember....   
   }
}
  1. in startup.cs, setup the DI like this:
 public void ConfigureServices(IServiceCollection services)
 {
  //...
   var config = new MapperConfiguration(cfg =>
   {
     cfg.AddProfile(new YourProfile());
   });
   services.AddSingleton<IMapper>(sp => config.CreateMapper())
  //...
 }
  1. Now injection:
public class Repository{
    private readonly IMapper _mapper;
    public Repository(IMapper mapper){
        _mapper = mapper;
    }
    public List<TDto> ToDtoList<TDto>(){
     return _mapper.Map<List<TDto>>(sourceList);
    }
 }

Source: https://pintoservice.wordpress.com/2016/01/31/dependency-injection-for-automapper-4-2-in-asp-net-vnext-mvc-project/

like image 45
Rex Avatar answered Oct 30 '25 16:10

Rex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!