1 AutoMapperExtensions(以泛型形式,扩展)
using AutoMapper;
namespace Web.Extensions
{
/// <summary>
/// 【自动映射扩展--类】
/// <remarks>
/// 摘要:
/// 该类通过“AutoMapper”中间件,以泛型形式,把1个指定类型的1/n个指定实例中的数据直接赋值到另1个指定类型的1个指定实例中;或另1个指定类型的枚举数实例中。
/// </remarks>
/// </summary>
public static class AutoMapperExtensions
{
/// <typeparam name="TSource">泛型类型实例(1个指定类型实例,该类型实例为赋值操作,提供数据支撑)。</typeparam>
/// <typeparam name="TDestination">泛型类型实例(另1个指定类型实例,该类型实例为被赋值操作,提供实例支撑)。</typeparam>
/// <param name="source">1个指定类的1个指定实例,该实例为赋值操作提供数据支撑。</param>
/// <summary>
/// 【映射到】
/// <remarks>
/// 摘要:
/// 通过相应的参数实例及其“AutoMapper”中间件,把1个指定类的1个指定实例中的数据,直接赋值到另1个指定类的1个指定实例中。
/// </remarks>
/// <returns>
/// 返回:
/// 被赋值后的另1个指定类型的1个指定实例。
/// </returns>
/// </summary>
public static TDestination MapTo<TSource, TDestination>(this TSource source) where TSource : class where TDestination : class
{
//如果1个指定类型实例中没有数据,向赋值操作提供,则直接退出当前方法。
if (source == null)
return default(TDestination);
//为1个指定类的实例与另1个指定类的实例,执行赋值操作时,获取两者之间的赋值操作映射规则。
MapperConfiguration _config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
//通过指定赋值操作映射规则实例,获取与之相应的映射器实例,通过该映射器实例1个指定类的1个指定实例,赋值到,另1个指定类的1个指定实例中,并最终返回另1个指定类的1个指定实例。
IMapper _mapper = _config.CreateMapper();
return _mapper.Map<TDestination>(source);
}
/// <typeparam name="TSource">泛型类型实例(1个指定类型实例,该类型实例为赋值操作,提供数据支撑)。</typeparam>
/// <typeparam name="TDestination">泛型类型实例(另1个指定类型实例,该类型实例为被赋值操作,提供实例支撑)。</typeparam>
/// <param name="source">枚举数实例,该实例中存储着1个指定类的1/n个指定实例,该枚举数实例为赋值操作提供数据支撑。</param>
/// <summary>
/// 【映射到列表】
/// <remarks>
/// 摘要:
/// 通过相应的参数实例及其“AutoMapper”中间件,把1个指定类的1/n个指定实例中的所有数据,直接赋值到另1个指定类型的1/n个指定实例,最后把另1个指定类的1/n个指定实例,存储到另1个指定类型的枚举数实例中。
/// </remarks>
/// <returns>
/// 返回:
/// 枚举数实例,该枚举数实例中存储着另1个指定类的1/n个指定实例。
/// </returns>
/// </summary>
public static IEnumerable<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source) where TSource : class where TDestination : class
{
//如果1个指定类型实例中没有数据,向赋值操作提供,则直接退出当前方法,并返回1个新的枚举数实例,该枚举数实例中没有存储任何数据。
if (source == null)
return new List<TDestination>();
//为1个指定类的实例与另1个指定类的实例,执行赋值操作时,获取两者之间的赋值操作映射规则。
var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
//通过指定赋值操作映射规则实例,获取与之相应的映射器实例,通过该映射器实例把枚举数实例所存储的1个指定类的所有实例,依次赋值到,另1个指定类的1/n个指定实例中,存储到另1个指定类型的枚举数实例中,并最终返回另1个指定类的枚举数实例。
var mapper = config.CreateMapper();
return mapper.Map<List<TDestination>>(source);
}
}
}
2 AutoMapperExtensionsExam(以指定类型(Exam),扩展)
using AutoMapper;
using Data.Domain.Students;
using Web.Models;
namespace Web.Extensions
{
/// <summary>
/// 【考试自动映射扩展--类】
/// <remarks>
/// 摘要:
/// 该类通过“AutoMapper”中间件,以指定类型(Exam)形式,把1个指定类型的1/n个指定实例中的数据直接赋值到另1个指定类型的1个指定实例中;或另1个指定类型的枚举数实例中。
/// </remarks>
/// </summary>
public static class AutoMapperExtensionsExam
{
/// <param name="examModel">考试模型记录的1个指定实例。</param>
/// <summary>
/// 【考试自动映射】
/// <remarks>
/// 摘要:
/// 通过相应的参数实例及其“AutoMapper”中间件,把考试模型记录的1个指定实例中的数据,通过相应的赋值操作映射规则,直接赋值到考试类的1个指定实例中。
/// </remarks>
/// <returns>
/// 返回:
/// 被赋值后的考试类的1个指定实例。
/// </returns>
/// </summary>
public static Exam ExamAutoMapper(this ExamModel examModel)
{
//如果考试模型记录实例中没有数据,向赋值操作提供,则直接退出当前方法。
if (examModel == null)
return default(Exam);
//为考试模型记录实例实例与考试类的实例,执行赋值操作时,定义两者之间的赋值操作映射规则。
//因为考试模型记录中的属性成员名称与考试类中的属性成员名称一致,所有不必为其定义特定的“.ForMember()”赋值操作映射规则。
MapperConfiguration _config = new MapperConfiguration(cfg => cfg.CreateMap<ExamModel, Exam>());
//通过指定赋值操作映射规则实例,获取与之相应的映射器实例,通过该映射器实例,把考试模型记录的1个指定实例中的数据,直接赋值到考试类的1个指定实例中,并最终返回考试类的1个指定实例。
IMapper _mapper = _config.CreateMapper();
return _mapper.Map<Exam>(examModel);
}
/// <param name="examModel">考试模型记录的1个指定实例。</param>
/// <summary>
/// 【考试映射到】
/// <remarks>
/// 摘要:
/// 通过自定义扩展方法,把考试模型记录的1个指定实例中的数据,通过相应的赋值操作映射规则,直接赋值到考试类的1个指定实例中。
/// </remarks>
/// <returns>
/// 返回:
/// 被赋值后的考试类的1个指定实例。
/// </returns>
/// </summary>
public static Exam ExamMapTo(this ExamModel examModel)
{
//如果考试模型记录实例中没有数据,向赋值操作提供,则直接退出当前方法。
if (examModel == null)
return default(Exam);
//通过自定义扩展方法,把考试模型记录的1个指定实例中的数据,通过相应的赋值操作映射规则,直接赋值到考试类的1个指定实例中。
return examModel.MapTo<ExamModel, Exam>();
}
}
}
3 重构Web.Controllers.LogicRenderController.[HttpPost]CreatePopup方法
[HttpPost]
public async Task<IActionResult> CreatePopup(ExamModel model)
{
Exam _exam = new Exam();
//常规赋值操作。
//_exam.Score = model.Score;
//_exam.Comment= model.Comment;
//_exam.Name = model.Name;
//_exam.CourseId = model.CourseId;
//_exam.StudentId = model.StudentId;
//以指定类型形式映射后,执行赋值操作。
//_exam = model.ExamAutoMapper();
//以泛型类型形式映射后,执行赋值操作。
//_exam = model.MapTo<ExamModel, Exam>();
//以指定类型形式调用泛型类型形式映射后,执行赋值操作。
_exam = model.ExamMapTo();
await _context.SugarScope.Insertable(_exam).ExecuteReturnEntityAsync();
model.SpecialtySelectList = PrepareSpecialty(0, true);
model.GradeSelectList = PrepareGrade(0, true);
model.CourseSelectList = PrepareCourse(0, true);
model.CategorySelectList = new List<SelectListItem>();
model.StudentSelectList = new List<SelectListItem>();
ViewBag.RefreshPage = true;
return View(model);
}
对以上功能更为具体实现和注释见:22-10-18-09_SqlSugarAcquaintance(初识SqlSugarCore之AutoMapper)。