Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which layer of the application should contain DTO implementation

Lately I've been hearing a lot about DTOs and how useful they are but I can't find a good example of using it in ASP.NET context.

Let's say I use three tier architecture:

  1. Data layer(using Entity Framework)
  2. Business Layer(WCF Service)
  3. Presentation Layer (MVC 4.0 web application)

Where should I convert from the EF Employee object to an EmployeeDTO POCO?

Lets say I do the conversion in the Data Access layer but what happens in the WCF service? Should it then be converted to another DataMember object and when it get's to the UI layer(MVC web app) should it then be converted for the third time to a model? I would appreciate it if someone could please clear this for me

like image 371
Denys Wessels Avatar asked Sep 07 '25 08:09

Denys Wessels


1 Answers

In similar situation I used to put dto's into Core which is known to all three. So you have

     Core
       |
 ------------
 |     |    |
DAL   BL   PL

Each layer can operate with Core.Dto.Employee. Each layer also exposes Core.Dto.Employee externally in its API. But internally each layer can transform/adapt Core.Dto.Employee, e.g. you read from database EF.Employee and later convert it to Core.Dto.Employee. Transformation is contained by the layer's boundary.

If you have several different models to represent same thing throughout the layers, for example PL wants PL.Employee and DAL operates on EF.Employee, you will end up with a mess.

like image 51
oleksii Avatar answered Sep 09 '25 04:09

oleksii