繁体   English   中英

向温莎城堡注册 AutoMapper 5.1.1

[英]Register AutoMapper 5.1.1 with Castle Windsor

我正在尝试向 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 not initialized 异常:

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

    }

}

感谢您的任何建议。

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

对于那些使用容器使用 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. 现在我可以走了。 只需通过构造函数创建一个映射器的实例并使用它。

     readonly IMapper mapper; public UserMenuService(IMapper mapper) { this.mapper = mapper; }
  2. 当您想返回 Dto 时,只需使用

    return mapper.Map<IEnumerable<UserMenuReadModel>, List<UserMenuDto>>(result);
  3. 最后但并非最不重要的是,在 global.asax 上注册 IOC 引导配置

     container = new WindsorContainer(); BootstrapConfig.Register(this.container)

希望这对使用新版 AutoMapper 和 Castle Windsor 的人有所帮助。 随意评论批评或建议。

我仍然遇到“没有为此对象定义无参数构造函数”的问题

在 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at AutoMapper.MappingOperationOptions`2.CreateInstanceT at AutoMapper.Mapper.MapCore [TSource,TDestination](TSource source, TDestination destination, ResolutionContext context, Type sourceType, Type destinationType, IMemberMap memberMap) at AutoMapper.Mapper.Map[TSource,TDestination](TSource source, TDestination destination) at AutoMapper.Mapper.Map[TSource ,TDestination](TSource 源)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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