好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解c# AutoMapper 使用方式

安装方式:使用vs自带的nuget管理工具,搜索automapper ,选择第一个安装到你的项目即可。

先说说dto

dto是个什么东东?

dto(data transfer object)就是数据传输对象,说白了就是一个对象,只不过里边全是数据而已。

为什么要用dto?

1、dto更注重数据,对领域对象进行合理封装,从而不会将领域对象的行为过分暴露给表现层

2、dto是面向ui的需求而设计的,而领域模型是面向业务而设计的。因此dto更适合于和表现层的交互,通过dto我们实现了表现层与领域model之间的解耦,因此改动领域model不会影响ui层

3、dto说白了就是数据而已,不包含任何的业务逻辑,属于瘦身型的对象,使用时可以根据不同的ui需求进行灵活的运用

我从网上找了一些资料,

automapper

现在我们既然知道了使用dto的好处,那么我们肯定也想马上使用它,但是这里会牵扯一个问题:怎样实现dto和领域model之间的转换?

有两个思路,我们要么自己写转换代码,要么使用工具。不过就应用而言,我还是觉得用工具比较简单快捷,那就使用工具吧。其实这样的转换工具很多,不过我还是决定使用automapper,因为它足够轻量级,而且也非常流行,国外的大牛们都使用它。使用automapper可以很方便的实现dto和领域model之间的转换,它是一个强大的object-object mapping工具。

下载了个demo,然后自己又写了一遍,我把automapper 的使用分为两种:

1、viewmodel与实体的字段名字是一致的,viewmodel的字段可以比实体中的字段少,但是不能多。

还有一种情况是:源实体中的字段名字是getxxx,那么viewmodel中对应的字段可以是xxx,也会自动对应赋值,比如我写的demo中源实体中geta,viewmodel中的a;

再有一种情况就是实体中的实体赋值,在我写的这个例子中,源实体中包含的实体类字段为sub,里面包含的字段名字为age,

那么destmodel中对应的字段名字可以是:subage,那么automapper就可以自动为你赋值了,大家看最后的运行结果。

给大家看下我建的源实体:

?

public class source1

{

   public string name { set ; get ; }

 

   public string geta { set ; get ; }

   public string getd { set ; get ; }

 

 

   public string setb { set ; get ; }

 

   public string c { set ; get ; }

 

   public subsource1 sub { set ; get ; }

}

 

 

public class subsource1

{

   public string age { set ; get ; }

 

}

还有viewmodel(要转化成为你想要的模型):

?

public class dest1

{

   public string name { set ; get ; }

 

   public string a { set ; get ; }

 

   public string c { set ; get ; }

 

   public string subage { set ; get ; }

 

   public string d { set ; get ; }

}

我封装的扩展方法:

?

/// <summary>

/// 类型映射,默认字段名字一一对应

/// </summary>

/// <typeparam name="tdestination">转化之后的model,可以理解为viewmodel</typeparam>

/// <typeparam name="tsource">要被转化的实体,entity</typeparam>

/// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param>

/// <returns>转化之后的实体</returns>

public static tdestination mapto<tdestination, tsource>( this tsource source)

   where tdestination: class

   where tsource : class

{

   if (source == null ) return default (tdestination);

   var config = new mapperconfiguration(cfg => cfg.createmap<tdestination, tsource>());

   var mapper = config.createmapper();

   return mapper.map<tdestination>(source);

}

使用方式:

?

var source1 = new source1

{

   name = "source" ,

   sub = new subsource1 { age = "25" },

   c = "c" ,

   geta = "a" ,

   setb = "b"

};

 

var destviewmodel = source1.mapto<dest1, source1>();

运行结果:

2.viewmodel与实体字段名字没有全部对应,只有几个字段的名字和源实体中的字段名字是一样的,其他的字段是通过实体中的几个字段组合或者是格式或者是类型转化而来的,

使用方法:不能再使用这个扩展方法了,只能自己额外写代码,代码如下:

?

var config2 = new mapperconfiguration(

   cfg => cfg.createmap<sourceuser, destuser2>()

     .formember(d => d.destname, opt => opt.mapfrom(s => s.name))  //指定字段一一对应

     .formember(d => d.birthday, opt => opt.mapfrom(src => src.birthday.tostring( "yy-mm-dd hh:mm" ))) //指定字段,并转化指定的格式

     .formember(d => d.age, opt => opt.condition(src => src.age > 5)) //条件赋值

     .formember(d => d.a1, opt => opt.ignore()) //忽略该字段,不给该字段赋值

     .formember(d => d.a1, opt => opt.nullsubstitute( "default value" )) //如果源字段值为空,则赋值为 default value

     .formember(d => d.a1, opt => opt.mapfrom(src => src.name + src.age * 3 + src.birthday.tostring( "d" )))); //可以自己随意组合赋值

var mapper2 = config2.createmapper();

注释中都包含了平时常用的几种情况,其他的我就没有再写。

下面再给大家把list转化的扩展方法代码贴上:

?

/// <summary>

/// 集合列表类型映射,默认字段名字一一对应

/// </summary>

/// <typeparam name="tdestination">转化之后的model,可以理解为viewmodel</typeparam>

/// <typeparam name="tsource">要被转化的实体,entity</typeparam>

/// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param>

/// <returns>转化之后的实体列表</returns>

public static ienumerable<tdestination> maptolist<tdestination, tsource>( this ienumerable<tsource> source)

   where tdestination : class

   where tsource : class

{

   if (source == null ) return new list<tdestination>();

   var config = new mapperconfiguration(cfg => cfg.createmap<tdestination, tsource>());

   var mapper = config.createmapper();

   return mapper.map<list<tdestination>>(source);

}

同样的使用方式:

?

         var source1 = new source1

         {

           name = "source" ,

           sub = new subsource1 { age = "25" },

           c = "c" ,

           geta = "a" ,

           setb = "b"

         };

var source3 = new source1

         {

           name = "source3" ,

           sub = new subsource1 { age = "253" },

           c = "c3" ,

           geta = "a3" ,

           setb = "b3"

         };

         var sourcelist = new list<source1> { source1, source3 };

         var destviewmodellist = sourcelist.maptolist<dest1, source1>();

运行结果:

 以上就是我个人所得,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://HdhCmsTestcnblogs测试数据/dawenyang/p/7966850.html

dy("nrwz");

查看更多关于详解c# AutoMapper 使用方式的详细内容...

  阅读:47次