Free conversion of DTOs and model using AutoMapper (up)

Source: Internet
Author: User
Tags bbcode
<span id="Label3"></p><p><p>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.<br>Frequent data conversions are trivial and messy, and many times we have to:<br>(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.<br>(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.<br>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.<br>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.<br><strong>"one" Application Scenario Description</strong><br>Let's take a look at my "virtual" domain model. This time I defined a bookstore (bookstore):</p></p>C # code <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li><span class="keyword">Public <span class="keyword">Class Bookstore</span></span></li> <li>{</li> <li><span class="keyword">public <span class="keyword">string Name { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li> <li>public <span class="keyword">list<book> Books { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li> <li>public <span class="keyword">address Address { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li> <li>}</li> </ol> </ol><p><p><br>The bookstore has its own address:</p></p>C # code <ol class="dp-c" start="1"> <li><li><span class="keyword">Public <span class="keyword">class Address</span></span></li></li> <li><li>{</li></li> <li><li><span class="keyword">public <span class="keyword">string Country { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">Public <span class="keyword">string City { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Street { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Postcode { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li>}</li></li> </ol><p><p><br>At the same time, the bookstore put n book:</p></p>C # code <ol class="dp-c" start="1"> <li><li><span class="keyword">Public <span class="keyword">class</span> book</span></li></li> <li><li>{</li></li> <li><li><span class="keyword">public <span class="keyword">string Title { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Description { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Language { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">decimal price { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li>public <span class="keyword">list<author> Authors { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li></li> <li><li>public <span class="keyword">DateTime? publishdate { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li></li> <li><li>public <span class="keyword">Publisher Publisher { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li></li> <li><li><span class="keyword">Public <span class="keyword">int? Paperback { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li>}</li></li> </ol><p><p><br>Each book has publisher information (publisher):</p></p>C # code <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li><span class="keyword">Public <span class="keyword">class Publisher</span></span></li> <li>{</li> <li><span class="keyword">public <span class="keyword">string Name { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li> <li>}</li> </ol> </ol><p><p><br>Each book can have up to 2 authors ' information (Author):</p></p>C # code <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li><span class="keyword">Public <span class="keyword">class Author</span></span></li> <li>{</li> <li><span class="keyword">public <span class="keyword">string Name { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li> <li><span class="keyword">public <span class="keyword">string Description { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li> <li>public <span class="keyword">contactinfo ContactInfo { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li> <li>}</li> </ol> </ol><p><p><br>Each author has his own contact information (contactinfo):</p></p>C # code <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li><span class="keyword">Public <span class="keyword">class ContactInfo</span></span></li> <li>{</li> <li><span class="keyword">public <span class="keyword">string Email { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li> <li><span class="keyword">public <span class="keyword">string Blog { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li> <li><span class="keyword">public <span class="keyword">string Twitter { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li> <li>}</li> </ol> </ol><p><p><br>That's almost it, a domain model with hierarchical structure.<br>Take a look at our dto Structure.<br>In the DTO we have the bookstoredto corresponding to the bookstore:</p></p>C # code <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li><span class="keyword">Public <span class="keyword">class Bookstoredto</span></span></li> <li>{</li> <li><span class="keyword">public <span class="keyword">string Name { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li> <li>public <span class="keyword">list<bookdto> Books { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li> <li>public <span class="keyword">addressdto Address { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li> <li>}</li> </ol> </ol><p><p><br>It contains the addressdto corresponding to Address:</p></p>C # code <ol class="dp-c" start="1"> <li><li><span class="keyword">Public <span class="keyword">class Addressdto</span></span></li></li> <li><li>{</li></li> <li><li><span class="keyword">public <span class="keyword">string Country { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">Public <span class="keyword">string City { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Street { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Postcode { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li>}</li></li> </ol><p><p><br>and the bookdto corresponding to the Book:</p></p>C # code <ol class="dp-c" start="1"> <li><li><span class="keyword">Public <span class="keyword">class Bookdto</span></span></li></li> <li><li>{</li></li> <li><li><span class="keyword">public <span class="keyword">string Title { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Description { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Language { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">decimal price { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li>public <span class="keyword">DateTime? publishdate { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Publisher { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">Public <span class="keyword">int? Paperback { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Firstauthorname { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Firstauthordescription { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Firstauthoremail { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Firstauthorblog { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Firstauthortwitter { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Secondauthorname { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Secondauthordescription { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Secondauthoremail { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Secondauthorblog { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li><span class="keyword">public <span class="keyword">string Secondauthortwitter { <span class="keyword">get;  <span class="keyword">set;} </span></span></span></span></li></li> <li><li>}</li></li> </ol><p><p><br>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.<br>Just let's take a look at the map rules for DTOs to Model.<br>(1) Bookstoredto-bookstore</p></p> <table class="bbcode"> <tbody> <tr> <td>Fields in the Bookstoredto</td> <td>Fields in the bookstore</td> </tr> <tr> <td>Name</td> <td>Name</td> </tr> <tr> <td>Books</td> <td>Books</td> </tr> <tr> <td>Address</td> <td>Address</td> </tr> </tbody> </table><p><p><br>(2) Address Addressdto</p></p> <table class="bbcode"> <tbody> <tr> <td>Fields in the Addressdto</td> <td>Fields in Address</td> </tr> <tr> <td>Country</td> <td>Country</td> </tr> <tr> <td>City</td> <td>City</td> </tr> <tr> <td>Street</td> <td>Street</td> </tr> <tr> <td>Postcode</td> <td>Postcode</td> </tr> </tbody> </table><p><p><br>(3) bookdto.<br>Some of the basic fields in Bookdto can correspond directly to the fields in Book.</p></p> <table class="bbcode"> <tbody> <tr> <td>Fields in the Bookdto</td> <td>The fields in book</td> </tr> <tr> <td>Title</td> <td>Title</td> </tr> <tr> <td>Description</td> <td>Description</td> </tr> <tr> <td>Language</td> <td>Language</td> </tr> <tr> <td>Price</td> <td>Price</td> </tr> <tr> <td>Publishdate</td> <td>Publishdate</td> </tr> <tr> <td>Paperback</td> <td>Paperback</td> </tr> </tbody> </table><p><p><br>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.</p></p> <table class="bbcode"> <tbody> <tr> <td>Fields in the Bookdto</td> <td>A field in the 1th author object in authors in book</td> </tr> <tr> <td>Firstauthorname</td> <td>Name</td> </tr> <tr> <td>Firstauthordescription</td> <td>Description</td> </tr> <tr> <td>Firstauthoremail</td> <td>Contactinfo.email</td> </tr> <tr> <td>Firstauthorblog</td> <td>Contactinfo.blog</td> </tr> <tr> <td>Firstauthortwitter</td> <td>Contactinfo.twitter</td> </tr> </tbody> </table><p><p><br>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:</p></p> <table class="bbcode"> <tbody> <tr> <td>Fields in the Bookdto</td> <td>A field in the 2nd author object in authors in book</td> </tr> <tr> <td>Secondauthorname</td> <td>Name</td> </tr> <tr> <td>Secondauthordescription</td> <td>Description</td> </tr> <tr> <td>Secondauthoremail</td> <td>Contactinfo.email</td> </tr> <tr> <td>Secondauthorblog</td> <td>Contactinfo.blog</td> </tr> <tr> <td>Secondauthortwitter</td> <td>Contactinfo.twitter</td> </tr> </tbody> </table><p><p><br>Finally there is the publisher field, which corresponds to a standalone publisher Object.</p></p> <table class="bbcode"> <tbody> <tr> <td>Fields in the Bookdto</td> <td>Fields in Publisher</td> </tr> <tr> <td>Publisher</td> <td>Name</td> </tr> </tbody> </table><p><p><br>That's about it, Our demand is to transform the data between this big, dto, and the model of another big Lump.</p></p><p><p>Free conversion of DTOs and model using AutoMapper (up)</p></p></span>
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.