gpt4 book ai didi

c# - Automapper 使用自定义映射展平多个复杂对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:54 25 4
gpt4 key购买 nike

所以除了通常的 DTO 到业务映射器之外,我还有一些东西,我试图用最少的映射代码来映射它们。

设置

public class Target {

public string propA { get; set; }
public string propB { get; set; }
public string propC { get; set; }
public string propD { get; set; }
public string propE { get; set; }
public List<KeyValuePair> Tokens { get; set; }
}

public class Source {
public SomeClass SomeClass { get; set; }
public AnotherClass AnotherClass { get; set; }

}

public class SomeClass {
public string propA { get; set; }
public string propB { get; set; }
public string propDifferent { get; set; }
public List<KeyValuePair> Tokens { get; set; }
}

public class AnotherClass {
public string propC { get; set; }
public string propD { get; set; }
public List<KeyValuePair> Tokens { get; set; }
}

映射器配置

Mapper.CreateMap<SomeClass, Target>()
.ForMember(dest => dest.propE, opt => opt.MapFrom(src => src.propDifferent));


Mapper.CreateMap<AnotherClass, Target>();

Mapper.CreateMap<Source, Target>()
.ForMember(dest => dest, opt => opt.MapFrom(src => src.SomeClass))
.ForMember(dest => dest, opt => opt.MapFrom(src => src.AnotherClass));

这样做会抛出

Error: AutoMapper.AutoMapperConfigurationException: Custom configuration for members is only supported for top-level individual members on a type.

我还需要获取AnotherClass.TokensSomeClass.Tokens并将其添加到Target.Tokens

我知道我可以使用 .ConvertUsing 但我必须为每个属性定义映射,我失去了基于约定的匹配属性映射的优势。

是否有任何其他方法可以实现此目的(.ConvertUsing 或手动映射每个属性除外)?

如果不是通过 Automapper,是否可以通过 EmitMapper 实现?我想通过 EmitMapper 的 PostProcessing 可能可以添加到 Tokens 列表。

更新

经过一些黑客攻击,我找到了一个方法:

public static IMappingExpression<TSource, TDestination> FlattenNested<TSource, TNestedSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var sourceType = typeof(TNestedSource);
var destinationType = typeof(TDestination);
var sourceProperties = sourceType.GetProperties().ToDictionary(x => x.Name.ToLowerInvariant());
var childPropName = typeof (TSource).GetProperties().First(x => x.PropertyType == sourceType).Name;
var mappableProperties = destinationType.GetProperties()
.Where(p => sourceProperties.ContainsKey(p.Name.ToLowerInvariant()) &&
sourceProperties[p.Name.ToLowerInvariant()].PropertyType ==
p.PropertyType)
.Select(p => new {DestProperty = p.Name, SrcProperty = sourceProperties[p.Name.ToLowerInvariant()].Name});


foreach (var property in mappableProperties)
{
expression.ForMember(property.DestProperty,
opt => opt.MapFrom(src => src.GetPropertyValue(childPropName).GetPropertyValue(property.SrcProperty)));
}

return expression;
}

注意:我执行 Name.ToLowerInvariant() 是为了能够匹配 AccountID -> AccountId和类似的。

用法

AutoMapper.Mapper.CreateMap<Source, Target>()
.FlattenNested<Source, SomeClass, Target>()
.FlattenNested<Source, AnotherClass, Target>()
.ForMember(dest => dest.propE, opt => opt.MapFrom(src => src.propDifferent));

我在 IMappingExpression 中发现了一些其他属性,我也许可以使用这些属性并清理其中的大部分内容。当我找到它们时会更新。

最佳答案

这就是我解决类似问题的方法:

public static IMappingExpression<TSource, TDestination> FlattenNested<TSource, TNestedSource, TDestination>(
this IMappingExpression<TSource, TDestination> expression,
Expression<Func<TSource, TNestedSource>> nestedSelector,
IMappingExpression<TNestedSource, TDestination> nestedMappingExpression)
{
var dstProperties = typeof(TDestination).GetProperties().Select(p => p.Name);

var flattenedMappings = nestedMappingExpression.TypeMap.GetPropertyMaps()
.Where(pm => pm.IsMapped() && !pm.IsIgnored())
.ToDictionary(pm => pm.DestinationProperty.Name,
pm => Expression.Lambda(
Expression.MakeMemberAccess(nestedSelector.Body, pm.SourceMember),
nestedSelector.Parameters[0]));

foreach (var property in dstProperties)
{
if (!flattenedMappings.ContainsKey(property))
continue;

expression.ForMember(property, opt => opt.MapFrom((dynamic)flattenedMappings[property]));
}

return expression;
}

用法

public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}

public class Address
{
public string City { get; set; }
public string Street { get; set; }
}

public class CustomerDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Street { get; set; }
}

public class CustomerProfile : Profile
{
protected override void Configure()
{
var nestedMap = CreateMap<Address, CustomerDto>()
.IgnoreAllNonExisting();

CreateMap<Customer, CustomerDto>()
.FlattenNested(s => s.Address, nestedMap);
}
}

[TestFixture]
public class CustomerProfileTests
{
[Test]
public void Test()
{
Mapper.Initialize(c => c.AddProfile<CustomerProfile>());
Mapper.AssertConfigurationIsValid();
}
}

IgnoreAllNonExisting() 发现 here .

虽然这不是通用的解决方案,但对于简单的情况应该足够了。

优点是:

  1. 您使用 AutoMapper 创建嵌套 map ,因此您依赖于可信代码,您还可以使用诸如 RecognizePrefixes 之类的东西。
  2. 由于您需要指定嵌套属性选择器,因此当您有多个相同类型的嵌套属性时,可以避免可能出现的歧义。

关于c# - Automapper 使用自定义映射展平多个复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22720678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com