分层架构中的实体框架 [英] Entity Framework in Layered Architectures

查看:138
本文介绍了分层架构中的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我读过文章分层架构中的实体框架,有书面我们可以通过WCF向客户端发送EF实体。但是在Stackoverflow的许多线程中,人们会告诉我们在使用WCF时应该使用POCO(DTO)对象。
我有一些问题。


  1. 为什么微软为EF实体添加DataContract属性?微软希望我们在应用程序中使用这些对象吗?或者这只是为了非常简单的应用程序和快速开发?


  2. 如果我使用POCO对象,我应该创建自动生成的EF实体,POCO实体之后他们之间使用任何映射库?或者我应该在应用程序的所有组件中仅使用POCO对象?


  3. 如果我已经拥有自己的业务实体,有一些方法,映射到POCO对象,在哪个层应该将POCO对象转换为我的实体(例如,我有持久层,业务逻辑层,服务层(WCF),演示者层(客户端,使用WCF),UI层)?或者我不应该这样做我自己的实体?


提前感谢

解决方案


1.为什么Microsoft为EF实体添加DataContract
属性?
微软希望我们在
应用程序中使用这些
对象吗?或者这只是非常
简单的应用程序和快速
开发?


一般来说,它是一个在您的服务层中暴露您的EF实体的一个好主意,因为几乎不能满足您的服务层和模型表示。所以在模型中完成的任何更改都会直接影响您的服务,而不是一个好主意。您也必须在某一时刻为您的服务层进行版本管理,以避免在您的服务层中暴露EF实体。


2.如果我使用POCO对象,我应该创建自动生成的EF实体,
POCO-Entities,之后使用任何
映射库?或者我
应该只使用POCO对象在我的应用程序的所有
组件?


您可以使用POCO服务层内的对象,将其与任何底层分离(请参阅Automapper,以涵盖Entity-DTO映射成本)。但您仍然可以在架构中的数据和业务层中使用自动生成的EF实体。只是尝试不依赖于EF数据层的其他层中生成的域模型的特定功能。如果我已经拥有自己的业务
实体,这有一些方法,而且还有一些方法,它
应该映射到POCO对象,在
哪个层应该转换
POCO对象到我的实体(例如,
我有持久层,业务
逻辑层,服务层(WCF),
演示者层(客户端,使用WCF),UI
层)?或者我不应该这样我的
自己的实体?


服务层 http://msdn.microsoft.com/en-us/library/ms978717.aspx 。您将在应用程序的服务器层(持久性,业务,服务和演示者层)中透明地使用您的域模型,唯一需要您进行DTO映射的层就是服务层,请参见问题1.(另外,如果您在演示者层中使用ViewModels - 主意 - 您将需要在演示者层中使用POCO映射)。


Recently I've read article "The Entity Framework In Layered Architecture" and there is written we can send EF-entities to client through WCF. But in many threads on Stackoverflow people tell that POCO(DTO)-objects should be used when we use WCF. And I have some questions.

  1. Why did Microsoft add DataContract attribute to EF-entities? Does Microsoft wanted us to use these objects everywhere in our applications? Or this is only for very simple applications and for rapid development?

  2. If I use POCO-objects, should I create auto generated EF-Entities, POCO-Entities and after that use any mapping library between them? Or I should use only POCO-objects in all components of my application?

  3. If I already have my own business entity, which has some methods, and it should be mapped to POCO object, on which layer should I convert POCO-object to my entity (for example, I have persistence layer, business logic layer, service layer(WCF), presenter layer (client, use WCF), UI layer)? Or I shouldn't make such my own entities?

Thanks in advance

解决方案

1.Why did Microsoft add DataContract attribute to EF-entities? Does Microsoft wanted us to use these objects everywhere in our applications? Or this is only for very simple applications and for rapid development?

Generally speaking, it is a bad idea to expose your EF-Entities in the service layer because that hardly couples your service layer and model representation. so any changes done in the model ends affecting directly your services, not a good idea. also you will have to version your service layer in some moment, so avoid to expose the EF entities in your service layer.

2.If I use POCO-objects, should I create auto generated EF-Entities, POCO-Entities and after that use any mapping library between them? Or I should use only POCO-objects in all components of my application?

You can use POCO objects inside your service layer, to decouple it from any underlying layers (see Automapper, to cover the Entity-DTO mapping cost). but you could still use the autogenerated EF-entities among the data and business layers in your architecture. just try to not rely in EF specific features of your generated domain model in other layers different from data layer. to ease the migration to another ORM frameworks.

If I already have my own business entity, which has some methods, and it should be mapped to POCO object, on which layer should I convert POCO-object to my entity (for example, I have persistence layer, business logic layer, service layer(WCF), presenter layer (client, use WCF), UI layer)? Or I shouldn't make such my own entities?

Service layer http://msdn.microsoft.com/en-us/library/ms978717.aspx. you would be using your domain model transparently among the server tier (persistence, business, service and presenter layers) of your application, and the only layer that will require you a DTO mapping is the service layer, see question 1. (additionally if you are using ViewModels inside your the presenter layer -nice idea- you will require to use POCOs-mapping in the presenter layer too).

这篇关于分层架构中的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆