Arg*_*a C 2 c# automapper automapper-3
在我的应用程序中,我需要在不同的对象对之间进行许多映射(域对象,DTO,ViewModels等)。我AutoMapper为此大量使用。
因此,我有一个通用的Mapper类,该类具有映射不同对象的方法。作为映射在应用程序中坚持,我尽我CreateMap()的static constructor,这样的映射创建只有一次,是由我可能会使用他们的时间准备。这个班级看起来像这样
public class SomeMapper
{
static SomeMapper()
{
Mapper.CreateMap<SomeType1, SomeOtherType1>();
Mapper.CreateMap<SomeType2, SomeOtherType2>();
Mapper.CreateMap<SomeType3, SomeOtherType3>();
//...
}
public SomeOtherType1 MapSomeHierarchy1(SomeType1 source)
{ //... }
public SomeOtherType2 MapSomeHierarchy2(SomeType2 source)
{ //... }
}
Run Code Online (Sandbox Code Playgroud)
问题1:什么是创建映射的更好方法?(以任何方式更好-性能,语义,标准惯例等)
问题2:此代码也用于Console应用程序中。在特定的运行中,它不需要所有映射。因此,与其急切地创建所有映射,不可以在运行时创建映射(如果尚不存在)?就像是
public SomeOtherTypeN MapSomeHierarchyN(SomeTypeN source)
{
if (!AlreadyMapped(SomeTypeN, SomeOtherTypeN))
Mapper.CreateMap<SomeTypeN, SomeOtherTypeN>();
return Mapper.Map<SomeOtherTypeN>(source);
}
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来实现该方法AlreadyMapped()?
正如您所说的,映射仅在应用程序的生命周期内创建一次。我建议进行两个主要更改:
这些较小的单元可以单独进行单元测试,因此可以确保自动映射,显式映射或忽略所有目标属性。
public class MyProfile : Profile
{
protected override void Configure()
{
// Note, don't use Mapper.CreateMap here!
CreateMap<SomeType1, SomeOtherType1>();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以加载各个概要文件,从而可以在模块化应用程序中更接近的位置定义它们。
Mapper.AddProfile<MyProfile>();
Run Code Online (Sandbox Code Playgroud)
配置文件可以单独测试:
Mapper.AssertConfigurationIsValid<MyProfile>();
Run Code Online (Sandbox Code Playgroud)
我通常在每个配置文件中都包含一个单元测试-这样,如果您的源对象或目标对象以破坏映射的方式更改,您将立即知道。
从技术上讲,您可以在应用程序生命周期中的任何时候创建映射,但是如果您告诉您已完成,则AutoMapper会进行各种优化。如果您通过继承执行任何复杂的映射,则其中一些必不可少。而不是动态创建映射:
Mapper.CreateMap<SomeType1, SomeOtherType1>();
Mapper.AddProfile<MyProfile>();
Run Code Online (Sandbox Code Playgroud)
您应该改为使用Mapper.Initialize加载它们:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<SomeType1, SomeOtherType1>();
cfg.AddProfile<MyProfile>();
});
Run Code Online (Sandbox Code Playgroud)
如果绝对必须随时添加映射,则可以使用添加映射之后,强制AutoMapper再次执行其优化Mapper.Configuration.Seal()。
最后,如果您使用的是IoC容器,则可以通过在AutoMapper容器中注册所有配置文件,然后使用它们来查找和注册它们来结合这两种技术。这是使用Autofac的示例:
// Register Components...
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<MyProfile>().As<Profile>();
// This could also be done with assembly scanning...
builder.RegisterAssemblyTypes(typeof<MyProfile>.Assembly).As<Profile>();
// Build container...
var container = builder.Build();
// Configure AutoMapper
var profiles = container.Resolve<IEnumerable<Profile>>();
Mapper.Initialise(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});
Run Code Online (Sandbox Code Playgroud)
关于第二个问题,如果您遵循我在启动时创建映射的观点,则将不需要此操作,但是定义一个已经存在的映射将覆盖先前的映射,因此不会产生任何效果。