Free conversion of DTOs and model using AutoMapper (UP)

Source: Internet
Author: User
In actual software development projects, our "business logic" often requires us to transform the same data. For example, a web app collects the user's input from the front end as a dto, then transforms the DTO into a domain model and persists into the database. On the other hand, when the user requests the data, we need to do the opposite: the domain model that is queried from the database is converted to a dto in the opposite way and presented to the user. There are times when we are faced with more data usage requirements, such as clients with multiple data usage, and each client has its own different requirements for data structures, which also requires us to do more conversion.
Frequent data conversions are trivial and messy, and many times we have to:
(1) in the two types almost only the name is different and the structure is broadly similar, but can only be manually, attribute-by-property assignment in a way to achieve the "transfer" of data between types.
(2) Each encounter a new data conversion scene to manually implement a set of conversion logic, resulting in data conversion operations are repeated and dispersed to the various corners of the application.
If there is such a "Transformers" tool that turns "oranges" Into the "apples" we want, all we need to do is define the conversion rules-to do our real business logic, or even in a simple scenario where even rules don't need to be defined (Convention over Configuration), it would be a very good thing. In fact, in. NET we don't have to reinvent the wheel, because we have--automapper, a powerful object-object mapping tool.
Well, I admit that I'm a little bit excited, in fact the project I'm working on is going through the "puzzle", and automapper really gives me a bright feeling. So I took a little weekend off. I tried a little bit of automapper and realized that the "strong atmosphere field" of the DTO to the domain model was achieved by small application scenarios. I will share my experience in the article and hope to bring you a little help in the same confusion. Complete project Code I'll post it to my Git repository later, and you're welcome to use it for free.
"One" Application scenario description
Let's take a look at my "virtual" domain model. This time I defined a bookstore (bookstore):

C # code

public class Bookstore

{

public string Name {get; set;}

Public list<book> Books {get; set;}

Public address address {get; set;}

}


The bookstore has its own address:

C # code

public class Address

{

public string Country {get; set;}

public string City {get; set;}

public string Street {get; set;}

public string postcode {get; set;}

}


At the same time, the bookstore put N book:

C # code

public class Book

{

public string Title {get; set;}

public string Description {get; set;}

public string Language {get; set;}

Public decimal price {get; set;}

Public list<author> Authors {get; set;}

Public DateTime? publishdate {get; set;}

Public publisher publisher {get; set;}

public int? paperback {get; set;}

}


Each book has publisher information (publisher):

C # code

public class Publisher

{

public string Name {get; set;}

}


Each book can have up to 2 authors ' information (Author):

C # code

public class Author

{

public string Name {get; set;}

public string Description {get; set;}

Public ContactInfo ContactInfo {get; set;}

}


Each author has his own contact information (ContactInfo):

C # code

public class ContactInfo

{

public string Email {get; set;}

public string Blog {get; set;}

public string Twitter {get; set;}

}


That's almost it, a domain model with hierarchical structure.
Take a look at our DTO structure.
In the DTO we have the bookstoredto corresponding to the bookstore:

C # code

public class Bookstoredto

{

public string Name {get; set;}

Public list<bookdto> Books {get; set;}

Public addressdto Address {get; set;}

}


It contains the addressdto corresponding to address:

C # code

public class Addressdto

{

public string Country {get; set;}

public string City {get; set;}

public string Street {get; set;}

public string postcode {get; set;}

}


and the bookdto corresponding to the book:

C # code

public class Bookdto

{

public string Title {get; set;}

public string Description {get; set;}

public string Language {get; set;}

Public decimal price {get; set;}

Public DateTime? publishdate {get; set;}

public string Publisher {get; set;}

public int? paperback {get; set;}

public string Firstauthorname {get; set;}

public string Firstauthordescription {get; set;}

public string Firstauthoremail {get; set;}

public string Firstauthorblog {get; set;}

public string Firstauthortwitter {get; set;}

public string Secondauthorname {get; set;}

public string Secondauthordescription {get; set;}

public string Secondauthoremail {get; set;}

public string Secondauthorblog {get; set;}

public string Secondauthortwitter {get; set;}

}


Notice that our bookdto "leveled" the entire book hierarchy, with a bookdto carrying all of the data in all of the schema, such as author and publisher.
Just let's take a look at the map rules for DTOs to model.
(1) Bookstoredto-Bookstore

Fields in field Bookstore in Bookstoredto

Name Name

Books Books

Address Address


(2) Address addressdto

fields in the address of a field in Addressdto

Country Country

City City

Street Street

Postcode postcode


(3) Bookdto.
Some of the basic fields in Bookdto can correspond directly to the fields in book.

Fields in the field book in Bookdto

Title Title

Description Description

Language Language

Price price

Publishdate publishdate

Paperback Paperback


Each book has a maximum of 2 authors, each using the "first" prefix and the "Second" prefix in the Bookdto field. Therefore, all firstxxx fields are mapped to the 1th author object in the book's authors, and all secondxxx fields are mapped to the 2nd authors object in author.

Fields in the 1th author object in authors in the field book in Bookdto

Firstauthorname Name

Firstauthordescription Description

Firstauthoremail Contactinfo.email

Firstauthorblog Contactinfo.blog

Firstauthortwitter Contactinfo.twitter


Notice that the contactinfo.email in the previous table represents the email field corresponding to the contactinfo of the author object, and so on. Similarly we have:

Fields in the 2nd author object in authors in the field book in Bookdto

Secondauthorname Name

Secondauthordescription Description

Secondauthoremail Contactinfo.email

Secondauthorblog Contactinfo.blog

Secondauthortwitter Contactinfo.twitter


Finally there is the Publisher field, which corresponds to a standalone Publisher object.

fields in Publisher in the Bookdto field

Publisher Name


That's about it, our demand is to transform the data between this big, DTO, and the model of another big lump.

  • 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.