使用Castle Windsor注册AutoMapper 5.1.1

5

我想注册AutoMapper 5.1.1到CastleWindsor,但我不知道在哪里正确地调用Mapper.Initialize()。

AutoMapper配置文件:

namespace AutoMapper_DI.Mappings
{
    public class WebMappingProfile : Profile
    {        
        public WebMappingProfile()
        {
          CreateMap<Person, PersonDTO>();            
        }
    }
}

Castle Windsor 注册:

class MainInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {            
        container.AddFacility<TypedFactoryFacility>();

        container.Register(Component.For<IMapper>().UsingFactoryMethod(x =>
        {
            return new MapperConfiguration(c =>
            {
                c.AddProfile<WebMappingProfile>();
            }).CreateMapper();
        }));

        container.Register(Component.For<MainClass>());
    }
}

当使用_mapper时,我遇到了Mapper未初始化的异常:

class MainClass
{
    private readonly IMapper _mapper;

    public MainClass(IMapper mapper)
    {
        _mapper = mapper;
    }

    public void Start()
    {            
        Person p = new Person
        {
            Name = "Somebody",
            Surname = "Nobody",
            Birth = new DateTime(1984, 06, 18)
        };            
        var personDTO = Mapper.Map<Person, PersonDTO>(p);

    }

}

感谢任何建议。

你是否考虑将映射器配置单独创建并注册为“实例”,使用 Component.For<IMapper>().Instance(mapper); - stuartd
2个回答

5

所以,上面的代码是有效的。问题在于我很蠢。因为我不应该调用 Mapper.Map,而应该调用 _mapper.Map。


3

对于那些正在使用容器的Automapper 9.0和Castle Windsor(我的版本是3.2.0)的人,我创建了一个文件,在其中使用Dtos注册我的模型。

1. AutoMapperProfile.cs

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        //Register all model with Dtos here
        CreateMap<UserMenuReadModel, UserMenuDto>();
    }        
}

2. 我有AutoMapperInstall.cs作为安装程序

public class AutoMapperInstall : Castle.MicroKernel.Registration.IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Castle.MicroKernel.Registration.Component.For<IMapper>().UsingFactoryMethod(factory =>
        {
            return new MapperConfiguration(map =>
            {
                map.AddProfile<AutoMapperProfile>();

            }).CreateMapper();
        }));
    }
}

3. 我在BootstrapConfig.cs中注册了所有的安装程序。

public class BootstrapConfig
{
    public static void Register(IWindsorContainer container)
    {
        GlobalConfiguration.Configuration.Services.Replace(
            typeof(IHttpControllerActivator),
            new WindsorCompositionRoot(container));

        container.Install(               
            new DomainModelLayerInstall(),               
            new AutoMapperInstall()
        );
    }
}
  1. Now I'm good to go. Just make an instance of mapper through constructor and use it.

     readonly IMapper mapper;
     public UserMenuService(IMapper mapper)
     {          
         this.mapper = mapper;
      }
    
  2. When you want to return the Dto, simply use

    return mapper.Map<IEnumerable<UserMenuReadModel>, List<UserMenuDto>>(result);
    
  3. Last but not least, register IOC bootstrap config on global.asax

        container = new WindsorContainer();
        BootstrapConfig.Register(this.container)
    
希望这可以帮助使用新版AutoMapper和Castle Windsor的人。欢迎评论,提出批评或建议。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接