We mentioned ORM in previous articles, and then EF. Today we introduce a model of entity transformation relationship, AutoMapper.
I. What is it?
AutoMapper is an object mapping tool for. NET. The main function is to transform domain objects to anemia models and map database query results to entity objects.
What is anemia model? Anemia model (DTO, Data Transfer Object, that is to say, only contains attributes, can only store the necessary data, wood has any other redundant method data, dedicated to data transfer type objects).
What does that mean, for example, in ORM, a Model model interacting with a database is an entity with many attribute variable methods. When we interact with other systems (or other structures in the system), we do not want to pass this model directly to them for coupling or security or performance considerations (all in all), and then we create an anemia model to store and transfer data.
It is for this reason that we need to convert the Model model and DTO entities to each other.
2. How to use it
1. The attributes of two entities are exactly the same
We need to refer to it in vs before we apply it. Click on the library package manager under the tool and select the package management console! Then enter Install-Package AutoMapper. Or right-click on the solution, find AutoMapper in nuget management, install it, and then introduce AutoMapper, you can safely use it.
Model Entities
DTO Entities<span style="font-size:18px;">namespace testautomapper { //Student Source Entity Class public class studentSource { public string name { get; set; } public int age {get;set;} public string sex { get; set; } } }</span>
Single entity transformation process<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>
<span style="font-size:18px;"><span style="font-size:18px;">namespace testautomapper { class Program { static void Main(string[] args) { //Create mapping rules, 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 = "Cui Xiao Guang", age = 23, sex = "male" }; //Transform to get the target entity (DTO) var sPurpost = Mapper.Map<studentPurpost>(sSource); Console.WriteLine(sPurpost.name + sPurpost.sex + sPurpost.age); //Attributes of the output target entity } } </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 transformation relationship rule Mapper.CreateMap<studentSource, studentPurpost>(); //Create a collection of entities List<studentSource> list = new List<studentSource>(); studentSource s = new studentSource { name = "cuixiaoguang", age = 13, sex = "nan" }; list.Add(s); //Conversion List<studentPurpost> sPurpostList = Mapper.Map<List<studentSource>, List<studentPurpost>>(list); Console.WriteLine(sPurpostList[0].age + sPurpostList[0].name + sPurpostList[0].sex); } } }</span></span>
2. The attributes of two entities are not exactly the same
Model Entities
<span style="font-size:18px;">namespace testautomapper { //Student Source Entity Class public class studentSource { public string Sname { get; set; } public int age {get;set;} public string sex { get; set; } } }</span>
DTO Entities
<span style="font-size:18px;">namespace testautomapper { //Student Target Entity Class public class studentPurpost { public string Pname { get; set; } public string age { get; set; } public string sex { get; set; } } }</span>
Entity transformation (entity set is the same)
AutoMapper uses ForMember to specify mapping rules for each field:
Reference: Each custom member configuration uses an action delegate to configure each member.
Fortunately, there are powerful lambda expressions, and the definition of rules is simple and clear.
<span style="font-size:18px;"><span style="font-size:18px;">namespace testautomapper { class Program { static void Main(string[] args) { //Create mapping rules, the first parameter is the source entity (Model) and the second is the target entity (DTO) Mapper.CreateMap<studentSource, studentPurpost>(); //Define different attributes in an entity to correspond map.ForMember(d => d.Sname, opt => opt.MapFrom(s => s.Pname)); //Create a source entity studentSource sSource = new studentSource { Sname = "Cui Xiao Guang", age = 23, sex = "male" }; //Transform to get the target entity (DTO) var sPurpost = Mapper.Map<studentPurpost>(sSource); Console.WriteLine(sPurpost.name + sPurpost.sex + sPurpost.age); //Attributes of the output target entity } } </span>}</span>
3. The attributes of two entities are totally different
Model Entities
<span style="font-size:18px;">namespace testautomapper { //Student Source Entity Class public class studentSource { public string Sname { get; set; } public int Sage {get;set;} public string Ssex { get; set; } } }</span>
DTO Entities
<span style="font-size:18px;">namespace testautomapper { //Student Target Entity Class public class studentPurpost { public string Pname { get; set; } public string Page { get; set; } public string Psex { get; set; } } }</span>
Entity conversion
AutoMapper uses ConstructUsing to specify mapping rules for each field
<span style="font-size:18px;"><span style="font-size:18px;">namespace testautomapper { class Program { static void Main(string[] args) { //Create mapping rules, the first parameter is the source entity (Model) and the second is the target entity (DTO) Mapper.CreateMap<studentSource, studentPurpost>(); //Define different attributes in entities to correspond map.ConstructUsing(s => new studentPurpost { Pname= s.Sname, Page= s.Sage, Psex= s.Ssex }); //Create a source entity studentSource sSource = new studentSource { Sname = "Cui Xiao Guang", age = 23, sex = "male" }; //Transform to get the target entity (DTO) var sPurpost = Mapper.Map<studentPurpost>(sSource); Console.WriteLine(sPurpost.name + sPurpost.sex + sPurpost.age); //Attributes of the output target entity } } </span>}</span>
ForMember can also be used here, one by one, to correspond entity attributes, but it feels so cumbersome that I won't write it.
The original address of the article is http://www.mamicode.com/info-detail-507741.html.
Through this narration, we know that AutoMapper is an entity transformation model. It can transform two identical or different entities into each other, and can also transform two entity sets. Of course, AutoMapper's functions are far more than these, and its more functions need to be further understood. In fact, we do not need to use AutoMapper to transform in practical work, but things will become very simple after we use it, so we not only need to complete the task, but also to improve the efficiency of completing the task, which requires us to listen more, read more books, see more knowledge, so that we can make full use of existing knowledge and improve our quality of life.