[英]AutoMapper auto create createMap
我有一个服务正在调用另一个服务。 这两项服务都使用“相同的类”。 这些类的名称相同,具有相同的属性,但具有不同的命名空间,因此我需要使用 AutoMapper 将 map 从一种类型转换为另一种类型。
不,这很简单,因为我所要做的就是CreateMap<>
,但问题是我们有大约数百个类,我需要手动从中编写CreateMap<>
,并且它可以连接到我。 没有任何自动CreateMap
function。所以如果我说 CreateMap() 那么 AutoMapper 通过组织工作并找到所有类并自动为这些类及其子类等执行CreateMap
等等......
希望有一个简单的解决方案,或者我想一些反思可以解决它......
只需在选项中将CreateMissingTypeMaps
设置为 true:
var dto = Mapper.Map<FooDTO>
(foo, opts => opts.CreateMissingTypeMaps = true);
如果您需要经常使用它,请将 lambda 存储在委托字段中:
static readonly Action<IMappingOperationOptions> _mapperOptions =
opts => opts.CreateMissingTypeMaps = true;
...
var dto = Mapper.Map<FooDTO>(foo, _mapperOptions);
更新:
上述方法在 AutoMapper 的最新版本中不再适用。
相反,您应该创建一个将CreateMissingTypeMaps
设置为 true 的映射器配置,并从此配置创建一个映射器实例:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMissingTypeMaps = true;
// other configurations
});
var mapper = config.CreateMapper();
如果你想继续使用旧的静态 API(不再推荐),你也可以这样做:
Mapper.Initialize(cfg =>
{
cfg.CreateMissingTypeMaps = true;
// other configurations
});
更新 2 - Automapper 9 及更高版本:
从 Automapper 9.0 版开始,已删除CreateMissingTypeMaps
API。 Automapper 文档现在建议手动或使用反射显式配置地图。
CreateMissingTypeMaps 可以在您的个人资料中设置。 但是,建议为每个映射显式使用 CreateMap 并在每个配置文件的单元测试中调用 AssertConfigurationIsValid 以防止出现静默错误。
public class MyProfile : Profile {
CreateMissingTypeMaps = true;
// Mappings...
}
AutoMapper 有一个您可以使用的 DynamicMap 方法:这里有一个示例单元测试来说明它。
[TestClass]
public class AutoMapper_Example
{
[TestMethod]
public void AutoMapper_DynamicMap()
{
Source source = new Source {Id = 1, Name = "Mr FooBar"};
Target target = Mapper.DynamicMap<Target>(source);
Assert.AreEqual(1, target.Id);
Assert.AreEqual("Mr FooBar", target.Name);
}
private class Target
{
public int Id { get; set; }
public string Name { get; set; }
}
private class Source
{
public int Id { get; set; }
public string Name { get; set; }
}
}
如果有人仍然对此主题感兴趣,我创建了一个 NuGet package 提供自动映射功能,因为 AutoMapper 在某个版本中删除了它。
它在wakiter.AutoMapper.Extensions名称下可用。
要使用它,请调用CreateAutoMap
扩展方法,它会为您完成工作。
将
CreateMissingTypeMaps<\/code>选项设置为 true。
这是包
AutoMapper.Extensions.Microsoft.DependencyInjection<\/code>的 ASP.NET Core 示例:
public class Startup {
//...
public void ConfigureServices(IServiceCollection services) {
//...
services.AddAutoMapper(cfg => { cfg.CreateMissingTypeMaps = true; });
//...
}
//...
}
今天我在一些通用代码中也需要这个。 我试过这样的事情:
private static IMapper CreateMapper<T1, T2>()
{
return new MapperConfiguration(cfg => FillMapperConfig(cfg, typeof(T1), typeof(T2)))
.CreateMapper();
}
private static void FillMapperConfig(IMapperConfigurationExpression cfg, Type T1, Type T2)
{
if (T1 == T2)
{
return;
}
cfg.CreateMap(T1, T2);
foreach (PropertyInfo propertyInfo in T1.GetProperties())
{
PropertyInfo correspondingProperty =
T2.GetProperties()
.FirstOrDefault(p =>
p.Name == propertyInfo.Name);
if (correspondingProperty != null)
{
if (propertyInfo.PropertyType.IsGenericType &&
correspondingProperty.PropertyType.IsGenericType)
{
FillMapperConfig(
cfg,
propertyInfo.PropertyType.GetGenericArguments()[0],
correspondingProperty.PropertyType.GetGenericArguments()[0]);
}
else if (propertyInfo.PropertyType.IsClass &&
correspondingProperty.PropertyType.IsClass)
{
FillMapperConfig(
cfg,
propertyInfo.PropertyType,
correspondingProperty.PropertyType);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.