在分层体系结构实体框架 [英] Entity Framework in Layered Architectures

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

问题描述

最近,我读到一篇文章实体框架在分层架构,并有写我们可以通过WCF发送EF-实体客户端。但在#1人很多线程告知,当我们使用WCF POCO(DTO)-objects应该被使用。 我有一些问题。

  1. Microsoft为什么添加DataContract属性EF-实体?难道微软要我们在我们的应用无处不在使用这些对象?或者,这只是非常简单的应用和快速发展?

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

  3. 如果我已经有了自己的业务实体,具有一定的方法,它应该被映射到POCO的对象,在这层,我应该转换POCO对象,以我的实体(例如,我有持久层,商业逻辑层,服务层(WCF),presenter层(客户端,使用WCF),UI层)?或者我不应该让这样的我自己的实体?

在此先感谢

解决方案
  

1.为什么微软没有加入DataContract   属性为EF-实体?是否   微软希望我们能够利用这些   无处不在的对象在我们的   应用程序?或者,这只是针对极   简单的应用程序和快速   发展?

一般来说,这是一个坏主意,暴露你的EF-实体在服务层,因为,几乎没有将你的服务层和模型再presentation。所以在模型中所做的任何更改两端直接影响你的服务,不是一个好主意。你也将不得不版本的服务层在某些时刻,所以避免暴露EF实体服务层。

  

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

您可以使用POCO对象,你的服务层内,从任何下面的层分离(见Automapper,覆盖实体DTO映射费用)。但你仍然可以使用数据层和业务层之间自动生成的EF-实体在你的架构。只是尽量不要依赖于外汇基金的产生域模型的具体特点,从数据层不同的其他层。缓解迁移到另一个ORM框架。

  

如果我已经有了自己的事业   实体,其中有一些方法中,并且它   应该被映射到POCO对象,上   这层我应该转换   POCO-目的是我的实体(例如,   我有持久层,业务   逻辑层,服务层(WCF)   presenter层(客户端,使用WCF),UI   层)?或者我不应该让这样的我   自己的实体?

服务层 http://msdn.microsoft.com/en-us/库/ ms978717.aspx 。你会用你的域模型透明服务器层中(持久性,业务,服务和presenter层)应用程序,并且会要求你一个DTO映射的唯一层是服务层,请参阅问题1。(此外,如果您正在使用的presenter层 - 尼斯里的ViewModels idea-您将需要使用波苏斯映射在presenter层也是如此)。

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天全站免登陆