15

I am planning to use Automapper with ASP.NET MVC solution and Unity DI. The video posted on automapper on how to use is very old and doesn't show how mapper can be used with dependency injection. Most of the examples on stackoverflow also uses Mapper.CreateMap() method which is now deprecated.

The automapper guide says

Once you have your types you can create a map for the two types using a MapperConfiguration instance and CreateMap. You only need one MapperConfiguration instance typically per AppDomain and should be instantiated during startup.

 var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());

So i am assuming above line of code will go into application startup, like global.asax

To perform a mapping, create an IMapper use the CreateMapper method.

 var mapper = config.CreateMapper();
 OrderDto dto = mapper.Map<OrderDto>(order);

The above line will go into controller. However i am not understanding where this config variable coming from? How do i inject IMapper in controller?

LP13
  • 30,567
  • 53
  • 217
  • 400
  • You should configure the container to map between `IMapper` and the mapper instance. Then you should declare a dependency on `IMapper` from the controller (by accepting an `IMapper` in the constructor). – Yacoub Massad May 04 '16 at 22:08
  • 1
    any example? So will there be `new MapperConfiguration()' for each entity i need to map? Example would be really help – LP13 May 04 '16 at 22:14

1 Answers1

34

First, create a MapperConfiguration and from it an IMapper that has all your types configured like this:

var config = new MapperConfiguration(cfg =>
{
    //Create all maps here
    cfg.CreateMap<Order, OrderDto>();

    cfg.CreateMap<MyHappyEntity, MyHappyEntityDto>();

    //...
});

IMapper mapper = config.CreateMapper();

Then, register the mapper instance with the unity container like this:

container.RegisterInstance(mapper);

Then, any controller (or service) that wishes to use the mapper can declare such dependency at the constructor like this:

public class MyHappyController
{
    private readonly IMapper mapper;

    public MyHappyController(IMapper mapper)
    {
        this.mapper = mapper;
    }

    //Use the mapper field in your methods
}

Assuming that you set up the container correctly with the MVC framework, the controller should be constructable without an issue.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • 1
    thank you. Your intructions were very helpful. Just adding that for me due to existing configuration of how Unity was implemented I had to register as such Container.Register(Lifetime.Singleton); – Geovani Martinez May 30 '18 at 18:49
  • Thank you for this. This works perfectly. However, I'm trying to user AutoMapper as Property Injected property and as such, this doesn't work. Do you know how I can make it work? – Cubelaster Sep 05 '19 at 13:25
  • 1
    @Cubelaster, are you trying to inject IMapper as a property? If your DI container supports property injection and you use it correctly, it should work. – Yacoub Massad Sep 05 '19 at 15:04
  • @YacoubMassad, Yeah, I am. How do I know if my DI container supports it? Is there an Option of sorts? I can't find it in documentation. – Cubelaster Sep 05 '19 at 15:17
  • 1
    @Cubelaster, what DI container are you using? By the way, why do you want to use property injection and not constructor injection? – Yacoub Massad Sep 05 '19 at 15:23
  • @YacoubMassad, I'm using Unity DI, with .NET. I have constructor injection working, that's not a problem. However, property injection doesn't work. And I can't figure out why. I basically want to use some stuff for constructor injection and other stuff for property injection (I'd like for Base class to get stuff from property injection so I don't have to put them in constructor as well). I tried some stuff, but unsuccessful. Is there a trick? – Cubelaster Sep 06 '19 at 05:27
  • 1
    @Cubelaster, it has been a long time since I used a DI container. Did you see this? https://stackoverflow.com/questions/10267536/how-to-inject-dependency-property-using-ioc-unity – Yacoub Massad Sep 06 '19 at 09:02
  • @YacoubMassad Ah, man, thank you! I think I figured it out. The project I'm working on has Unity in only the top project. I am not able to use Dependency Attiribute but was hoping I could use Runtime. I'm guessing, however, that is not possible with this setup. Funny, because Windsor has no such problems. Thank you a lot! – Cubelaster Sep 06 '19 at 10:02
  • This answer is end of my search – Saurabh Solanki Jan 07 '20 at 12:04