如何在不耦合数据库结构的情况下设置OData和EF? [英] How can I setup OData and EF with out coupling to my database structure?

查看:262
本文介绍了如何在不耦合数据库结构的情况下设置OData和EF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢OData(WCF数据服务)。在过去的项目中,我已经编写了许多Web服务,只是为了允许使用不同的方式读取我的数据。

I really like OData (WCF Data Services). In past projects I have coded up so many Web-Services just to allow different ways to read my data.

OData为客户提供了极大的灵活性,使他们可以在他们拥有数据时

OData gives great flexibility for the clients to have the data as they need it.

但是,在今天的讨论中,一位同事指出,我们如何进行OData仅仅是赋予客户端应用程序与数据库的连接而已。 。

However, in a discussion today, a co-worker pointed out that how we are doing OData is little more than giving the client application a connection to the database.

这是我们设置WCF数据服务的方式(注意:这是传统方式)

Here is how we are setting up our WCF Data Service (Note: this is the traditional way)


  1. 创建数据库的实体框架(E)F数据模型

  2. 使用WCF数据服务发布该模型

  3. 为OData feed添加安全性

    (这比直接连接到SQL Server更好)

我的同事(正确)指出,我们所有的客户现在都将耦合到数据库。 (如果对表或列进行了重构,那么客户端也必须进行更改)。

My co-worker (correctly) pointed out that all our clients will be coupled to the database now. (If a table or column is refactored then the clients will have to change too)

EF在显示和隐藏数据方面提供了一些灵活性。不影响客户端应用程序的一些较小的数据库更改。但是我发现它非常有限。 (请参阅这篇例子)我发现POCO模板(虽然很好地允许分离模型和实体)也没有提供很大的灵活性。

EF offers a bit of flexibility on how your data is presented and could be used to hide some minor database changes that don't affect the client apps. But I have found it to be quite limited. (See this post for an example) I have found that the POCO templates (while nice for allowing separation of the model and the entities) also does not offer very much flexibility.

那么,问题是:我该如何告诉我的同事?如何设置WCF数据服务,使其使用面向业务的合同(就像每个读取操作都使用基于WCF Soap的标准服务一样)?

So, the question: What do I tell my co-worker? How do I setup my WCF Data Services so they are using business oriented contracts (like they would be if every read operation used a standard WCF Soap based service)?

请明确一点,让我以不同的方式提出这个问题。如何将EF与WCF数据服务分离。我可以自己订立合同,并使用AutoMapper在它们之间转换。但是我不想直接从EF转到OData。

Just to be clear, let me ask this a different way. How can I decouple EF from WCF Data Services. I am fine to make up my own contracts and use AutoMapper to convert between them. But I would like to not go directly from EF to OData.

注意:我仍然想将EF用作我的ORM。滚动我自己的ORM并不是真正的解决方案...

NOTE: I still want to use EF as my ORM. Rolling my own ORM is not really a solution...

推荐答案

如果您使用自定义类而不是使用直接由类生成的类如果您还将更改WCF数据服务的提供。这意味着您将不再将EF上下文作为通用参数传递给 DataService 基类。如果您拥有只读服务,这将是可以的,但是一旦您希望客户端进行任何数据修改,您将需要做很多工作。

If you use your custom classes instead of using classes generated directly by EF you will also change a provide for WCF Data Services. It means you will no more pass EF context as generic parameter to DataService base class. This will be OK if you have read only services but once you expect any data modifications from clients you will have a lot of work to do.

基于EF上下文的数据服务支持数据修改。所有其他数据服务都使用反射提供程序,默认情况下该只读状态直到您在您的自定义服务上下文类上实现 IUpdatable

Data services based on EF context supports data modifications. All other data services use reflection provider which is read only by default until you implement IUpdatable on your custom "service context class".

数据服务是一种用于快速创建公开数据的服务的技术。它们与上下文结合在一起,提供抽象是上下文的责任。如果要提供快速简便的服务,则取决于EF映射支持的功能。您可以在EDMX中进行一些抽象,可以进行投影(DefiningQuery,QueryView等),但是所有这些功能都有一些局限性(例如,投影是只读的,除非您使用存储过程进行修改)。

Data services are technology for creating quickly services exposing your data. They are coupled with their context and it is responsibility of the context to provide abstraction. If you want to make quick and easy services you are dependent on features supported by EF mapping. You can make some abstractions in EDMX, you can make projections (DefiningQuery, QueryView) etc. but all these features have some limitations (for example projections are readonly unless you use stored procedures for modifications).

数据服务与提供与数据库的连接不同。有一个非常大的区别-与数据库的连接将仅确保访问和执行权限,但不能确保数据安全。 WCF数据服务提供数据安全性,因为您可以创建拦截器,该拦截器将向查询添加筛选器以仅检索允许用户查看的数据或检查是否允许用户修改数据。那就是您可以告诉您的同事的区别。

Data services are not the same as providing connection to database. There is one very big difference - connection to database will ensure only access and execution permissions but it will not ensure data security. WCF Data Services offer data security because you can create interceptors which will add filters to queries to retrieve only data the user is allowed to see or check if he is allowed to modify the data. That is the difference you can tell your colleague.

在抽象的情况下,您是否想要快速简便的解决方案?您可以在服务和ORM之间注入抽象层,但是您需要实现所提到的方法,并且必须对其进行测试。

In case of abstraction - do you want a quick easy solution or not? You can inject abstraction layer between service and ORM but you need to implement mentioned method and you have to test it.

这篇关于如何在不耦合数据库结构的情况下设置OData和EF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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