加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

实体类型的转换? Automapper OR 自定义

发布时间:2020-12-13 20:41:32 所属栏目:PHP教程 来源:网络整理
导读:在引入了entityframework以后,系统有了1种新的需求,即对Viewmodel和model之间的相互转换。 这之间有各种境地。今天拿出来品味1下。 1用automapper 方法很简单。①添加对automapper的援用 ② 在援用的时候,创建两个实体之间的映照关系。我们要做的只是将要

在引入了entity framework 以后,系统有了1种新的需求,即对Viewmodel 和model之间的相互转换。

这之间有各种境地。今天拿出来品味1下。


1 用automapper

  方法很简单。①添加对automapper的援用


② 在援用的时候,创建两个实体之间的映照关系。我们要做的只是将要映照的两个类型告知AutoMapper(调用Mapper类的Static方法CreateMap并传入要映照的类型):

 

<span style="font-family:KaiTi_GB2312;font-size:18px;"> Mapper.CreateMap<StudentViewModel,BasicStudentEntity>();</span>

③实行转换

<span style="font-family:KaiTi_GB2312;font-size:18px;"> BasicStudentEntity enStudent = Mapper.Map<StudentViewModel,BasicStudentEntity>(student);</span>
如果studentviewmodel中有值为空的属性,AutoMapper在映照的时候会把BasicStudentEntity中的相应属性也置为空。固然我所说的是1种最为简单的使用方式。最简单,但是其可用范围也是最小。要求结构完全1致,且字段名也完全相同。很多情况下由于viewmodel 中的1些字段和model的不完全1致。致使我们使用这样的方式,非常不灵活。


其实automapper还有1种使用情况,即字段名没必要完全1致。(但是其意义应1致),这样的话我们可以定义类型间的简单映照规则。

<span style="font-family:KaiTi_GB2312;font-size:18px;">1. var map = Mapper.CreateMap<StudentViewModel,BasicStudentEntity>();   2. map.ForMember(d => d.Name, opt => opt.MapFrom(s => s.SchoolName));   </span>

但是即使是这样的话,还是不能解决我的另外一个问题。那就是当StudentViewModel比BasicStudentEntity 中的字段要多(意义也不1致)的情况下,没法进行转换。


2自定义

这时候候我们想到了我们自己转换写的自定义方法。

<span style="font-family:KaiTi_GB2312;font-size:18px;"> #region 3.0.0 学生管理公共方法 将view 转换成model(basicStudent) /// <summary> /// 学生管理公共方法 将view 转换成model(basicStudent) /// </summary> /// <param name="enstudentList"></param> /// <returns></returns> public List<BasicStudentEntity> ChangeViewToBasicstudentModel(List<StudentViewModel> enstudentList) { List<BasicStudentEntity> studentList = new List<BasicStudentEntity>(); foreach (var item in enstudentList) { BasicStudentEntity student = new BasicStudentEntity() { StudentID = item.StudentID,StudentNo = item.StudentNo,UserCode = item.UserCode,EntryPartyTime = item.EntryPartyTime,Speciality = item.Speciality,HealthCondition = item.HealthCondition,ExamineeNumber = item.ExamineeNumber,FatherName = item.FatherName,MotherName = item.MotherName,FatherPhone = item.FatherPhone,MotherPhone = item.MotherName,TrainDestination = item.TrainDestination,Note = item.Note,Operator = item.Operator,TimeStamp = item.TimeStamp,CreditCardNo = item.CreditCardNo,Name = item.Name,Sex = item.Sex,PoliticalStatus = item.PoliticalStatus,PreviousName = item.PreviousName,Email = item.Email,CellPhoneNumber = item.CellPhoneNumber,HomeTelephone = item.HomeTelephone,BirthPlace = item.BirthPlace,HomeAddress = item.HomeAddress,Nation = item.Nation,RoomID = item.RoomID,DirectionID = item.DirectionID,ClassID = item.ClassID }; studentList.Add(student); } return studentList; } #endregion </span>


自己的方法还是用着方便1点。如果是底层封装的话,是否是再加上泛型就能够大家共同调用了。这部份没有自己实践。仅供交换。



(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读