Oom frame AutoMapper Basic use (2)

Source: Internet
Author: User

For security reasons, in the background and the foreground of data transmission, often do not transfer the entity model directly, but the use of DTOs (data Transfer Object data transfer objects), so that in the background forward to pass the data can omit unnecessary information, only retain the necessary information, greatly enhance the data security.

Here are two relational models for each user, userdto

public class user{  Private Const int namemaxlength =;  Private Const int passwordmaxlength = +;  [Key]  Public long Id {get;}  [MaxLength (namemaxlength)]  public string Name {get; set;}  [MaxLength (passwordmaxlength)]  [DataType (Datatype.password)]  public string PassWord {get; set;}} public class userdto{  Private Const int namemaxlength =;  Private Const int passwordmaxlength = +;  [MaxLength (namemaxlength)]  public string Name {get; set;}  [MaxLength (passwordmaxlength)]  public string PassWord {get; set;}}

Here, the ID is defined as the self-growth primary key, in the registration page, this Id should not be visible, this time the benefits of using DTOs are reflected, this time, in the database will involve userdto to the User type conversion, according to previous experience, you can certainly follow the following to write:

User. Name=userdto.name;user. Password=userdto.password;

Such a conversion is possible, but if a User object is complex enough to have more than 10 or even more than 20 attributes, this will be awkward.

This time we can use AutoMapper to help us complete the userdto to User conversion.

Install the NuGet package first

In the Tools-nuget package manage-package Manage Console input

Install the appropriate NuGet package.

According to the help document given on Github, there are two ways to create a mapping, one that is static initalize and one that is created dynamically.

The following two different methods are used for unit testing

public void Using_initlalize_test ()  {    userdto dto = new Userdto    {      Name = "Niko",      PassWord = "1234", c5/>};    Mapper.initialize (CTX = CTX). Createmap<userdto, user> ());    User user = Mapper.map<userdto, user> (DTO);    User. Name.shouldbe ("Niko");    User. PASSWORD.SHOULDBE ("1234");    User. Id.tostring (). Shouldbe ("0");  }  public void Using_mapperconfiguration_test ()  {    var config = new Mapperconfiguration (CTX = CTX). Createmap<userdto, user> ());    var mapper = config. Createmapper ();    var mapper = new mapper (config);    Userdto dto = new Userdto    {      Name = "Niko",      PassWord = "1234",    };    User user = Mapper. Map<user> (DTO);    User user = mapper.map<user> (DTO);    User. Name.shouldbe ("Niko");    User. PASSWORD.SHOULDBE ("1234");    User. Id.tostring (). Shouldbe ("0");  }

Here you use the shouldly assertion framework, which references the official documentation.

The Assertconfigurationisvalid method is usually called after the rule is written, checking that the rule is complete

  Mapper.assertconfigurationisvalid ();

Both methods, unit tests are passed. In this case, using AutoMapper to handle complex object mappings will greatly simplify the amount of our code.

To make it easier to use automappper, extend the AutoMapper

public static class automapperextension{//<summary>//  object to Object///</summary>//  < Typeparam name= "T" ></typeparam>//  <param name= "obj" ></param>//  <returns> </returns> public  Static T-mapto<t> (This object obj)  {    if (obj = = null) return to default (    T); Mapper.initialize (Ctx=>ctx. Createmap (obj. GetType (), typeof (T));    return mapper.map<t> (obj);  <summary>///  collection to collection///  </summary>/  <typeparam name= "T" ></typeparam> //  <param name= "obj" ></param>///  <returns></returns> public  static List <T> mapto<t> (this IEnumerable obj)  {    if (obj = = null) throw new ArgumentNullException ();    Mapper.initialize (CTX = CTX). Createmap (obj. GetType (), typeof (T));    return mapper.map<list<t>> (obj);}  }

Use the above method for unit testing:

public void Testme ()  {    userdto dto = new Userdto    {      Name = "Niko",      PassWord = "1234",    };    User User=dto. Mapto<user> ();    User. Name.shouldbe ("Niko");    User. PASSWORD.SHOULDBE ("1234");    User. Id.tostring (). Shouldbe ("0");  }

Oom frame AutoMapper Basic use (2)

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.