How to use DTO's between UI, BLL, DAL

Oliver picture Oliver · Jan 14, 2011 · Viewed 8.9k times · Source

I'm trying to write a small app with very strict boundaries between BLL and DAL and am now wondering what the best way would be to pass the data (Domain Transfer Objects) between the layers.

I implemented some classes in a Domain Level (class library) that is accessed by both BLL and DAL. These classes basically just contain properties/data members and currently reflect the DAL data. Ex:

class CustomerData
{
  // some data fields
}

Then I implemented some classes in BLL as :

class Customer : CustomerData
{
  // Some methods
}

In my DAL I get the customer records from the database through Linq-to-Sql. I then map the linq object to my Domain object by:

CustomerData.field = LinqObject.field
// Etc

My thinking is thus that I now a CustomerData instance from my DAL to BLL when requested (and that I should pass a Customer instance to my UI).

In my BLL I will thus receive a CustomerData instance, but now I want to make a Customer out of it.

Questions:

  1. Do I have to in my BLL now create a Customer instance and AGAIN copy all field members ?
    Customer c = new Customer; c.field = CustomerData.field;
  2. How can I create a Customer from the CustomerData without the field copy steps ?
  3. Should I rather then use composition ?
    class Customer { CustomerData Data; }
  4. Is there a more effective way (less coding etc) to do this in my current layout ?
  5. Is there better ways to do this ?
  6. Any comments in general ?

Thanks !

Answer

Dead.Rabit picture Dead.Rabit · Jan 14, 2011

generally I consider DTO's to be non layer specific, created/consumed by DAL, processed by BLL and consumed/created by UI.

usually each layer is a separate project in the VS solution folder, the DTO's therefore are another project which is referenced by each layer.

this way if there is a field that needs to exist in the UI but not in the other layers, the DTO can be inherited from.