当前位置: 首页 > news >正文

深圳网站建设服务哪个便宜点广西建设职业技术学院官网

深圳网站建设服务哪个便宜点,广西建设职业技术学院官网,宠物网站建设总结,优秀手机网站设计先说说DTO DTO是个什么东东#xff1f; DTO#xff08;Data Transfer Object#xff09;就是数据传输对象#xff0c;说白了就是一个对象#xff0c;只不过里边全是数据而已。 为什么要用DTO? 1、DTO更注重数据#xff0c;对领域对象进行合理封装#xff0c;从而不会将… 先说说DTO DTO是个什么东东 DTOData Transfer Object就是数据传输对象说白了就是一个对象只不过里边全是数据而已。 为什么要用DTO? 1、DTO更注重数据对领域对象进行合理封装从而不会将领域对象的行为过分暴露给表现层 2、DTO是面向UI的需求而设计的而领域模型是面向业务而设计的。因此DTO更适合于和表现层的交互通过DTO我们实现了表现层与领域Model之间的解耦因此改动领域Model不会影响UI层 3、DTO说白了就是数据而已不包含任何的业务逻辑属于瘦身型的对象使用时可以根据不同的UI需求进行灵活的运用 AutoMapper 现在我们既然知道了使用DTO的好处那么我们肯定也想马上使用它但是这里会牵扯一个问题怎样实现DTO和领域Model之间的转换 有两个思路我们要么自己写转换代码要么使用工具。不过就应用而言我还是觉得用工具比较简单快捷那就使用工具吧。其实这样的转换工具很多不过我还是决定使用AutoMapper因为它足够轻量级而且也非常流行国外的大牛们都使用它。使用AutoMapper可以很方便的实现DTO和领域Model之间的转换它是一个强大的Object-Object Mapping工具。 一、如何添加AutoMapper到项目中 在vs中使用打开工具-库程序包管理器-程序包管理控制平台输入“Install-Package AutoMapper”命令就可以把AutoMapper添加到项目中了~ 二、吃点栗子 栗子1两个类型之间的映射 Mapper.CreateMapAddressDto, Address();AddressDto dto new AddressDto{Country China,City ShangHai,Street JinZhong Street}; Address address Mapper.MapAddressDto,Address(Dto); 栗子2两个映射的对象有部分字段名称不一样 AddressDto到Address的映射AddressDto的字段CountryName要对应Address的字段Country Mapper.CreateMapAddressDto, Address(). ForMember(d d.Country, opt opt.MapFrom(s s.CountryName)); 栗子3列表类型之间的映射 源类型ListAddress目标类型ListAddressDto AutoMapper.Mapper.CreateMap Address, AddressDto (); var addressDtoList AutoMapper.Mapper.MapList Address , List AddressDto ( addressList); 栗子4映射在增改查中的应用 public class ProductBll{Public IProductRepository productRepository{ set; get; }public ProductDTO CreateProduct(ProductDTO productDTO){Mapper.CreateMapProductDTO, Product();Product product Mapper.MapProductDTO, Product(productDTO);productRepository.AddProduct(product);return productDTO;}public ListProductDTO GetProduct(){Mapper.CreateMapProduct, ProductDTO();ListProductDTO arr new ListProductDTO();productRepository.GetProduct().ForEach(i {arr.Add(Mapper.MapProduct, ProductDTO(i));});return arr;}public ProductDTO ModifyProduct(ProductDTO productDTO){Mapper.CreateMapProductDTO, Product();Product product Mapper.MapProductDTO, Product(productDTO);productRepository.ModifyProduct(product);return productDTO;} } 三、让AutoMapper使用变得简单 吃过上面的栗子你觉得怎么样呢如果想继续吃那就去查看AutoMapper的具体API文档吧倘若在项目中真正要用的时候我觉得还是应该对AutoMapper的方法进行一些整理最好能够封装一下这里我通过扩展方法的形式将其封装为AutoMapperHelper这样以后使用AutoMapper就变的SO EASY了~ using System.Collections; using System.Collections.Generic; using System.Data; using AutoMapper; namespace Infrastructure.Utility{/// summary/// AutoMapper扩展帮助类/// /summarypublic static class AutoMapperHelper{/// summary/// 类型映射/// /summary public static T MapToT(this object obj){if (obj null) return default(T);Mapper.CreateMap(obj.GetType(), typeof(T));return Mapper.MapT(obj);}/// summary/// 集合列表类型映射/// /summary public static ListTDestination MapToListTDestination(this IEnumerable source){foreach (var first in source){var type first.GetType();Mapper.CreateMap(type, typeof(TDestination));break;}return Mapper.MapListTDestination(source);}/// summary/// 集合列表类型映射/// /summary public static ListTDestination MapToListTSource, TDestination(this IEnumerableTSource source){//IEnumerableT 类型需要创建元素的映射Mapper.CreateMapTSource, TDestination();return Mapper.MapListTDestination(source);}/// summary/// 类型映射/// /summary public static TDestination MapToTSource, TDestination(this TSource source, TDestination destination)where TSource : classwhere TDestination : class{if (source null) return destination;Mapper.CreateMapTSource, TDestination();return Mapper.Map(source, destination);}/// summary/// DataReader映射/// /summary public static IEnumerableT DataReaderMapToT(this IDataReader reader){Mapper.Reset();Mapper.CreateMapIDataReader, IEnumerableT();return Mapper.MapIDataReader, IEnumerableT(reader);}} } 你可以像下面的栗子这样使用 //对象映射ShipInfoModel shipInfoModel ShipInfo.MapToShipInfoModel();//列表映射List ShipInfoModel shipInfoModellist ShipInfoList.MapToListShipInfoModel(); 小结 在项目中多使用DTO实现表现层与领域Model的解耦用AutoMapper来实现DTO与领域Model的相互转换 转载于:https://www.cnblogs.com/yanglang/p/6902360.html
http://wiki.neutronadmin.com/news/183650/

相关文章:

  • 广州网站建设需要多少费用专业网站建设模块
  • 网站建设相关图片天津seo博客
  • 建立网站需要多少钱经营y湖南岚鸿非常好沈阳网站建设费用
  • 可以用来注册网站域名的入口是怎么找电商平台合作
  • 鹤壁网站推广公司浏览器加速器
  • 做网站需要流程设计好用的网站
  • 做网站建设需要十堰网站建设公司电话
  • 优化网站排名怎么样有没有免费的网站服务器
  • 做网站直接开二级域名专门做墓志铭的网站
  • 做网站优化费用六安市公司网站建设
  • wordpress搜索400百度快照优化公司
  • 怎么用ajax做电商网站用ai做网页设计
  • 网站建设精美模板下载老域名购买
  • 珠海网站建设小小网络做网站实现图片自动压缩
  • 做交流网站有哪些手机医疗网站建设
  • 重庆做网站设计价格合理的网站建设
  • thinkphp做的网站小男孩与大人做的网站
  • jsp网站开发案例网站盗号怎么做
  • 推广外贸网站seo的基本步骤
  • 开网站卖茶要怎么做个人怎么进行网站建设
  • 地产网站方案属于网页制作工具的是
  • 佛山cms模板建站网络事件营销
  • 深圳品牌创意网站建设seo资料
  • 做网站这么便宜可以吗wordpress 百度云
  • 衣服网站建设方案wordpress商城微信支付
  • 门户网站建设自查整改报告前端网站优化
  • 猫眼网站建设简单的网站怎么做的
  • 临沂网站建设电话厦门网站排名优化价格
  • 一男一女做那个的动漫视频网站学会网站开发需要多久
  • 做代加工的网站发布电商网站建设