Simple use of automapper for DTO transformation

Source: Internet
Author: User

The DTO (Data Transfer object) data Transfer object, which simply transmits the data, completes the transformation between the domain objects and does not contain domain business processing.
When domain model designers focus only on the core business and are satisfied with the refinement of the domain model rather than the specific implementation, the DTO appears in large numbers.

When the system is complicated, DTOs may be implemented in multiple domain model combinations.

Why use DTOs?
1, isolate the domain model, make changes to the domain models without affecting the UI, maintain the security of the domain model, do not expose the business logic.
2, in distributed mode, different scenarios using the same data structure have different requirements.

Simple example:

Order.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceappexample{ Public classOrder { Public intOrderID {Get;Set; }  Public stringOrderNumber {Get;Set; }  PublicDateTime OrderDate {Get;Set; } }}

OrderDTO.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceappexample{ Public classOrderdto { Public intOrderID {Get;Set; }  Public stringOrderNumber {Get;Set; }  PublicDateTime OrderDate {Get;Set; }  Public intOrderYear {Get;Set; } }}

Program.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceappexample{classProgram {Static voidMain (string[] args) {Order Order=NewOrder {OrderID=1, OrderNumber="201406040001", OrderDate=DateTime.Now}; AutoMapper.Mapper.Initialize (CFG={cfg. Createmap<order, orderdto>()                    . Formember (dest= = Dest. OrderYear, opt = opt. Mapfrom (src =src.            Orderdate.year));            }); Orderdto Orderdto= Automapper.mapper.map<order, orderdto>(order); Console.WriteLine ("{0}-{1}-{2}-{3}", Orderdto.orderid, Orderdto.ordernumber, Orderdto.orderdate, orderdto.orderyear); }    }}

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.