Implementing DTO's in.Net RRS feed

  • Question

  • Hi,
    Can anyone suggest some good approaches to build Data Transfer Objects using .Net custom classes. Also would like to make them bindable friendly. Meaning when I have an array of entities and would like to bind the same to a datagrid. what interfaces do I need to implement to make it bindable to the datagrid.


    Wednesday, November 1, 2006 10:16 AM

All replies

  • First you should definately read Martin Fowler's Patterns of Enterprise Application Architecture if you have not already - there is a whole pattern/chapter on DTOs with example implementations in Java.  The Java code is easy to understand and translate into C# or VB.NET.  Since the DTOs are just data holders you might be tempted to just create a class with some public members.  For databinding, though, you really need to use actual public Properties.  For the DataGrid, all you need is an array or any enumerable collection. I prefer a System.Collections.Generics.List<T>.   Make sure the DTOs are marked serializable.  Use binary serialization with .NET remoting.   Use SOAP for web services.
    Wednesday, November 1, 2006 3:00 PM
  • Hi Sai,

       I invite you to check an article published in SkyScrapr a couple of weeks ago

       That article comments about DTO and particularly mentions a free way to take advantage of its benefits in the .NET platform, thanks to the typed datasets (not just mere ADO.NET datasets but domain classes backed on datasets)



       Hope it helps

    Sunday, November 5, 2006 5:48 PM
  • Dtos are really just for transfers, you should probably map the dto to a custom business object. 

    for binding its best to look for interfaces under the system.componentmodel   i.e.  IdataErrorInfo and INotifyPropertyChanged. If you want to learn more about business objects, try looking at a framework like that might give you some ideas. 
    Sunday, November 5, 2006 9:39 PM
  • Wednesday, November 8, 2006 8:21 AM
  • Sai,

    I've found that using XML schemas to define the DTOs, and then using the xsd.exe VS2005 command line utility to generate the classes very useful on a few counts:
    1. If you're using a good XML editor (eg: XMLSpy), you can export the schema diagrams to images and embed them in your documentation. Very handy for handing over to other developers
    2. You don't need to write a line of code to build the classes assuming you set the data types in your child elements (eg: xs:dateTime, xs:string, etc) appropriately
    3. You can use them in web services as method parameters and return types without needing to map your DTO to an XML-serializable data type
    4. It's just as easy to bind objects and arrays of objects generated in this manner to data grids and any other form elements using the ObjectDataSource object.
    Hope this helps.


    Wednesday, November 8, 2006 10:05 PM