Installation method: Use the VS bring-your-own NuGet management tool, search for AutoMapper, and choose the first one to install to your project.
I got some information from the Internet,
Reference URL: http://blog.csdn.net/csethcrm/article/details/52934325
Downloaded a demo, and then I wrote it again, I put the use of automapper into two kinds:
1, ViewModel and the field name of the entity is consistent, viewmodel field can be less than the field in the entity, but not many.
Another case is that the field name in the source entity is getxxx, then the corresponding field in the ViewModel can be xxx, and will automatically correspond to the assignment, such as I wrote the demo in the source entity in the Geta,viewmodel of A;
In another case, the entity assignment in the entity, in the example I wrote, the Entity class field contained in the source entity is a sub, which contains the fields named Age ,
Then the corresponding field name in the Destmodel can be: subage, then AutoMapper can be automatically assigned to you, you see the final result of the operation.
Let me show you the source entity I built:
Public classSource1 { Public stringName {Set;Get; } Public stringGeta {Set;Get; } Public stringgetd {Set;Get; } Public stringSETB {Set;Get; } Public stringCSet;Get; } PublicSubSource1 Sub {Set;Get; } } Public classSubSource1 { Public stringAge {Set;Get; } }
and ViewModel (to be transformed into the model you want):
Public classDest1 { Public stringName {Set;Get; } Public stringASet;Get; } Public stringCSet;Get; } Public stringSubage {Set;Get; } Public stringDSet;Get; } }
The extension method I encapsulated:
/// <summary> ///type mapping, default field name one by one corresponds/// </summary> /// <typeparam name= "Tdestination" >the model after conversion can be understood as ViewModel</typeparam> /// <typeparam name= "TSource" >entities to be transformed, entity</typeparam> /// <param name= "source" >You can use the type of this extension method, any reference type</param> /// <returns>the entity after conversion</returns> Public StaticTdestination Mapto<tdestination, Tsource> ( ThisTSource Source)whereTdestination:class whereTSource:class { if(Source = =NULL)return default(tdestination); varConfig =NewMapperconfiguration (cfg = cfg. Createmap<tdestination, tsource>()); varMapper =CONFIG. Createmapper (); returnMapper. Map<tdestination>(source); }
How to use:
varSource1 =NewSource1 {Name="Source", Sub=NewSubSource1 {age =" -"}, C="C", Geta="A", Setb="B" }; varDestviewmodel = Source1. Mapto<dest1, source1> ();
Operation Result:
2.viewmodel does not all correspond to the name of the entity field, only a few fields have the same name as the field name in the source entity, and the other fields are converted from several fields in the entity or by format or type.
How to use: You can no longer use this extension method, you can only write extra code, the code is as follows:
varConfig2 =Newmapperconfiguration (CFG= cfg. Createmap<sourceuser, destuser2>() . Formember (d= = D.destname, opt + = opt. Mapfrom (s = s.name))//Specify field one by one to correspond. Formember (d = d.birthday, opt = = opt. Mapfrom (src = src. Birthday.tostring ("YY-MM-DD hh:mm")))//Specify the field and convert the specified format. Formember (d = d.age, opt = = opt. Condition (src = src. Age >5))//Conditional Assignment. Formember (d = d.a1, opt = = opt. Ignore ())//ignore the field and do not assign a value to the field. Formember (d = d.a1, opt = = opt. Nullsubstitute ("Default Value"))//If the source field value is empty, the value is assigned to the default value. Formember (d = d.a1, opt = = opt. Mapfrom (src = src. Name + src. Age *3+ src. Birthday.tostring ("D"))));//you can combine assignments at your own discretion . varMapper2 = Config2. Createmapper ();
The comments contain a few things that are usually used, and I don't write any more.
The following is a list of extension methods to convert the code to paste:
/// <summary> ///Collection list type mappings, default field name one by one corresponds/// </summary> /// <typeparam name= "Tdestination" >the model after conversion can be understood as ViewModel</typeparam> /// <typeparam name= "TSource" >entities to be transformed, entity</typeparam> /// <param name= "source" >You can use the type of this extension method, any reference type</param> /// <returns>List of entities after conversion</returns> Public StaticIenumerable<tdestination> Maptolist<tdestination, Tsource> ( ThisIenumerable<tsource>source)whereTdestination:class whereTSource:class { if(Source = =NULL)return NewList<tdestination>(); varConfig =NewMapperconfiguration (cfg = cfg. Createmap<tdestination, tsource>()); varMapper =CONFIG. Createmapper (); returnMapper. Map<list<tdestination>>(source); }
The same way of use:
varSource1 =NewSource1 {Name="Source", Sub=NewSubSource1 {age =" -"}, C="C", Geta="A", Setb="B" };varSource3 =NewSource1 {Name="Source3", Sub=NewSubSource1 {age ="253"}, C="C3", Geta="A3", Setb="B3" }; varSourceList =NewList<source1>{source1, source3}; varDestviewmodellist = Sourcelist.maptolist<dest1, source1> ();
Operation Result:
The above is my personal income, if there are errors, please correct me.
How to use C # AutoMapper