/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:AutoMapper映射帮助类-WPF (AutoMapper 12.0.0)
*│ 作 者:执笔小白
*│ 版 本:1.0
*│ 创建时间:2023-01-14 15:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Service._Base
*│ 类 名:AutoMapperBase
*└──────────────────────────────────────────────────────────────┘
*/
using AutoMapper;
using OrderTable_WinForm.Model;
using OrderTable_WinForm.ViewModel;
using System;
using System.Collections.Generic;
namespace OrderTable_WinForm.Service._Base
{
/// <summary>
/// AutoMapper映射帮助类-WPF
/// AutoMapper 版本12.0.0
/// </summary>
public class AutoMapperBase
{
#region 实体映射
/// <summary>
/// 1、类型映射_默认字段一一对应
/// </summary>
/// <typeparam name="TSource">源类型</typeparam>
/// <typeparam name="TDestination">目标类型</typeparam>
/// <param name="tSource">源数据</param>
/// <returns></returns>
public static TDestination AutoMapperTo<TSource, TDestination>(TSource tSource) where TSource : class where TDestination : class
{
if (tSource == null) return default;
var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
var mapper = config.CreateMapper();
return mapper.Map<TDestination>(tSource);
}
/// <summary>
/// 2、类型映射
/// ① 字段名称不对应或
/// ② 类型转化
/// ③ 字段省略
/// ④ 字段名称或类型不对应
/// ⑤ 条件赋值或null处理
/// ⑥ 组合赋值
/// </summary>
/// <typeparam name="TSource">源类型</typeparam>
/// <typeparam name="TDestination">目标类型</typeparam>
/// <param name="tSource">源数据</param>
/// <param name="configurationExpression">类型</param>
/// <returns></returns>
public static TDestination AutoMapperTo<TSource, TDestination>(TSource tSource, MapperConfigurationExpression configurationExpression) where TSource : class where TDestination : class
{
if (tSource == null) return default;
//var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>()
// .ForMember(d => d.DestName, opt => opt.MapFrom(s => s.Name)) // 指定字段一一对应
// .ForMember(d => d.Birthday, opt => opt.MapFrom(src => src.Birthday.ToString("yy-MM-dd HH:mm"))) // 指定字段,并转化指定的格式
// .ForMember(d => d.Age, opt => opt.Condition(src => src.Age > 5)) // 条件赋值
// .ForMember(d => d.A1, opt => opt.Ignore()) // 忽略该字段,不给该字段赋值
// .ForMember(d => d.A1, opt => opt.NullSubstitute("Default Value")) // 如果源字段值为空,则赋值为 Default Value
// .ForMember(d => d.A1, opt => opt.MapFrom(src => src.Name + src.Age * 3 + src.Birthday.ToString("d")))); // 可以自己随意组合赋值
var config = new MapperConfiguration(configurationExpression); // 可以自己随意组合赋值
var mapper = config.CreateMapper();
return mapper.Map<TDestination>(tSource);
}
#endregion 实体映射
#region 列表映射
/// <summary>
/// 集合列表类型映射,默认字段名字一一对应
/// </summary>
/// <typeparam name="TSource">源类型</typeparam>
/// <typeparam name="TDestination">目标类型</typeparam>
/// <param name="source">源数据</param>
/// <returns></returns>
public static List<TDestination> AutoMapperToList<TSource, TDestination>(List<TSource> sources) where TSource : class where TDestination : class
{
if (sources == null) return new List<TDestination>();
var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
var mapper = config.CreateMapper();
return mapper.Map<List<TDestination>>(sources);
}
#endregion 列表映射
}
/// <summary>
/// AutoMapper映射帮助类 - WPF(AutoMapper 12.0.0)
/// </summary>
public class AutoMapperBaseTeat
{
#region 实体映射
/// <summary>
/// 1、类型映射_默认字段一一对应
/// </summary>
public void AutoMapperToDemo()
{
TableTest_ViewModel tableTest1 = new();
TableTest tableTest= AutoMapperBase.AutoMapperTo<TableTest_ViewModel, TableTest>(tableTest1);
}
/// <summary>
/// 2、类型映射
/// ① 字段名称不对应或
/// ② 类型转化
/// ③ 字段省略
/// ④ 字段名称或类型不对应
/// ⑤ 条件赋值或null处理
/// ⑥ 组合赋值
/// </summary>
public void AutoMapperTo2Demo()
{
TableTest_ViewModel tableTest1 = new();
// 这里是语句,不是无用的数据;
MapperConfigurationExpression mapperConfiguration = new();
//var config = new MapperConfiguration(cfg => cfg.CreateMap<TableTest_ViewModel, TableTest>()
// .ForMember(d => d.DestName, opt => opt.MapFrom(s => s.Name)) // 指定字段一一对应
// .ForMember(d => d.Birthday, opt => opt.MapFrom(src => src.Birthday.ToString("yy-MM-dd HH:mm"))) // 指定字段,并转化指定的格式
// .ForMember(d => d.Age, opt => opt.Condition(src => src.Age > 5)) // 条件赋值
// .ForMember(d => d.A1, opt => opt.Ignore()) // 忽略该字段,不给该字段赋值
// .ForMember(d => d.A1, opt => opt.NullSubstitute("Default Value")) // 如果源字段值为空,则赋值为 Default Value
// .ForMember(d => d.A1, opt => opt.MapFrom(src => src.Name + src.Age * 3 + src.Birthday.ToString("d")))); // 可以自己随意组合赋值
TableTest tableTest = AutoMapperBase.AutoMapperTo<TableTest_ViewModel, TableTest>(tableTest1, mapperConfiguration);
}
#endregion 实体映射
#region 列表映射
/// <summary>
/// 集合列表类型映射,默认字段名字一一对应
/// </summary>
public void AutoMapperToListDemo()
{
List<TableTest_ViewModel> tableTestVMs = new();
List<TableTest> tableTests = AutoMapperBase.AutoMapperToList<TableTest_ViewModel, TableTest>(tableTestVMs);
}
#endregion 列表映射
}
}
标签:opt,src,映射,ForMember,AutoMapper,new,WPF,赋值
From: https://www.cnblogs.com/qq2806933146xiaobai/p/17057866.html