有人知道如何使用LightInject配置AutoMapper吗?AutoMapper文档中只有Ninject和Simple Injector的示例。
我在尝试注册AutoMapper配置时遇到困难。
我使用的是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)
);
}
}
我想明白了。下面的代码在CompositionRoot中,使用IServiceRegistry注册工厂。我将把var config = new MapperConfiguration(cfg => cfg.AddProfiles(typeof(CustomProfileClass).Assembly));
代码移到我将创建的自定义MapperConfiguration类中。
public class CompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
var config = new MapperConfiguration(cfg => cfg.AddProfiles(typeof(CustomProfileClass)));
serviceRegistry.Register(c => config.CreateMapper());
}
}