当底层数据库有关系时,系统使用来自另一个系统的 WCF 服务 [英] System consuming WCF services from another system, when underlying databases have relationships

查看:19
本文介绍了当底层数据库有关系时,系统使用来自另一个系统的 WCF 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在许多系统中都遇到过的一个问题,但这是一个很好的例子.与一个系统何时消费另一个系统的WCF服务有关,每个系统都有自己的数据库,但两个数据库之间存在关系.

This is an issue that I have struggled with in a number of systems but this one is a good example. It is to do with when one system consumes WCF services from another system, and each system has their own database, but there are relationships between the two databases.

我们有一个中央数据库,用于保存公司所有文件的记录.该数据库包括文档和文件夹表,它模仿 Windows 文件结构.NHibernate 负责数据访问,域层处理逻辑(验证文件名/同一文件夹中没有相同的文件名等),服务层位于其上,服务名为CreateDocument(bytes[])"、RenameDocument(id)", newName) ', 'SearchDocuments(filename, filesize, createdDate)' 等.这些服务通过 WCF 公开.

We have a central database that holds a record of all documents in the company. This database includes Document and Folder tables and it mimicks a windows file structure. NHibernate takes care of data access, a domain layer handles logic (validating filenames/no identical filenames in the same folder etc.) and a service layer sits on that, with services named 'CreateDocument(bytes[])', 'RenameDocument(id, newName) ', 'SearchDocuments(filename, filesize, createdDate)' etc. These services are exposed with WCF.

人力资源系统使用这些服务.HR 数据库有一个单独的数据库,该数据库具有 Document 数据库的外键:它包含一个 HRDocument 表,该表具有一个外键 DocumentId,然后是 HR 特定的,例如 EmployeeId 和 ContractId.

An HR system consumes these services. The HR database has a separate database that has foreign keys to the Document database: it contains an HRDocument table that has a foreign key DocumentId, and then HR specific such as EmployeeId and ContractId.

以下是其他人的问题:

1) 为了保存文档,我必须调用 WCF 服务将其保存到中央数据库,返回 ID,然后保存到 HRDocument 表(连同 HR 特定信息).由于 WCF 调用和所有特定于 Document 的数据访问都在 Document 应用程序中完成,因此无法在一个事务中完成所有这些操作,从而可能会丢失事务完整性.

1) In order to save a document, I have to call the WCF service to save it to the central db, return the ID and then save to the HRDocument table (along with the HR specific information). Because of the WCF call and all Document specific data access being done within the Document application, this can't be done all within one transaction, resulting in a possible loss of transaction integrity.

2) 为了搜索例如employeeId 和createdDate,我必须调用传入createdDate(文档数据库特定字段)的搜索服务,然后在返回记录的Id 上搜索HRDocument 数据库以过滤结果回来.这让人感觉混乱、缓慢而且是错误的.

2) In order to search on say, employeeId and createdDate, I have to call the search service passing in the createdDate (Document database specific fields) and then search the HRDocument database on the Id's of the returned records to filter the results returned. This feels messy, slow and just wrong.

我可以将 NHibernate 映射文件复制到 HR 应用程序 DAL 中的文档数据库中.这意味着我可以指定 HRDocument 和 Document 之间的关系.这意味着我可以加入表格并像这样搜索,但也意味着我将不得不复制域逻辑并违反 DRY 原则,以及所有相关的内容.

I could duplicate the NHibernate mapping files to the Document database in the DAL of the HR application. This means I could specify the relationship between HRDocument and Document. This means I could join the tables and search like that but would also mean I would have to duplicate domain logic and violate the DRY principle, and all that entails.

我不禁觉得我在这里做错了,错过了一些简单的事情.

I can't help feeling I'm doing something wrong here and have missed something simple.

推荐答案

我建议您申请 CQRS 和此处为事件驱动架构原则

I recommend you to apply CQRS and Event Driven Architecture principles here

    • 使用 Guids 作为主键 - 那么你将能够生成主键用于文档并将其传递给 WCF
      方法调用.
    • 在 WCF 服务的另一端使用消息传递来防止数据丢失(在数据库故障和
      类似的东西).
    • 删除数据库之间的约束 - 立即一致的 应用程序不规模.使用最终一致性代替范式.
    • Use Guids as primary keys - then you will be able to generate primary key for document and pass it to WCF
      method call.
    • Use messaging on other side of WCF service to prevent data loss (in case of database failure and
      something like that).
    • Remove constaints between databases - immediate consistent applications don't scale. Use eventual consistency paradigm instead.

引入单独的数据存储读取包含非规范化数据的目的.然后你就可以做搜索很容易.确保读取存储的一致性(在Document 创建时的情况失败)你可以实现一些简单的工作流程(传奇CQRS)

Introduce separate data storage for reads purpose that contains denormalized data. Then you will be able to do search very easy. To ensure consistency in your read storage (in case when Document creation failed) you could implement some simple workflow (saga in terms of CQRS)

这篇关于当底层数据库有关系时,系统使用来自另一个系统的 WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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