【问题标题】:Which one of these two options is the best for implementing a port in the domain这两个选项中的哪一个最适合在域中实现端口
【发布时间】:2020-07-18 22:21:13
【问题描述】:

我有一个Document 类,需要在保存到数据库之前获得外部授权。

可以创建authorize这样的方法吗...

class Document:
    authorize(IExternalAuthorizator authorizator):
        authorization_result = authorizator.authorize(this)
        // update internal state with result...

然后在类似的用例或服务中使用它...

class UseCase:
    execute(IDocumentRepository repo, IExternalAuthorizator authorizator):
        doc = new Document()
        doc.authorize(authorizator)
        repo.save(doc)

或者我应该有这样的Document 类...

class Document:
    authorize(AuthorizationResult result):
        // update internal state with result...

然后是用例……

class UseCase:
    execute(IDocumentRepository repo, IExternalAuthorizator authorizator):
        doc = new Document()
        result = authorizator.authorize(doc)
        doc.authorize(result)
        repo.save(doc)

还是没有,只有第三种选择?

有什么帮助吗?

【问题讨论】:

    标签: domain-driven-design clean-architecture hexagonal-architecture


    【解决方案1】:

    基于我自己对文献的研究

    • 第二种形式更好
    • 两者差别不大。

    您的域模型本质上是一个状态机:它有自己的一些信息,新信息到达,模型决定如何将新信息与它已经知道的信息集成。

    当我们需要一些额外的信息来让模型继续运行时,通常会引入额外的端口,而这些信息在其他地方

    其他地方的信息的问题在于,在我们想要的那一刻,信息可能不可用(也许授权无法升级)。

    所以一个合乎逻辑的问题是:这些接口中的哪一个更适合异步信息交换?

    当然,如果我们努力将域模型与持久性解决方案隔离以避免代码混乱,那么出于同样的原因,我们还应该将域模型与异步消息检索隔离开来。该代码是管道世界的一部分,与我们编写域计算时完全无关。

    实际上,让应用程序担心与 Authorization 的协商意味着,当授权不可用时,应用程序可以继续处理其他工作,并在授权数据可用时返回此特定文档。域模型本身没有(也不应该)有这样做的上下文。

    以不同的方式表达相同的想法,领域模型的“单一职责”是记账,而不是信息检索。

    还要注意这两种方法不一定有太大的不同:

    class Document:
        authorize(IExternalAuthorizator authorizator):
            authorization_result = authorizator.authorize(this)
            this.authorize(authorization_result)
    
        authorize(AuthorizationResult result):
            // update internal state with result...
    

    class UseCase:
        execute(IDocumentRepository repo, IExternalAuthorizator authorizator):
            doc = new Document()
            authorize(doc, authorizator)
            repo.save(doc)
    
        authorize(Document doc, IExternalAuthorizator authorizator):
            authorization_result = authorizator.authorize(doc)
            doc.authorize(authorization_result)
    

    简而言之:在这两种情况下都是一样的除了对于小数据检索部分。当数据检索微不足道时,这不会产生影响,但随着越来越多的管道问题被引入,它会产生影响。

    【讨论】:

      猜你喜欢
      • 2022-08-22
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多