-
Notifications
You must be signed in to change notification settings - Fork 42
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:
- ListCustomerDto.cs which is only used for listing Customers.
- CrudSalesOrderDetailDto.cs that is used for list, detail, create and update.
As you can see the main parts are:
- 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.
- You might want to add attributes to a property that help the presentation layer, like
- 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.
- The
CrudFunctions
enum is a set of flags. You can find its definition in EfGenericDtoBase.cs
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.
Live example web sites!
Introduction/Overview
Commands and options
- Introduction to Commands
- Key interfaces
- Calculated properties
- DoNotCopyBackToDatabase attribute
- Configuration Options
Data Transfer Objects (DTOs)