AutoMapper

1、简单入门使用介绍

234653_ibwo_1416844.jpg

234654_6fNO_1416844.jpg

234655_18TE_1416844.jpg

234655_9SId_1416844.jpg

234656_gI7G_1416844.jpg

234657_B6Lr_1416844.jpg

234658_Tjah_1416844.jpg

2、实际应用

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

235449_Pz7F_1416844.jpg在App_Start中添加配置文件

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

        注意1:Mapper.Initialize在项目中只能用一次,否则会把所有配置覆盖。 点此有参考

        注意2:IDE有些时候不能识别配置的东西,底下会有红线等,但那只是IDE的问题,编译不会有问题。

        注意3方法中的参数方向问题

        注意4:不管哪种架构的各个层都是要在这里配置映射,所以别嫌麻烦,写全了程序集引用,比                                                       如:Poco.ReportForms.StopRecording,Bll.Entities.StopRecording

(3)在Global中启动

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

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

(4)创建转换方法中的使用

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

3、推荐资料

        当然automapper还有很多东西,这个需要大家去官网学习了。

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

4、福利时间

004256_7hIi_1416844.jpg

004257_eMBE_1416844.jpg

004258_oYbE_1416844.jpg

004259_kosh_1416844.jpg






转载于:https://my.oschina.net/u/1416844/blog/278999

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值