1、简单入门使用介绍
2、实际应用
(1)添加文件并引入程序集
在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这哥们博客什么都写,大家看看