Free conversion of DTOs and model using AutoMapper (UP) "Go"

Source: Internet
Author: User

Transferred from: http://zz8ss5ww6.iteye.com/blog/1126205

Note: The code for this series of articles can be downloaded here.

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
    1. Public Class Bookstore
    2. {
    3. public string Name { get;  set;}
    4. Public list<book> Books { get;  set;}
    5. Public address Address { get;  set;}
    6. }


The bookstore has its own address:

C # code
  1. Public class Address
  2. {
  3. public string Country { get;  set;}
  4. Public string City { get;  set;}
  5. public string Street { get;  set;}
  6. public string Postcode { get;  set;}
  7. }


At the same time, the bookstore put N book:

C # code
  1. Public Class book
  2. {
  3. public string Title { get;  set;}
  4. public string Description { get;  set;}
  5. public string Language { get;  set;}
  6. public decimal price { get;  set;}
  7. Public list<author> Authors { get;  set;}
  8. Public DateTime? publishdate { get;  set;}
  9. Public Publisher Publisher { get;  set;}
  10. public int? Paperback { get;  set;}
  11. }


Each book has publisher information (publisher):

C # code
    1. Public class Publisher
    2. {
    3. public string Name { get;  set;}
    4. }


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

C # code
    1. Public class Author
    2. {
    3. public string Name { get;  set;}
    4. public string Description { get;  set;}
    5. Public contactinfo ContactInfo { get;  set;}
    6. }


Each author has his own contact information (ContactInfo):

C # code
    1. Public class ContactInfo
    2. {
    3. public string Email { get;  set;}
    4. public string Blog { get;  set;}
    5. public string Twitter { get;  set;}
    6. }


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
    1. Public class Bookstoredto
    2. {
    3. public string Name { get;  set;}
    4. Public list<bookdto> Books { get;  set;}
    5. Public addressdto Address { get;  set;}
    6. }


It contains the addressdto corresponding to address:

C # code
  1. Public class Addressdto
  2. {
  3. public string Country { get;  set;}
  4. Public string City { get;  set;}
  5. public string Street { get;  set;}
  6. public string Postcode { get;  set;}
  7. }


and the bookdto corresponding to the book:

C # code
  1. Public class Bookdto
  2. {
  3. public string Title { get;  set;}
  4. public string Description { get;  set;}
  5. public string Language { get;  set;}
  6. public decimal price { get;  set;}
  7. Public DateTime? publishdate { get;  set;}
  8. public string Publisher { get;  set;}
  9. public int? Paperback { get;  set;}
  10. public string Firstauthorname { get;  set;}
  11. public string Firstauthordescription { get;  set;}
  12. public string Firstauthoremail { get;  set;}
  13. public string Firstauthorblog { get;  set;}
  14. public string Firstauthortwitter { get;  set;}
  15. public string Secondauthorname { get;  set;}
  16. public string Secondauthordescription { get;  set;}
  17. public string Secondauthoremail { get;  set;}
  18. public string Secondauthorblog { get;  set;}
  19. public string Secondauthortwitter { get;  set;}
  20. }


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 the Bookstoredto Fields in the bookstore
Name Name
Books Books
Address Address


(2) Address addressdto

Fields in the Addressdto Fields in Address
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 Bookdto The fields in book
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 Bookdto A field in the 1th author object in authors in book
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 Bookdto A field in the 2nd author object in authors in book
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 the Bookdto Fields in Publisher
Publisher Name


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

Free conversion of DTOs and model using AutoMapper (UP) "Go"

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.