MHO*_*OOS 4 c# castle-windsor ioc-container automapper
我使用以下答案中描述的以下方法来创建映射器的实例:
var platformSpecificRegistry = AutoMapper.Internal.PlatformAdapter.Resolve<IPlatformSpecificMapperRegistry>();
platformSpecificRegistry.Initialize();
var autoMapperCfg = new AutoMapper.ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers);
var mappingEngine = new AutoMapper.MappingEngine(_autoMapperCfg);
Run Code Online (Sandbox Code Playgroud)
如以下问题所述:
AutoMapper如何将对象A映射到对象B,这取决于上下文.
我如何能够使用[重用]如下所示的Automapper配置文件类来创建映射器的实例?
public class ApiTestClassToTestClassMappingProfile : Profile
{
protected override void Configure()
{
base.Configure();
Mapper.CreateMap<ApiTestClass, TestClass>()
.IgnoreAllNonExisting();
}
}
Run Code Online (Sandbox Code Playgroud)
只是为了让您了解我为什么需要这样的功能:我使用以下方法将所有Automapper Profile类注册到我的IoC容器[CastleWindsor]中:
IoC.WindsorContainer.Register(Types.FromThisAssembly()
.BasedOn<Profile>()
.WithServiceBase()
.Configure(c => c.LifeStyle.Is(LifestyleType.Singleton)));
var profiles = IoC.WindsorContainer.ResolveAll<Profile>();
foreach (var profile in profiles)
{
Mapper.AddProfile(profile);
}
IoC.WindsorContainer.Register(Component.For<IMappingEngine>().Instance(Mapper.Engine));
Run Code Online (Sandbox Code Playgroud)
虽然上面完全满足了初始化我的静态Mapper类的需要,但我真的不知道如何重新使用我的Automapper配置文件类来创建实例映射器[使用非静态映射器].
您需要确保您的Profile调用正确的CreateMap调用:
public class ApiTestClassToTestClassMappingProfile : Profile
{
protected override void Configure()
{
CreateMap<ApiTestClass, TestClass>()
.IgnoreAllNonExisting();
}
}
Run Code Online (Sandbox Code Playgroud)
基础Profile类上的CreateMap将该映射与该配置文件和配置相关联.
此外,您的IgnoreAllNonExisting应该被CreateMap调用中的MemberList.Source选项取代.这表示"使用源类型作为我的成员列表来验证而不是目标类型".
这是您使用配置文件创建MapperConfiguration的方法
public static class MappingProfile
{
public static MapperConfiguration InitializeAutoMapper()
{
MapperConfiguration config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new WebMappingProfile()); //mapping between Web and Business layer objects
cfg.AddProfile(new BLProfile()); // mapping between Business and DB layer objects
});
return config;
}
}
Run Code Online (Sandbox Code Playgroud)
个人资料示例
//Profile number one saved in Web layer
public class WebMappingProfile : Profile
{
public WebMappingProfile()
{
CreateMap<Question, QuestionModel>();
CreateMap<QuestionModel, Question>();
/*etc...*/
}
}
//Profile number two save in Business layer
public class BLProfile: Profile
{
public BLProfile()
{
CreateMap<BLModels.SubModels.Address, DataAccess.Models.EF.Address>();
/*etc....*/
}
}
Run Code Online (Sandbox Code Playgroud)
将自动映射器连接到DI框架(这是Unity)
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();
var mapper = MappingProfile.InitializeAutoMapper().CreateMapper();
container.RegisterInstance<IMapper>(mapper);
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
在您的课程中使用IMapper进行映射
public class BaseService
{
protected IMapper _mapper;
public BaseService(IMapper mapper)
{
_mapper = mapper;
}
public void AutoMapperDemo(){
var questions = GetQuestions(token);
return _mapper.Map<IEnumerable<Question>, IEnumerable<QuestionModel>>(questions);
}
public IEnumerable<Question> GetQuestions(token){
/*logic to get Questions*/
}
}
Run Code Online (Sandbox Code Playgroud)
我的原始帖子可以在这里找到:http : //www.codeproject.com/Articles/1129953/ASP-MVC-with-Automapper-Profiles
| 归档时间: |
|
| 查看次数: |
21151 次 |
| 最近记录: |