简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM