Introduction to the use of automapper in Oom

Source: Internet
Author: User
This article mainly introduces the automapper of the Oom framework, the five examples can help you solve common basic problems. Have a certain reference value, follow the small series below to see it

Write in front

Oom, as the name implies, object-object-mapping entities to convert each other, AutoMapper is also a cliché, its meaning is to help you do not have to manually convert simple and troublesome inter-entity relationships, such as ViewModel and entity conversion, Searchmodel and entity conversion, my share of the meaning is that most of the online sharing is a few years ago, a lot of methods have been discarded, to the compiler will tell you that the method is outdated, obsolete, not recommended to use, For example, Mapper.createmap and other methods, of course, the old driver most directly to GitHub to read the document, or Google a bit to understand, but the Chinese material on the method of abandonment, and there is no explanation. The five examples in this article can help you solve common basic problems.

Prepare

First we prepare some viewmodel and tmodel. ViewModel is the entity in which you interact with the user. TModel is the entity that you deal with the database.

The entity shows as follows:

TModel has the following three simple entities, which have separate entities and one-to-many entities.

public class Taddress{public string Country {get, set,} public string City {get; set;} public string Street {get; SE T public string Postcode {GET, set,} public string Createtime {get; set;} public int Createuserid {get; set;}}
public class Tauthor {public  string Name {get; set;}  public string Description {get; set;}  Public list<tcontactinfo> ContactInfo {get; set;}}  public class Tcontactinfo {public int Id {GET, set,} public string e-mail {get; set;} public string Blog {get; set;} public string Twitter {get; set;}}

ViewModel of the following three:

public class Vm_address {public string Country {get, set,} public string City {get; set;} public string City2 {get; Set }} public class Vm_author {public string Name {get, set;} public string Description {get; set;} Public List<vm_co Ntactinfo> ContactInfo {get; set;}}  public class Vm_contactinfo {public int Id {GET, set,} public string e-mail {get; set;} public string Blog {get; set; public string Twitter {get; set;}}

Single Entity conversions

When a single entity is converted, when the attribute field name exactly matches, you only need to specify a conversion rule between two entities, specifying the source and destination target entities. Then you should refer to the following example:

Vm_address dto = new vm_address  {  country = ' China ', city  = ' Beijing '  };  Mapper.initialize (M = m.createmap<vm_address, taddress> ());  Taddress address = mapper.map<vm_address, taddress> (DTO);

Please note that in automapper5.x, initialize to initialize your rules is preferred.

After you specify the conversion rules, use the Map method to convert and output your target entities. There is also the first parameter that represents Sourcemodel, and the second parameter is Destinationmodel.

Single entity different name attribute conversions

When you need to map the fields of different names, be aware that using the Formember method, the first parameter requires you to set the Target field for the special configuration you want, the second parameter you need to set your action on the property of the field, I chose the Mapfrom method it provides, The point is to tell AutoMapper that I need to say that the city source of the target entity is specified as the City2 property value of the source entity.

Vm_address dto = new vm_address  {  country = "China",  City2 = "Beijing"  };  Mapper.initialize (M = m.createmap<vm_address, taddress> (). Formember (x = x.city, opt = = opt.) Mapfrom (o = o.city2));  Taddress address = mapper.map<vm_address, taddress> (DTO);

Collection conversions

When converting between collections, you do not need to configure a match between the target list and the source list object, only the mapping matching relationships of your generic objects need to be configured.

  Taddress address = new Taddress {country = ' China ', city = ' Beijing '};  Taddress address2 = new Taddress () {country = "USA", city = "New York"};  list<taddress> AddressList = new list<taddress> () {address2, address};  Mapper.initialize (M = m.createmap<taddress, vm_address> ());//Here you only need to configure transformations between entities, not transformations of entity collections  List<vm_ address> res = Mapper.map<list<taddress>, list<vm_address>> (AddressList);

Entities contain different types of property conversions (ignore attributes)

When an entity contains different types of attributes, such as TModel1 contains a list<tmodel>, and your ViewModel1 contains a list<viewmodel> this time you can choose to ignore this property

var contacts = new List<tcontactinfo> () {new Tcontactinfo () {Blog = "MyBlog", Email = "ws@qq.com"}, New Tcontactinfo () {Blog = "MyBlog", Email = "ll@qq.com"}};  Tauthor author = new Tauthor () {Description = "description", Name = "Wu Di", ContactInfo = contacts};  Mapper.initialize (m = {m.createmap<tauthor, vm_author> (). Formember (x = x.contactinfo, opt = = opt.) Ignore ());       }); Vm_author dto = Mapper.map<tauthor, vm_author> (Author);//Here the Ignore represents the configuration contractinfo the operation of this property is ignored ignore, the property is ignored when mapping Since List<tcontactinfo> () and list<vm_contactinfo> () are different types, it is necessary to configure the Ignore or special mappings, the special mapping example to see below

Entities contain different types of property conversions (Specify attribute Mapfrom)

Of course you need this attribute, you can not ignore him, but use Mapfrom to make special designation, and when the type is not the same, you have to specify the mapping between the two types of matching relationship. As in the following example

M.createmap<tcontactinfo, vm_contactinfo> ();
M.createmap<tauthor, Vm_author> (). Formember (x = x.contactinfo, opt = = opt.) Mapfrom (o = o.contactinfo));

var contacts = new List<tcontactinfo> ()  {  new Tcontactinfo () {Blog = "MyBlog", Email = "ws@qq.com"},  N EW Tcontactinfo () {Blog = "MyBlog", Email = "ll@qq.com"}  };  Tauthor author = new Tauthor () {Description = "description", Name = "Wu Di", ContactInfo = contacts};  Mapper.initialize (M + =  {  m.createmap<tcontactinfo, vm_contactinfo> ();//Note the necessary m for internal conversion of different types of entities  . Createmap<tauthor, Vm_author> (). Formember (x = x.contactinfo, opt = = opt.) Mapfrom (o = o.contactinfo));//Pay attention to making mapfrom is necessary  });  Vm_author dto = Mapper.map<tauthor, vm_author> (Author);

"Recommended"

1. asp free Video Tutorial

2. asp Tutorials

3. Eon the ASP Basic video tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.