当底层数据库存在关系时,系统使用来自另一个系统的 WCF 服务

发布于 2024-11-15 20:25:37 字数 933 浏览 1 评论 0原文

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

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

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

以下是其他问题:

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

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

我可以将 NHibernate 映射文件复制到 HR 应用程序 DAL 中的文档数据库。这意味着我可以指定 HRDocument 和 Document 之间的关系。这意味着我可以像这样连接表和搜索,但也意味着我必须重复域逻辑并违反 DRY 原则,以及所有这些。

我不禁觉得我在这里做错了什么并且错过了一些简单的事情。

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.

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.

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.

Here are the problems amonst others:

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) 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.

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.

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

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

发布评论

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

评论(2

合约呢 2024-11-22 20:25:37

我建议您在此处应用 CQRS 和事件驱动架构原则

    • 使用 Guid 作为主键 - 然后你
      将能够生成主键
      获取文档并将其传递给 WCF
      方法调用。
    • 在 WCF 服务的另一端使用消息传递以防止数据丢失
      (如果数据库出现故障并且
      类似的东西)。
    • 消除数据库之间的约束 - 立即
      一致
      应用程序不
      规模。使用最终一致性
      相反,范式。
  1. 引入单独的数据存储
    读取包含非规范化数据的目的。然后你将能够
    进行搜索非常容易。为确保
    读取存储的一致性(在
    创建文档时的情况
    失败)你可以实施一些
    简单的工作流程(

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

    • 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.
  1. 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)

掩耳倾听 2024-11-22 20:25:37

您可以创建一个通用代码库,其中包括文档的基本实现以及所有映射、基本域模型等。

文档服务和 HR 系统使用相同的代码库。但在 HR 系统中,您可以使用继承映射策略使用 HRDocument 扩展基文档类(或多个类),这将最适合您的需求。

public class HRDocument : Document 

从 HR 系统中,您甚至不必再调用文档服务,您只需使用 NH 并享受 ACID 等。但文档服务仍然存在,并且没有代码重复。

You can create a common codebase which will include base implementation of Document along with all the mappings, base Domain Model etc.

A Document Service and an HR System use the same codebase. But in HR System you extend base Document class (or classes) with your HRDocument using inheritance mapping strategy which will suit your needs the best.

public class HRDocument : Document 

And from HR System you don't even have to call Document Service anymore, you just use NH and enjoy ACID and all that. But Document Service is still there and there's no code duplication.

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