[英]Register AutoMapper 5.1.1 with Castle Windsor
I'm trying to register AutoMapper 5.1.1 with CastleWindsor, but I don't know, where to properly call Mapper.Initialize().我正在尝试向 CastleWindsor 注册 AutoMapper 5.1.1,但我不知道在哪里正确调用 Mapper.Initialize()。
AutoMapper profile: AutoMapper 配置文件:
namespace AutoMapper_DI.Mappings
{
public class WebMappingProfile : Profile
{
public WebMappingProfile()
{
CreateMap<Person, PersonDTO>();
}
}
}
Castle Windsor registration:温莎城堡注册:
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>());
}
}
And then when I use _mapper I got Mapper not initialized exception:然后当我使用 _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);
}
}
Thanks for any advices.感谢您的任何建议。
So, code above is working.所以,上面的代码正在工作。 Problem was, that I'm an idiot.问题是,我是个白痴。 Because I should not calling Mapper.Map, but _mapper.Map.因为我不应该调用 Mapper.Map,而是调用 _mapper.Map。
For those who are using Automapper 9.0 with Castle Windsor (my version is 3.2.0) using container.对于那些使用容器使用 Automapper 9.0 和 Castle Windsor(我的版本是 3.2.0)的人。 I created a file where I register my model with Dtos.我创建了一个文件,在其中向 Dtos 注册我的模型。 1. AutoMapperProfile.cs 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 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. I register all of my installer on BootstrapConfig.cs 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()
);
}
}
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; }
When you want to return the Dto, simply use当您想返回 Dto 时,只需使用
return mapper.Map<IEnumerable<UserMenuReadModel>, List<UserMenuDto>>(result);
Last but not least, register IOC bootstrap config on global.asax最后但并非最不重要的是,在 global.asax 上注册 IOC 引导配置
container = new WindsorContainer(); BootstrapConfig.Register(this.container)
Hope this helps someone using new version of AutoMapper with Castle Windsor.希望这对使用新版 AutoMapper 和 Castle Windsor 的人有所帮助。 Feel free to comment with criticisms or suggestions.随意评论批评或建议。
I am still getting the issue "no parameterless constructor defined for this object"我仍然遇到“没有为此对象定义无参数构造函数”的问题
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(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 source)在 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.