Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure AutoMapper using LightInject

Does anyone know how to configure AutoMapper using LightInject? The AutoMapper documentation only has examples for Ninject and Simple Injector.

I am having difficulty trying to register the AutoMapper configuration.

I'm using ASP.NET MVC C#.

public class CompositionRoot : ICompositionRoot
{
    public void Compose(IServiceRegistry serviceRegistry)
    {
      serviceRegistry.Register(c => new AutoMapperConfiguration());
    }
}

public static class AutoMapperConfiguration
{
    public AutoMapperConfiguration()
    {
        Mapper.Initialize(cfg =>
           cfg.AddProfiles(typeof(Namespace.Class).Assembly)
        );
    }
}
like image 639
Kevin C. Avatar asked Mar 10 '26 16:03

Kevin C.


1 Answers

I figured it out. The code below is in the CompositionRoot, where the factory is registered using IServiceRegistry. I will be moving the var config = new MapperConfiguration(cfg => cfg.AddProfiles(typeof(CustomProfileClass).Assembly)); code to a custom MapperConfiguration class that I will create.

public class CompositionRoot : ICompositionRoot
{
    public void Compose(IServiceRegistry serviceRegistry)
    {
      var config = new MapperConfiguration(cfg => cfg.AddProfiles(typeof(CustomProfileClass)));
      serviceRegistry.Register(c => config.CreateMapper());
    }
}
like image 65
Kevin C. Avatar answered Mar 13 '26 08:03

Kevin C.