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这哥们博客什么都写,你们看看

四、福利时间

相关文章
相关标签/搜索