EF架構~AutoMapper對象映射工具簡(jian)化了實體賦值的過(guo)程
AutoMapper是一個.NET的對象映射工具,一般地,我們進行面向服務的開發時,都會涉及到DTO的概念,即數據傳輸對象,而為了減少系統的負載,一般我們不會把整個表的字段作為傳輸的數據,而是單獨根據具體場景,寫一個新的類,這個類一般以DTO結尾,意思是說,它是網絡上的數據傳輸用的,而你的DTO數據對象的賦值過程就成了一個問題,而為了減少賦值過程的代碼量,AutoMapper就出來了,它可以實現實體對實體的賦值過程,或者叫“映射過程”。
我(wo)心中(zhong)的(de)項目(mu)應該是(shi)這(zhe)樣的(de),用(yong)戶(hu)業(ye)(ye)務服(fu)務,產品業(ye)(ye)務服(fu)務,訂單(dan)業(ye)(ye)務服(fu)務,這(zhe)樣服(fu)務都(dou)使 用(yong)單(dan)獨(du)的(de)數(shu)(shu)據庫,它們之間(jian)的(de)通(tong)訊采用(yong)WCF進(jin)行實(shi)現(xian),在獲數(shu)(shu)據時會(hui)在WEB端添(tian)加緩存機(ji)制(zhi),以減(jian)少對(dui)(dui)WCF的(de)調用(yong),而在WCF的(de)網(wang)絡通(tong)訊中(zhong),數(shu)(shu)據類型一(yi)般 不會(hui)使用(yong)poco實(shi)體(ti),因(yin)為(wei)它會(hui)有很多對(dui)(dui)當前業(ye)(ye)務無用(yong)的(de)字段,我(wo)們會(hui)為(wei)具體(ti)業(ye)(ye)務建立具體(ti)的(de)DTO對(dui)(dui)象(xiang),而entity實(shi)體(ti)與(yu)DTO實(shi)體(ti)之間(jian)的(de)賦值過程我(wo)們 可(ke)以用(yong)AutoMapper來實(shi)現(xian)。
AutoMapper在程(cheng)序中的(de)體現:
DTO實體
[DataContract] public class ProductDTO { [DataMember] public int ProductID { get; set; } [DataMember] public string ProductName { get; set; } [DataMember] public System.DateTime CreateDate { get; set; } [DataMember] public int SaleCount { get; set; } [DataMember] public Nullable<int> ClickCount { get; set; } [DataMember] public string Info { get; set; } [DataMember] public int UserID { get; set; } [DataMember] public decimal SalePrice { get; set; } [DataMember] public int Discount { get; set; } }
POCO實體:
public partial class Product { public Product() { this.ProductDetail = new HashSet<ProductDetail>(); } public int ProductID { get; set; } public string ProductName { get; set; } public System.DateTime CreateDate { get; set; } public int SaleCount { get; set; } public Nullable<int> ClickCount { get; set; } public string Info { get; set; } public int UserID { get; set; } public decimal SalePrice { get; set; } public int Discount { get; set; } public System.DateTime UpdateDate { get; set; } public virtual User_Info User_Info { get; set; } public virtual ICollection<ProductDetail> ProductDetail { get; set; } }
下面使用(yong)AutoMapper實現對象兩個實體的(de)(de)賦(fu)值,這(zhe)是WCF服(fu)務中的(de)(de)代碼片斷:
public class ProductService : ServiceBase, IProductService { //通過ServiceLocator從(cong)IoC容器中獲得(de)對象(xiang) IProductRepository productRepository = ServiceLocator.Instance.GetService<IProductRepository>(); #region IProductService 成員 public ProductDTO CreateProduct(ProductDTO productDTO) { Mapper.CreateMap<ProductDTO, Product>(); Product product = Mapper.Map<ProductDTO, Product>(productDTO); productRepository.AddProduct(product); return productDTO; } public List<ProductDTO> GetProduct() { Mapper.CreateMap<Product, ProductDTO>(); List<ProductDTO> arr = new List<ProductDTO>(); productRepository.GetProduct().ForEach(i => { arr.Add(Mapper.Map<Product, ProductDTO>(i)); }); return arr; } public ProductDTO ModifyProduct(ProductDTO productDTO) { Mapper.CreateMap<ProductDTO, Product>(); Product product = Mapper.Map<ProductDTO, Product>(productDTO); productRepository.ModifyProduct(product); return productDTO; } #endregion }
怎么樣,這(zhe)種方式的對象賦值很(hen)爽吧,呵呵。