Skip to content

Basic use of DTOs

Jon Smith edited this page Jan 20, 2015 · 2 revisions

There are plenty of times you just want to transform the data or access a calculated property. In these cases the DTO is really easy to write. Here are some examples:

As you can see the main parts are:

  1. The properties you want, with the right names to cause the copy. See DTO data copying.
    • You might want to add attributes to a property that help the presentation layer, like [DataType(DataType.Currency)] or validation attributes like [StringLength(25)]
    • In the CrudSalesOrderDetailDto.cs you will see the use of the [DoNotCopyBackToDatabase] attribute. The name says what it does.
  2. You must override the SupportedFunctions property say what methods this DTO supports. This is safety check to stop the wrong DTO being used in the wrong place.

See what GenericServices saves you doing

You might like to look at the commented out definition of ListQueryUntracked in ListSalesOrderDto.cs to see what you would need to do if GenericServices did not use AutoMapper and DelegateDecompiler. As you can see its not that hard to write your own copying, but that example is only for list. When you have hundreds of DTOs and two-way copying it gets boring, and you have to check them all.