Which layer of the application should contain DTO implementation

徘徊边缘 提交于 2021-02-05 13:11:54

问题


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


回答1:


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.




回答2:


Your service layer exposes DTO's. This means that in the service layer you define data contracts as you would like them to be exposed to the outside world. In most cases they are flattened entities that not necessarily have the same structure as your database entities.

It is the responsibility of your service layer to use business/data layer and construct the DTO's that you expose to the outside world.

What you use in your business and data layer depends on the architecture. You could have a domain model that is mapped with code first. In that case, the service layer maps domain entities to data contracts (DTO's). If you don't have a domain model (anemic model), then you could as well just map the database directly to your DTO's.

The ASP.NET MVC site consumes the service, and maps the DTO's it receives to dedicates view models that are then passed to the specific view.

In addition, you may decide to also split queries from commands. This is a good approach because the DTO's that you get back as the reqult of a query are totally different than commands that you send to the service. A command only contains what's needed to execute the command and contain the business intend what you want to achieve, while a query returns a flattened model of what you need in the UI.

Other remarks:

  • Don't expose your database entities.
  • Don't convert in business layer, as it's not business logic.



回答3:


Have a look at https://stackoverflow.com/a/6310507/1771365 added here as I don’t have enough reputation to add comments.

Personally I would pass entitys between your Persistence layer and your business layer. As you are using MVC chances are your are going pass view models to your controllers. At which point I would map your view model to your DTOs(s).

If you plan to use DTO between all of your layers then create a cross cutting project which you can then reference.




回答4:


An approach that I'm particularly fond of is to the DTO conversion in your Business Layer.

Scenario: Your Presentation Layer calls your Business Layer passing a DTO. You do some logic & validation, then convert the DTO to an entity and send it to your Data Access Layer.

i.e. UI --> Bus. Layer (convert to Entity) --> Data Layer

I like this approach as I believe the Data Layer should not have any conversion logic and should receive and handle entities as needed. Another reason why this is effective is that you can now define specific business rules / validation logic during the conversion process before sending it to the Data Layer. Here's an old MSDN article, but has some great details explaining a similar approach.




回答5:


I use a project named Shared for such purposes, spesifically to share object with all layers. Regardless of the name, it should be visible by all layers.




回答6:


There should not be any conversion. You would just use Poco Objects as DTO.

EF works with Poco and they can be serialized by WCF.

They should be defined in an assembly that is refernced by all projects.

In ASP.NET MVC you would mapp to a ViewModel by using the Poco - DTO.



来源:https://stackoverflow.com/questions/28298642/which-layer-of-the-application-should-contain-dto-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!