首页 技术 正文
技术 2022年11月6日
0 收藏 633 点赞 282 浏览 2272 个字

最近发现了一个比较有趣的东西 AutoMapper,主要将Model转换为DTO,DTO更注重数据,对领域对象进行合理封装,从而不会将领域对象的行为过分暴露给表现层。

先来看一点实例,两个类之间的映射。

首先定义两个类Source与DTOSource:

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

 1  public class Source
2 {
3 public int Id { get; set; }
4 public string Content { get; set; }
5 }
6
7 public class DTOSource
8 {
9 public int Id { get; set; }
10 public string Content { get; set; }
11 }

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

Source与DTOSource字段完全相同,来看看它俩如何通过AutoMapper转换,代码很简单。

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

1 Mapper.Initialize(x=>{
2 x.CreateMap<Source,DTOSource>();
3 });
4
5 Source s = new Source{Id=1,Content="123"};
6
7 DTOSource dto = Mapper.Map<DTOSource>(s);

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

第一步建立Source到DTOSource之间的映射,初始化一个Source实例后,来看下执行结果:

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

执行完成后,可以看到dto中的数据与之前初始化的s的数据是一样的,就像是直接将s拷贝了一份给dto,在两个类字段名定全相同的情况下如此,那么如果DTOSource中的字段名与Source中的不相同如何,其实也很简单,只需

要改成一点点的代码既可:

我们将DTOSource中的Content的字段名改成Desc,此时只需要建立映射关系时,指定字段就可以了:

1 Mapper.Initialize(x => {
2 x.CreateMap<Source, DTOSource>().ForMember(c=>c.Desc,q=> {
3 q.MapFrom(z => z.Content);
4 });
5 });

来看看运行结果如何;

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

可以看到与之前的运行结果是相同的。

那么如何映射两个List,其实也很简单,和上述代码几乎可以说是无差别,只是在最后一步时,要做一点点的修改就可以了。如下面代码:

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

 1  Mapper.Initialize(x => {
2 x.CreateMap<Source, DTOSource>().ForMember(c => c.Desc, q =>
3 {
4 q.MapFrom(z => z.Content);
5 });
6 });
7
8 s.Add(new Source { Id = 1, Content = "123" });
9
10 var dto = Mapper.Map<List<DTOSource>>(s);

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

可以看到除了最后一句代码,其它几乎是完全相同的,只是在最后一句代码中,目标类型改成了List<DTOSource>仅此而已。看下运行结果如何:

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

结果符合预期。

在实际的项目中,这样的写法肯定是不符合要求的,一般会做一个封装,新建一个SourceProfile继承自Profile:

1  public SourceProfile()
2 {
3 base.CreateMap<Source, DTOSource>().ForMember(c => c.Desc, q => {
4 q.MapFrom(z => z.Content);
5 });
6 }

所有映射关系都可以写在这一个类里,只需要在程序初始化的时候调用一次就可以了:

1 Mapper.Initialize(x => {  x.AddProfile<SourceProfile>();  });

博主使用的AutoMapper版本6.1.1.0,因为AutoMapper在6.0版本时移除了Profile中的Configure,所以与6.0版本以下写法有点不同,6.0以下版本写法为:

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

1 public class SourceProfile : Profile
2 {
3 protected override void Configure()
4 {
5 CreateMap<Source, DTOSource>().ForMember(c => c.Desc, q => {
6 q.MapFrom(z => z.Content);
7 });
8 }
9 }

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

继承Profile重写其Configure即可,调用方式与上述没有太大差别。 Mapper.Initialize中可添加一个或多个Profile。

在MVC项目的应用中,可以将Mapper.Initialize封装到一个类里;

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

 public static class AutoMapperForMvc
{
public static void Register()
{
Mapper.Initialize(x => {
x.AddProfile<SourceProfile>();
});
} }

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

进而在MVC的Global中进一次性注册:

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

 1 public class MvcApplication : System.Web.HttpApplication
2 {
3 protected void Application_Start()
4 {
5 AreaRegistration.RegisterAllAreas();
6 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
7 RouteConfig.RegisterRoutes(RouteTable.Routes);
8 BundleConfig.RegisterBundles(BundleTable.Bundles);
9 //注册
10 AutoMapperForMvc.Register();
11 }
12 }

转载 AutoMapper在C#中的有趣应用  https://www.cnblogs.com/lvlinlv/p/7344916.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287