日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

集成和配置AutoMapper

系統 1949 0

?

AutoMapper的基本使用請參考 : http://www.cnblogs.com/ljzforever/archive/2011/12/29/2305500.html

?

學習一下Nop是如何配置和集成AutoMapper的。

?

IStartupTask.cs

      
        namespace
      
      
         Nop.Core.Infrastructure

{

    
      
      
        public
      
      
        interface
      
      
         IStartupTask 

    {

        
      
      
        void
      
      
         Execute();



        
      
      
        int
      
       Order { 
      
        get
      
      
        ; }

    }

}
      
    

?

之前的隨便介紹了 ITypeFinder實現 , 項目啟動時,ITypeFinder會找到項目中所有繼承 IStartupTask 接口的實現。其中就有AutoMapperStartupTask

      
        using
      
      
         AutoMapper;


      
      
        using
      
      
         Nop.Admin.Models.Blogs;


      
      
        using
      
      
         Nop.Admin.Models.Catalog;


      
      
        using
      
      
         Nop.Admin.Models.Cms;


      
      
        using
      
      
         Nop.Admin.Models.Common;


      
      
        using
      
      
         Nop.Admin.Models.Customers;


      
      
        using
      
      
         Nop.Admin.Models.Directory;


      
      
        using
      
      
         Nop.Admin.Models.Discounts;


      
      
        using
      
      
         Nop.Admin.Models.ExternalAuthentication;


      
      
        using
      
      
         Nop.Admin.Models.Forums;


      
      
        using
      
      
         Nop.Admin.Models.Localization;


      
      
        using
      
      
         Nop.Admin.Models.Logging;


      
      
        using
      
      
         Nop.Admin.Models.Messages;


      
      
        using
      
      
         Nop.Admin.Models.News;


      
      
        using
      
      
         Nop.Admin.Models.Orders;


      
      
        using
      
      
         Nop.Admin.Models.Payments;


      
      
        using
      
      
         Nop.Admin.Models.Plugins;


      
      
        using
      
      
         Nop.Admin.Models.Polls;


      
      
        using
      
      
         Nop.Admin.Models.Settings;


      
      
        using
      
      
         Nop.Admin.Models.Shipping;


      
      
        using
      
      
         Nop.Admin.Models.Tax;


      
      
        using
      
      
         Nop.Admin.Models.Topics;


      
      
        using
      
      
         Nop.Core.Domain.Blogs;


      
      
        using
      
      
         Nop.Core.Domain.Catalog;


      
      
        using
      
      
         Nop.Core.Domain.Common;


      
      
        using
      
      
         Nop.Core.Domain.Customers;


      
      
        using
      
      
         Nop.Core.Domain.Directory;


      
      
        using
      
      
         Nop.Core.Domain.Discounts;


      
      
        using
      
      
         Nop.Core.Domain.Forums;


      
      
        using
      
      
         Nop.Core.Domain.Localization;


      
      
        using
      
      
         Nop.Core.Domain.Logging;


      
      
        using
      
      
         Nop.Core.Domain.Media;


      
      
        using
      
      
         Nop.Core.Domain.Messages;


      
      
        using
      
      
         Nop.Core.Domain.News;


      
      
        using
      
      
         Nop.Core.Domain.Orders;


      
      
        using
      
      
         Nop.Core.Domain.Polls;


      
      
        using
      
      
         Nop.Core.Domain.Shipping;


      
      
        using
      
      
         Nop.Core.Domain.Tax;


      
      
        using
      
      
         Nop.Core.Domain.Topics;


      
      
        using
      
      
         Nop.Core.Infrastructure;


      
      
        using
      
      
         Nop.Core.Plugins;


      
      
        using
      
      
         Nop.Services.Authentication.External;


      
      
        using
      
      
         Nop.Services.Cms;


      
      
        using
      
      
         Nop.Services.Messages;


      
      
        using
      
      
         Nop.Services.Payments;


      
      
        using
      
      
         Nop.Services.Shipping;


      
      
        using
      
      
         Nop.Services.Tax;




      
      
        namespace
      
      
         Nop.Admin.Infrastructure

{

    
      
      
        public
      
      
        class
      
      
         AutoMapperStartupTask : IStartupTask

    {

        
      
      
        public
      
      
        void
      
      
         Execute()

        {

            
      
      
        //
      
      
        TODO remove 'CreatedOnUtc' ignore mappings because now presentation layer models have 'CreatedOn' property and core entities have 'CreatedOnUtc' property (distinct names)

            

            
      
      
        //
      
      
        address
      
      

            Mapper.CreateMap<Address, AddressModel>
      
        ()

                .ForMember(dest 
      
      => dest.AddressHtml, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.AvailableCountries, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.AvailableStates, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.FirstNameDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.LastNameDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.EmailDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.CompanyDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.CountryDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.StateProvinceDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.CityDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.Address1Disabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.Address2Disabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.ZipPostalCodeDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.PhoneNumberDisabled, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.FaxNumberDisabled, mo =>
      
         mo.Ignore());

            Mapper.CreateMap
      
      <AddressModel, Address>
      
        ()

                .ForMember(dest 
      
      => dest.CreatedOnUtc, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.Country, mo =>
      
         mo.Ignore())

                .ForMember(dest 
      
      => dest.StateProvince, mo =>
      
         mo.Ignore());


      
    
        
                      ……

}
        
      

?

然后是單元測試,AutoMapperStartupTask 提交后進行集成測試,驗證配置是否正確。

      
        [TestFixture]

    
      
      
        public
      
      
        class
      
      
         AutoMapperConfigurationTest

    {

        [Test]

        
      
      
        public
      
      
        void
      
      
         Configuration_is_valid()

        {

            
      
      
        var
      
       autoMapperStartupTask = 
      
        new
      
      
         AutoMapperStartupTask();

            autoMapperStartupTask.Execute();

            Mapper.AssertConfigurationIsValid();

        }

    }
      
    

?

這里一個專門用于Mapper的擴展方法。

      
        public
      
      
        static
      
      
        class
      
      
         MappingExtensions

    {

        
      
      
        #region
      
       Category



        
      
        public
      
      
        static
      
       CategoryModel ToModel(
      
        this
      
      
         Category entity)

        {

            
      
      
        return
      
       Mapper.Map<Category, CategoryModel>
      
        (entity);

        }



        
      
      
        public
      
      
        static
      
       Category ToEntity(
      
        this
      
      
         CategoryModel model)

        {

            
      
      
        return
      
       Mapper.Map<CategoryModel, Category>
      
        (model);

        }



        
      
      
        public
      
      
        static
      
       Category ToEntity(
      
        this
      
      
         CategoryModel model, Category destination)

        {

            
      
      
        return
      
      
         Mapper.Map(model, destination);

        }

。。。
      
    

?

最后是調用完成ViewModel,Entity的轉換

      
        var
      
       gridModel = 
      
        new
      
       GridModel<CategoryModel>
      
        

            {

                Data 
      
      = categories.Select(x =>
      
        

                {

                    
      
      
        var
      
       categoryModel =
      
         x.ToModel();

                    categoryModel.Breadcrumb 
      
      =
      
         x.GetCategoryBreadCrumb(_categoryService);

                    
      
      
        return
      
      
         categoryModel;

                }),

                Total 
      
      =
      
         categories.TotalCount

            };

            
      
      
        return
      
      
        new
      
      
         JsonResult

            {

                Data 
      
      =
      
         gridModel

            };
      
    

?

總結: 光學會如何使用一個工具還不行,還要學習它如何能和自己的項目進行良好的集成。。使用不當的話反而影響插件和項目的擴展性,增加項目的復雜度。Nop的模塊劃分很清晰,方便了今后學習和改造。

?

參考:

http://www.cnblogs.com/ljzforever/archive/2011/12/29/2305500.html

集成和配置AutoMapper


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 常德市| 昌图县| 义马市| 柏乡县| 长子县| 崇义县| 长寿区| 广东省| 蓝田县| 上思县| 嘉定区| 阳江市| 金华市| 皮山县| 厦门市| 迁西县| 江门市| 榆林市| 荣昌县| 石柱| 榆社县| 平乐县| 家居| 三门峡市| 师宗县| 隆尧县| 上栗县| 上饶市| 淳化县| 仁布县| 玛曲县| 岫岩| 嘉义县| 洛川县| 汝州市| 新兴县| 通州市| 务川| 太康县| 鄂尔多斯市| 永川市|