对于某个聚合对象,可能存在嵌套,思路基本是一致的:
#region demo5
public class OuterSource
{
public int Value { get; set; }
public InnerSource Inner { get; set; }
}
public class InnerSource
{
public int OtherValue { get; set; }
}
public class OuterDest
{
public int Value { get; set; }
public InnerDest Inner { get; set; }
}
public class InnerDest
{
public int OtherValue { get; set; }
}
#endregion
#region Nesting
Mapper.Initialize(cfg =>
{
cfg.CreateMap<OuterSource, OuterDest>();
//cfg.CreateMap<InnerSource, InnerDest>();
});
var source = new OuterSource
{
Value = 5,
Inner = new InnerSource { OtherValue = 15 }
};
//验证类型映射是否正确
//Mapper.AssertConfigurationIsValid();
//执行映射
var dest = Mapper.Map<OuterSource, OuterDest>(source);
Console.WriteLine("dest.Value:" + dest.Value);
Console.WriteLine("dest.Inner is null:" + (dest.Inner == null ? "true" : "false"));
Console.WriteLine("dest.Inner.OtherValue:" + dest.Inner.OtherValue);
#endregion
关于解耦处理(使得映射的方式更加灵活,可以通过外部配置映射方式):
#region demo6
public class Source
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
}
public class Destination
{
public int Value1 { get; set; }
public DateTime Value2 { get; set; }
public Type Value3 { get; set; }
}
public class CustomResolver : IValueResolver<Source2, Destination2, int>
{
int IValueResolver<Source2, Destination2, int>.Resolve(Source2 source, Destination2 destMember, int destination, ResolutionContext context)
{
return source.Value1 + source.Value2;
}
}
public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
{
DateTime ITypeConverter<string, DateTime>.Convert(string source, DateTime destination, ResolutionContext context)
{
try
{
return System.Convert.ToDateTime(source);
}
catch
{
return DateTime.MinValue;
}
}
}
public class TypeTypeConverter : ITypeConverter<string, Type>
{
Type ITypeConverter<string, Type>.Convert(string source, Type destination, ResolutionContext context)
{
TypeConverter dd = TypeDescriptor.GetConverter(source.GetType());
dd.CanConvertTo(typeof(Type));
Type type = Assembly.GetExecutingAssembly().GetType(source);
return type;
}
}
#endregion
#region
var source = new Source
{
Value1 = "5",
Value2 = "01/18/2018jj",
Value3 = "LibStudy.Program"
};
Mapper.Initialize(cfg =>
{
cfg.CreateMap<string, int>().ConvertUsing(x => Convert.ToInt32(x));
cfg.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
cfg.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
cfg.CreateMap<Source, Destination>();
});
//验证类型映射是否正确
Mapper.AssertConfigurationIsValid();
Destination result = Mapper.Map<Source, Destination>(source);
Console.WriteLine("result.Value1:" + result.Value1.ToString());
Console.WriteLine("result.Value2:" + result.Value2.ToString());
Console.WriteLine("result.Value3:" + result.Value3.ToString());
#endregion
#region 解耦映射2
var source2 = new Source2
{
Value1 = 5,
Value2 = 7
};
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Source2, Destination2>()
//.ForMember(dest => dest.Total, opt => opt.MapFrom(src => src.Value1 + src.Value2));
.ForMember(dest => dest.Total, opt => opt.ResolveUsing<CustomResolver>());
});
var result2 = Mapper.Map<Source2, Destination2>(source2);
Console.WriteLine("result.Total:" + result2.Total);
#endregion
版权声明:本文为Joyhen原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。