I'm working on a large ASP.NET MVC 5 project nowadays and I'm implementing DI by using Ninject framework for MVC. Actually it's the first time to me to use Ninject and I'm in dire need to know what is the best practice of using AutoMApper 5.2.0 With it.
After Googling I found some examples which demonstrate an old version of AutoMapper that have some deprecated methods in the new version.
My solution is consist of the following projects:
I'm working on the same project in this link.
there are three things you need to set up for AutoMapper in Ninject.
AutoMapper.IMapper
here is the NinjectModule
I use for this purpose:
public class AutoMapperModule : NinjectModule
{
public override void Load()
{
Bind<IMapper>().ToMethod(AutoMapper).InSingletonScope();
}
private IMapper AutoMapper(Ninject.Activation.IContext context)
{
Mapper.Initialize(config =>
{
config.ConstructServicesUsing(type => context.Kernel.Get(type));
config.CreateMap<MySource, MyDest>();
// .... other mappings, Profiles, etc.
});
Mapper.AssertConfigurationIsValid(); // optional
return Mapper.Instance;
}
}
then you will just inject AutoMapper.IMapper
into your classes instead of using the static Mapper
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With