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); } }}