18 07 2008
DTO – not only for web services
Usually developers think that DTO (Data Transfer Object) is something related to web services where data serialization is required to move data from one point of network to another. It is clear that in most cases there is no point to move complex data structures over network when client side needs this data in pretty simple form. That’s why DTOs are created – they are simple, easily serializable and there is no data that client side doesn’t need. But there are also other DTOs you may find useful.
User Interface DTOs
User Interface (UI) may also need some DTOs. By example, data bound controls try usually to save bounded data to their ViewState. But what happens when data source is list on some objects and not DataTable as we can usually see from beginner tutorials? DataTable is serializable and therefore it is easy to save DataTable to ViewState. But objects are often not marked as serializable and binding them to data bound controls may cause exceptions.
This problem can be solved using DTOs. We can create DTOs as flat objects that are serializable. They doesn’t contain any child objects – only properties with their values. We can wrap source data to DTOs and bind list of these DTOs to our data bound controls.
Search criteria DTOs
In one of our current project we are using some DTOs to define search criterias. These DTOs are also flat ones because they store only ID-s of search conditions, string tokens and dates. There is one main search DTO and some internal search DTOs. This far we are happy with this solution because if we design those DTOs carefully they are very easy and convenient to use in client code.
Search sitemap DTOs
ASP.NET Futures has support for search engine sitemaps (follow this reference to take a look at the code examples). To let it create a search engine sitemap for products you must define class for products that has specific attributes required by sitemap generating system. This object is also simple DTO because it has no complex attributes.
I am sure that there are many more DTOs, these three were just examples of some additional DTOs besides DTOs we are using with web services. Okay, and as a last word – DTO is design pattern refered also to as Value Object in Sun.