automapperlight-inject

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)
        );
    }
}

Solution

  • 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());
        }
    }