摆脱WCF数据服务的紧耦合
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你应该将你的服务包装在一个抽象层中 - 我相信这称为服务代理模式。
然后,您的所有客户端都将与代理进行交互,如果您的服务将来发生变化,您只需更改代理即可 - 当然,除非服务的工作方式发生根本性变化,在这种情况下,您必须更改您的代理客户,自然。
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.
我从未使用过 WCF,因此这个答案可能不适用于该技术。但是,您不能将所有数据库逻辑封装在与客户端交互的方法或类中吗?然后,如果出现新数据库,您可以在方法/类内更改逻辑,只要公共契约相同,那么您就不必更新客户端代码。
例如:
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:
除了另一条评论中提到的抽象级别之外 - 您应该使用存储库。
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
如果您使用实体框架通过 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).