应用程序的哪一层应包含DTO实现 [英] Which layer of the application should contain DTO implementation
问题描述
最近我已经听到了很多有关DTO及其用途的信息,但是我找不到在ASP.NET上下文中使用DTO的好例子.
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:
- 数据层(使用实体框架)
- 业务层(WCF服务)
- 表示层(MVC 4.0 Web应用程序)
我应该在哪里将EF Employee对象转换为EmployeeDTO POCO?
Where should I convert from the EF Employee object to an EmployeeDTO POCO?
让我们说我在数据访问层中进行了转换,但是WCF服务中会发生什么呢?然后应该将其转换为另一个 DataMember
对象,并且当其到达UI层(MVC Web应用程序)时是否应该将其第三次转换为模型?如果有人可以为我清除此信息,我将不胜感激
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
推荐答案
在类似情况下,我曾经将dto放入 Core 中,这三者都知道.所以你有
In similar situation I used to put dto's into Core which is known to all three. So you have
Core
|
------------
| | |
DAL BL PL
每层都可以与 Core.Dto.Employee
一起运行.每层还在其API中从外部公开 Core.Dto.Employee
.但是内部每个层都可以转换/适应 Core.Dto.Employee
,例如您从数据库 EF.Employee
中读取,然后将其转换为 Core.Dto.Employee
.变换包含在图层的边界中.
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.
如果您有多个不同的模型来表示整个层中的同一事物,例如PL想要 PL.Employee
,而DAL在 EF.Employee
上运行,那么您将最终一团糟.
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.
这篇关于应用程序的哪一层应包含DTO实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!