简体   繁体   English

实体框架和DTO

[英]Entity Framework and DTO

Im planning to use the Entities generated by the EF (POCO) in sending data to the client instead of creating DTOs? 我打算使用EF(POCO)生成的实体向客户端发送数据而不是创建DTO? Is this a good practice? 这是一个好习惯吗? Basically, my EDMX file is on my DAL layer. 基本上,我的EDMX文件在我的DAL层上。 So the UI will have direct access on my DAL. 因此,用户界面将直接访问我的DAL。 Thanks. 谢谢。

It depends on how close the client is to your object domain. 这取决于客户端与对象域的接近程度。 If it is your client , then maybe - and indeed this is pretty much how ADO.NET Data Services (etc) work - directly exposing your model. 如果它是您的客户端 ,那么可能 - 实际上这几乎是ADO.NET数据服务(等)如何工作 - 直接暴露您的模型。

However, if the client is anything else I would suggest a dedicated DTO. 但是,如果客户是其他任何我建议专用的DTO。 In fact, I'd suggest it anyway ;p Otherwise, it gets somewhat complex: 事实上,无论如何我都会建议它;否则,它会变得有些复杂:

  • controlling the serialization details (what members? what names? what happens when we version it?) 控制序列化细节(什么成员?什么名字?当我们发布它时会发生什么?)
  • handling relation properties (it has an Orders member... but is that lazily loaded? do we want that?) 处理关系属性(它一个Orders成员......但是它是懒惰加载的吗?我们想要吗?)
  • merging incoming objects (for updates etc) back into the model 将传入对象(用于更新等)合并回模型
  • suppressing any logic that you've added in setters, etc, during deserialization 在反序列化期间抑制您在setter等中添加的任何逻辑
  • handling identity management if you get 2 separate copies of the same object back in a tree-based serializer like DataContractSerializer 如果在基于树的序列化程序(如DataContractSerializer中获得同一对象的2个单独副本,则处理身份管理

In most cases, having a separate DTO makes most of these problems just go away 在大多数情况下,拥有一个单独的DTO会使大部分问题消失

Basically, I don't think it's a good idea to send DAL objects to your interface, so I would use DTOs. 基本上,我不认为将DAL对象发送到您的界面是个好主意,所以我会使用DTO。 To minimize the effort doing so I would take a look at an DTO generator , to generate DTO code which lets you convert from DAL object to DTO and vice versa. 为了最大限度地减少这样做,我将看一下DTO生成器 ,生成DTO代码,它允许您从DAL对象转换为DTO,反之亦然。

EDIT: Sorry, didn't see you are using POCO. 编辑:对不起,没看到你正在使用POCO。 Have a look at this SO post 看看这篇SO帖子

DTO is a good practice to minimise the amount of data you transfer over the wire to only include relevant fields. DTO是一种很好的做法,可以最大限度地减少通过网络传输的数据量,只包含相关字段。 This is amongst other benefits. 这是其他好处之一。 Checkout Automapper and the ProjectTo method which can automatically convert DAL to DTO and vice versa. Checkout Automapper和ProjectTo方法,可以自动将DAL转换为DTO,反之亦然。 ProjectTo under the bonnet will only select columns included in the configured mapping. 项目引擎盖下的项目将仅选择已配置映射中包含的列。

https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions

First of all, I believe you cannot use Entities generated by Entity Framework as return types in your Service, at least in a WCF Service. 首先,我相信您不能将实体框架生成的实体用作服务中的返回类型,至少在WCF服务中是这样。

But, why you would like to use entities all across your application? 但是,为什么要在整个应用程序中使用实体? If you have a common architecture with a client-server structure, your client won't need all the information that an EntityObject has, like the ObjectContext where it is contained, the state on it, and lots of other info that your client not only won't use, but more important: don't have to know. 如果您有一个具有客户端 - 服务器结构的通用体系结构,那么您的客户端将不需要EntityObject具有的所有信息,例如包含它的ObjectContext,其上的状态以及客户端的许多其他信息不会使用,但更重要的是:不必知道。

In that case you should use the DTO pattern, or other design pattern you think better that decouples the Server side from the Client Side. 在这种情况下,您应该使用DTO模式或您认为更好的其他设计模式,将服务器端与客户端分离。 I believe DTO Pattern is the most widely used and recommended. 我相信DTO Pattern是最广泛使用和推荐的。 If you are using Entity Framework you can go to http://entitiestodtos.codeplex.com , it is and AddIn for Visual Studio that I published, it is free and open source. 如果您使用的是Entity Framework,可以访问http://entitiestodtos.codeplex.com ,它是我发布的Visual Studio和AddIn,它是免费的开源软件。 It generates your DTOs from your Entity Framework Data Model (EDMX). 它从您的实体框架数据模型(EDMX)生成您的DTO。

Regards, Fabian Fernandez 此致,Fabian Fernandez

Using Entity Framework code first TT such as "EntityFramework Reverse POCO Generator" ( https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator ) to generate Business Models associated to database tables schema in separate Business Entity Model library. 首先使用实体​​框架代码TT,例如“EntityFramework Reverse POCO Generator”( https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator ),以在单独的业务实体模型库中生成与数据库表模式关联的业务模型。 The TT is able to be modified to include custom attributes (such as DataMember, Serializable) specially to WCF Entity. 可以修改TT以特别向WCF实体包含自定义属性(例如DataMember,Serializable)。

This way could have both benefits of DTO and EF POCO. 这种方式既可以带来DTO和EF POCO的好处。 It could easily maintain consistence between Business Models Entities and database tables. 它可以轻松地保持业务模型实体和数据库表之间的一致性。

The reason is, for most cases, any time modify the database table structure, developer also needs to consistent the Business Model structure (DTO here). 原因是,对于大多数情况,任何时候修改数据库表结构,开发人员还需要一致的业务模型结构(这里是DTO)。 There is additional works to maintain mappers. 还有其他工作来维护映射器。

If the database access layer model (EF POCO here) loose coupling with DTO model. 如果数据库访问层模型(这里的EF POCO)与DTO模型松散耦合。 Developer will be very difficult to detect the potential error which cannot show in compilation time specially for maintaining existing enterprise level applications. 开发人员很难检测到在编译时间内无法显示的潜在错误,专门用于维护现有的企业级应用程序。

If we has additional custom properties required for Business Model entities, we are able to add partial class with custom properties. 如果我们有业务模型实体所需的其他自定义属性,我们可以添加具有自定义属性的部分类。

We are still able to use mappers to convert EF POCO entity to DTO entity when it needed. 我们仍然可以使用映射器在需要时将EF POCO实体转换为DTO实体。 But my point is, try to minimize abuse using mappers which just doing duplicate works compared with directly binding data to EF POCO model. 但我的观点是,与直接将数据绑定到EF POCO模型相比,尝试使用mappers来减少滥用,这只是做重复工作。

Since we can generate the EF POCO entities in separated Business Model library, DTO entity is able to stay with them in same Business Model library. 由于我们可以在独立的业务模型库中生成EF POCO实体,因此DTO实体可以将它们保留在同一个业务模型库中。 Thus, developer has more flexibility to decide how to use them. 因此,开发人员可以更灵活地决定如何使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM