EF架構~AutoMapper對象映射工具簡化了實體賦值的過程


回到目錄

AutoMapper是一個.NET的對象映射工具,一般地,我們進行面向服務的開發時,都會涉及到DTO的概念,即數據傳輸對象,而為了減少系統的負載,一般我們不會把整個表的字段作為傳輸的數據,而是單獨根據具體場景,寫一個新的類,這個類一般以DTO結尾,意思是說,它是網絡上的數據傳輸用的,而你的DTO數據對象的賦值過程就成了一個問題,而為了減少賦值過程的代碼量,AutoMapper就出來了,它可以實現實體對實體的賦值過程,或者叫“映射過程”

我心中的項目應該是這樣的,用戶業務服務,產品業務服務,訂單業務服務,這樣服務都使 用單獨的數據庫,它們之間的通訊采用WCF進行實現,在獲數據時會在WEB端添加緩存機制,以減少對WCF的調用,而在WCF的網絡通訊中,數據類型一般 不會使用poco實體,因為它會有很多對當前業務無用的字段,我們會為具體業務建立具體的DTO對象,而entity實體與DTO實體之間的賦值過程我們 可以用AutoMapper來實現。

AutoMapper在程序中的體現:

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; }
    }

下面使用AutoMapper實現對象兩個實體的賦值,這是WCF服務中的代碼片斷:

public class ProductService : ServiceBase, IProductService
    {

        //通過ServiceLocator從IoC容器中獲得對象
        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
    }

怎么樣,這種方式的對象賦值很爽吧,呵呵。

 回到目錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM