We mentioned the ORM in the previous article, and then we used EF, and today we are going to introduce a model automapper of the real-body transformation relationship.
first, what is
AutoMapper is a. NET object Mapping tool . The main role is to perform the transformation between the domain object and the anemia model , and the database query results are mapped to the entity object.
What is an anemic model? anemia model (Dto,data Transfer object, which means only the attributes, only the necessary data, Wood has any other redundant method data, specifically for data Transfer type Object ).
So what does that mean, for example, in ORM, model models that interact with databases are entities with many attribute variable methods. And when we interact with other systems (or other structures in the system) for coupling considerations or security considerations or performance considerations (in short, all sorts of considerations), we don't want to pass the model models directly to them, and we create a The anemia model holds the data and passes it.
That's why we need to convert between model models and DTO entities.
second, how to use 1, two entities have exactly the same properties
Before we apply it, we'll refer to it in vs. Click Library Package Manager under Tools to select the Package management Console! then enter Install-packageautomapper. or right-click the solution, find AutoMapper in NuGet management, install, and then in the introduction of AutoMapper, you can safely use the
Model Entity
<span style= "FONT-SIZE:18PX;" >namespace testautomapper { //Student Source entity classes public class Studentsource {public string name {get; set ; } public int age {get;set;} public string Sex {get; set;} } } </span>
dto entity
<span style= "FONT-SIZE:18PX;" >namespace testautomapper { ///Student Target entity class public class Studentpurpost {public string name { Get Set Public string Age {get; set;} public string Sex {get; set;} } } </span>
Single Entity conversion process
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >namespace testautomapper { class program { static void Main (string[] args) { //create mapping rule , the first parameter is the source entity (Model), and the second is the target entity (DTO) Mapper.createmap<studentsource, studentpurpost> (); Create a source entity studentsource ssource = new Studentsource { name = "Tri Xiaokuang", age = all, sex = "Male" };< c15/>//is converted to get the target entity (DTO) var spurpost = mapper.map<studentpurpost> (ssource); Console.WriteLine (spurpost.name + spurpost.sex + spurpost.age); Output target entity's properties } } </span>}</span>
multiple entity conversion processes (entity sets)
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >namespace testautomapper { class program { static void Main (string[] args) { // Create a transform relationship rule Mapper.createmap<studentsource, studentpurpost> (); Create an Entity collection list<studentsource> List = new list<studentsource> (); Studentsource s = new Studentsource { name = "Cuixiaoguang", age =, sex = "Nan" }; List. ADD (s); Convert list<studentpurpost> spurpostlist = Mapper.map<list<studentsource>, List< Studentpurpost>> (list); Console.WriteLine (spurpostlist[0].age + spurpostlist[0].name + spurpostlist[0].sex);}}} </span></span>
2, two entity properties are not exactly the same
model entity
<span style= "FONT-SIZE:18PX;" >namespace testautomapper { //Student Source entity class public classes studentsource {public string Sname { Get Set } public int age {get;set;} public string Sex {get; set;} }
dto entity
<span style= "FONT-SIZE:18PX;" >namespace testautomapper { //Student Target entity class public classes studentpurpost {public string Pname {get; set;} public string Age {get; set;} public string Sex {get; set;} } } </span>
entity Transformations (same as entity collections)
AutoMapper uses Formember to specify a mapping rule for each field:
References: The each custom member configuration uses a action delegate to configure each member.
Fortunately there is a powerful lambda expression, and the definition of the rule is straightforward.
< Span style= "font-size:18px" >
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >namespace testautomapper { class program { static void Main (string[] args) { //create mapping rule , the first parameter is the source entity (Model), and the second is the target entity (DTO) Mapper.createmap<studentsource, studentpurpost> (); Define the different attributes in the entity so that they correspond to the map. Formember (d = d.sname, opt = = opt. Mapfrom (s = s.pname)); Create a source entity studentsource ssource = new Studentsource { Sname = "Tri Xiaokuang", age = all, sex = "Male" } ; Convert to get target entity (DTO) var spurpost = mapper.map<studentpurpost> (ssource); Console.WriteLine (spurpost.name + spurpost.sex + spurpost.age); Output target entity's properties } }
3, two entities have a different property
model entity
<span style= "FONT-SIZE:18PX;" >namespace testautomapper { //Student Source entity classes public class Studentsource {public string Sname {get; Set } public int Sage {get;set;} public string Ssex {get; set;} }
dto entity
<span style= "FONT-SIZE:18PX;" >namespace testautomapper { //Student Target entity class public classes studentpurpost {public string Pname { Get Set Public string Page {get; set;} public string Psex {get; set;} } } </span>
entity conversions
AutoMapper using constructusing to specify a mapping rule for each field
< Span style= "font-size:18px" >
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >namespace Testautomapper {class Program {static void Main (string[] args) { Create a mapping rule, the first parameter is the source entity (Model), the second is the target entity (DTO) Mapper.createmap<studentsource, studentpurpost> (); Define all the different attributes in the entity so that they correspond to the map. Constructusing (s = = new Studentpurpost { Pname= S.sname, page= s.sage, Psex= s.ssex}); Create a source entity studentsource ssource = new Studentsource {Sname = "Tri Xiaokuang", Age = all, sex = "male"}; Convert to get target entity (DTO) var spurpost = mapper.map<studentpurpost> (ssource); Console.writeliNE (spurpost.name + spurpost.sex + spurpost.age); Output target entity's properties}} </span>}</span>
This can also be usedFormember, each of the entity attributes corresponding, but it feels so troublesome, so I do not write
Small message: Through this story, we know that AutoMapper is the entity conversion model, he can convert two identical or different entities to each other, but also can transform two entity sets, of course, automapper function far more than these, Its more features need further understanding. In fact, we do not need to use AutoMapper to transform the actual work, but we use the things will become very simple, so we not only to complete the task, but also to improve the efficiency of the task, which requires us to listen to more and more read, well-informed, In this way, we can make full use of existing knowledge and improve our quality of life.
The bridge of the Automapper--map entity (ii)