摆脱WCF数据服务的紧耦合

发布于 2024-11-02 12:08:31 字数 155 浏览 1 评论 0原文

我有一个 WCF 数据服务,它通过 HTTP 公开我的数据库。

我意识到,将来如果我的数据库发生变化,客户端也需要更新,这意味着我将数据库与客户端紧密耦合。

如何保留 WCF 数据服务的优点(它可以轻松地通过 http 公开数据,无需太多努力)并仍然具有低耦合性?

I have a WCF Data Service that exposes my database over HTTP.

I realized that in the future, if my database changes, the clients will need to be updated as well, meaning that I have tightly coupled my database with clients.

How do I keep the benefits of WCF Data Service (it can easily expose data over http without much effort) and still have low-coupling ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何其悲哀 2024-11-09 12:08:31

计算机科学中的任何问题都可以通过添加间接层来解决

-大卫·惠勒

你应该将你的服务包装在一个抽象层中 - 我相信这称为服务代理模式。

然后,您的所有客户端都将与代理进行交互,如果您的服务将来发生变化,您只需更改代理即可 - 当然,除非服务的工作方式发生根本性变化,在这种情况下,您必须更改您的代理客户,自然。

Any problem in computer science can be solved by adding a layer of indirection

-David Wheeler

You should wrap your service in a layer of abstraction—I believe this is called the service proxy pattern.

Then, all your clients would interact with the proxy, and if your service changes in the future, you just have to change the proxy—unless of course something fundamental changes in the way the service works, in which case you would have to change your client, naturally.

云醉月微眠 2024-11-09 12:08:31

我从未使用过 WCF,因此这个答案可能不适用于该技术。但是,您不能将所有数据库逻辑封装在与客户端交互的方法或类中吗?然后,如果出现新数据库,您可以在方法/类内更改逻辑,只要公共契约相同,那么您就不必更新客户端代码。

例如:

class Client
{

     DatabaseClass DC = new DatabaseClass();
     DC.PerformMethod(); //Blissfully unaware of the methods inner workings.

 }

class DatabaseClass
{

    public void PerformMethod()
    {
        //Encapsulate DB Logic here.  If you need to change it, you can just change it here and the client needs to know nothing of it
     }

 }

I've never used WCF, so this answer may not apply to this technology. But can you not encapsulate all DB logic in a method or class that your clients interact with? You can then change the logic in the event of a new DB, within the method/class and as long as the public contract is the same, then you should not have to update the client code.

For example:

class Client
{

     DatabaseClass DC = new DatabaseClass();
     DC.PerformMethod(); //Blissfully unaware of the methods inner workings.

 }

class DatabaseClass
{

    public void PerformMethod()
    {
        //Encapsulate DB Logic here.  If you need to change it, you can just change it here and the client needs to know nothing of it
     }

 }
你对谁都笑 2024-11-09 12:08:31

除了另一条评论中提到的抽象级别之外 - 您应该使用存储库。

http://martinfowler.com/eaaCatalog/repository.html

In addition to the level of abstraction mentioned in another comment - you should be using Repositories.

http://martinfowler.com/eaaCatalog/repository.html

痕至 2024-11-09 12:08:31

如果您使用实体框架通过 WCF 数据服务公开数据库,那么 EF 就是您的间接级别。它允许您更改数据库架构并仍然保持相同的模型(这是 WCF 数据服务公开的)。

If you're using Entity Framework to expose your DB through WCF Data Services, than the EF is your level of indirection. It allows you change your DB schema and still keep the same model (Which is what WCF Data Services exposes).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文