4

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

AutoMapper 配置文件:

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

温莎城堡注册:

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

    }

}

感谢您的任何建议。

4

2 回答 2

4

因此,上面的代码正在运行。问题是,我是个白痴。因为我不应该调用 Mapper.Map,而是 _mapper.Map。

于 2016-09-25T09:58:14.617 回答
2

For those who are using Automapper 9.0 with Castle Windsor (my version is 3.2.0) using container. I created a file where I register my model with Dtos. 1. AutoMapperProfile.cs

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

2. I have AutoMapperInstall.cs as Installer

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. I register all of my installer on 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)
    

Hope this helps someone using new version of AutoMapper with Castle Windsor. Feel free to comment with criticisms or suggestions.

于 2019-11-21T13:50:00.930 回答