网站建设的空间是什么意思,网络网站是多少钱一年,wordpress功能图,网店代理免费一件代发先说说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