问题描述
我一直在努力将AutoMapper注入控制器.我喜欢Code Camp服务器的实现.它围绕AutoMapper的IMappingEngine创建包装器.依赖项注入是使用StructureMap完成的.但是我需要在项目中使用温莎城堡.那么,我们如何使用Windsor实施以下依赖项注入和设置?我不是在温莎城堡中寻找逐行等效的实现.如果您想这样做,请放心.相反,温莎相当于StructureMap的注册表和配置文件的内容是什么?我需要Profile来定义CreateMap<>,如下所示.
I have been working on injecting AutoMapper into controllers. I like the implementation of Code Camp Server. It creates a wrapper around AutoMapper's IMappingEngine. The dependency injection is done using StructureMap. But I need to use Castle Windsor for my project. So, how do we implement the following dependency injection and set-up using Windsor? I am not looking for line-by-line equivalent implementation in Castle Windsor. If you want to do that, please feel free. Instead, what is Windsor equivalent of StructureMap's Registry and Profile? I need Profile to define CreateMap<> like the following.
谢谢.
会议控制器:
public MeetingController(IMeetingMapper meetingMapper, ...)
会议映射器:
public class MeetingMapper : IMeetingMapper
{
private readonly IMappingEngine _mappingEngine;
public MeetingMapper(IMappingEngine mappingEngine)
{
_mappingEngine = mappingEngine;
}
public MeetingInput Map(Meeting model)
{
return _mappingEngine.Map<Meeting, MeetingInput>(model);
}
......
}
自动映射器注册表:
public class AutoMapperRegistry : Registry
{
public AutoMapperRegistry()
{
ForRequestedType<IMappingEngine>().TheDefault.Is.ConstructedBy(() => Mapper.Engine);
}
}
会议映射器配置文件:
public class MeetingMapperProfile : Profile
{
public static Func<Type, object> CreateDependencyCallback = (type) => Activator.CreateInstance(type);
public T CreateDependency<T>()
{
return (T)CreateDependencyCallback(typeof(T));
}
protected override void Configure()
{
Mapper.CreateMap<MeetingInput, Meeting>().ConstructUsing(
input => CreateDependency<IMeetingRepository>().GetById(input.Id) ?? new Meeting())
.ForMember(x => x.UserGroup, o => o.MapFrom(x => x.UserGroupId))
.ForMember(x => x.Address, o => o.Ignore())
.ForMember(x => x.City, o => o.Ignore())
.ForMember(x => x.Region, o => o.Ignore())
.ForMember(x => x.PostalCode, o => o.Ignore())
.ForMember(x => x.ChangeAuditInfo, o => o.Ignore());
}
}
推荐答案
您的意思是如何在Windsor中注册它?
you mean how do you register it in Windsor?
您可能必须注册FactorySupportFacility拳头...目前我无法检查.
you may have to register FactorySupportFacility fist... I have no way of checking at this moment.
container.AddFacility<FactorySupportFacility>();
然后
container.Register(Component.For<IMappingEngine>().UsingFactoryMethod(()=>
Mapper.Engine));
这篇关于注入AutoMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!