不错的东西: AutoMapper
生活随笔
收集整理的這篇文章主要介紹了
不错的东西: AutoMapper
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
詳細信息可閱讀原文:http://csharppulse.blogspot.in/2013/08/crud-operations-using-automapper-in-c_381.html
這東西可簡化將模型轉(zhuǎn)換為實體的代碼,具體可看代碼:
目前習(xí)慣寫法的代碼:public ActionResult Details(int? id) { var dbContext = new MyDBDataContext(); var userDetails = dbContext.Users.FirstOrDefault(userId => userId.UserId == id); var user = new LearningMVC.Models.User(); if (userDetails != null) { user.UserId = userDetails.UserId; user.FirstName = userDetails.FirstName; user.LastName = userDetails.LastName; user.Address = userDetails.Address; user.PhoneNo = userDetails.PhoneNo; user.EMail = userDetails.EMail; user.Company = userDetails.Company; user.Designation = userDetails.Designation; } return View(user); }使用AutoMap后可這樣寫:
public ActionResult Details(int? id) { var dbContext = new MyDBDataContext(); Mapper.CreateMap<LearningMVC.User, LearningMVC.Models.User>(); var userDetails = dbContext.Users.FirstOrDefault(userId => userId.UserId == id); LearningMVC.Models.User user = Mapper.Map<LearningMVC.User, LearningMVC.Models.User>(userDetails); return View(user); }總結(jié)
以上是生活随笔為你收集整理的不错的东西: AutoMapper的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tcpdump源码分析系列7:main函
- 下一篇: 五 Django 1.5.4 User