AutoMapper

一、簡單入門使用介紹

二、實際應用

(1)添加文件並引入程序集
html

在App_Start中添加配置文件c#

(2)配置
架構

在這裏將全部的配置都配置在這裏,包含ui-bll-dao各層之間的轉換

namespace PCITC.MES.EAM.UI.App_Start
{
    public class MapperConfig
    {
        public static void RegisterMappers()
        {
            Mapper.Reset();
            Mapper.Initialize(cfg =>
            {
                cfg.AddProfile(new ViewModelToEntityProfile());
                cfg.AddProfile(new EntityToViewModelProfile());
                cfg.AddProfile(new EntityToPocoProfile());
                cfg.AddProfile(new PocoToEntityProfile());
            });
        }
    }
    
    public class ViewModelToEntityProfile : Profile
    {
        protected override void Configure()
        {
            #region 報表管理-開停機記錄
            Mapper.CreateMap<Areas.ReportForms.Models.StopRecording, Bll.Entities.StopRecording>();
            Mapper.CreateMap<Areas.ReportForms.Models.StopRecordingQueryParameter, Bll.Entities.StopRecordingQueryParameter>();
            #endregion 
        }
    }

    public class EntityToViewModelProfile : Profile
    {
        protected override void Configure()
        {
            #region 報表管理-開停機記錄
            Mapper.CreateMap<Bll.Entities.StopRecording, Areas.ReportForms.Models.StopRecording>();
            #endregion 
        }
    }

    public class EntityToPocoProfile : Profile
    {
        protected override void Configure()
        {
            #region 報表管理-開停機記錄
            Mapper.CreateMap<Bll.Entities.StopRecording, Poco.ReportForms.StopRecording>();
            Mapper.CreateMap<Bll.Entities.StopRecordingQueryParameter, DAL.ReportForms.StopRecordingQueryParameter>();
            #endregion 
        }
    }

    public class PocoToEntityProfile : Profile
    {
        protected override void Configure()
        {
            #region 維修管理-類別參數文件關係配置
            Mapper.CreateMap<Poco.OperManagement.ParaFileRelationship, Bll.Entities.ParaFileRelationship>()
                .ForMember(dto => dto.CatalogName, conf => conf.MapFrom(ol => ol.OperFailureObjCatalog.FailureObjName))
                .ForMember(dto => dto.CatalogCode, conf => conf.MapFrom(ol => ol.OperFailureObjCatalog.FailureObjCode))
                .ForMember(dto => dto.GroupName, conf => conf.MapFrom(ol => ol.OperFailureObjGroup.FailureObjName))
                .ForMember(dto => dto.GroupCode, conf => conf.MapFrom(ol => ol.OperFailureObjGroup.FailureObjCode))
                .ForMember(dto => dto.Code, conf => conf.MapFrom(ol => ol.OperFailureObjCode.FailureObjCode))
                .ForMember(dto => dto.CodeName, conf => conf.MapFrom(ol => ol.OperFailureObjCode.FailureObjName));
            #endregion

            #region 報表管理-開停機記錄
            Mapper.CreateMap<Poco.ReportForms.StopRecording, Bll.Entities.StopRecording>();
            #endregion 
        }        
    }
}

        注意1Mapper.Initialize在項目中只能用一次,不然會把全部配置覆蓋。 點此有參考app

        注意2:IDE有些時候不能識別配置的東西,底下會有紅線等,但那只是IDE的問題,編譯不會有問題。ide

        注意3方法中的參數方向問題學習

        注意4:無論哪一種架構的各個層都是要在這裏配置映射,因此別嫌麻煩,寫全了程序集引用,比                                                       如:Poco.ReportForms.StopRecording,Bll.Entities.StopRecordingui

(3)在Global中啓動
spa

namespace PCITC.MES.EAM.Wcf
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            MapperConfig.RegisterMappers();
        }
    }
}

(4)建立轉換方法中的使用code

using System.Collections.Generic;
using AutoMapper;

namespace PCITC.MES.EAM.Bll.Entities
{
    public static class SealedManagementEntityBuilder
    {

        /// <summary>
        /// pocoModel轉bllModel
        /// </summary>
        /// <param name="pocoModel"></param>
        /// <returns></returns>
        public static SealedManagement BuildSealedManagementToBllModel(Poco.ProfessionalManagement.SealedManagement pocoModel)
        {
            // 驗證類型映射是否正確
            //Mapper.AssertConfigurationIsValid();
            return Mapper.Map<Poco.ProfessionalManagement.SealedManagement, SealedManagement>(pocoModel);
        }

        /// <summary>
        /// bllModel轉pocoModel
        /// </summary>
        /// <param name="bllModel"></param>
        /// <returns></returns>
        public static Poco.ProfessionalManagement.SealedManagement BuildSealedManagementToPocoModel(SealedManagement bllModel)
        {
            return Mapper.Map<SealedManagement, Poco.ProfessionalManagement.SealedManagement>(bllModel);
        }

        /// <summary>
        /// pocoModels轉bllModels
        /// </summary>
        /// <param name="pocoModels"></param>
        /// <returns></returns>
        public static IList<SealedManagement> BuildSealedManagementToBllModelManyToMany(IList<Poco.ProfessionalManagement.SealedManagement> pocoModels)
        {
            return Mapper.Map<IList<Poco.ProfessionalManagement.SealedManagement>, IList<SealedManagement>>(pocoModels);
        }

        /// <summary>
        /// bllModels轉pocoModels
        /// </summary>
        /// <param name="bllModels"></param>
        /// <returns></returns>
        public static IList<Poco.ProfessionalManagement.SealedManagement> BuildSealedManagementToPocoModelManyToMany(IList<SealedManagement> bllModels)
        {
            return Mapper.Map<IList<SealedManagement>, IList<Poco.ProfessionalManagement.SealedManagement>>(bllModels);
        }
    }
}

三、推薦資料

        固然automapper還有不少東西,這個須要你們去官網學習了。
orm

        http://www.tuicool.com/articles/qq2q6fA

        http://www.cnblogs.com/xishuai/category/577114.html

http://www.cnblogs.com/happyframework/archive/2013/06/06/3120805.html這哥們博客什麼都寫,你們看看

四、福利時間

相關文章
相關標籤/搜索