ArchUnit不检测通用的禁止依赖关系。

huangapple go评论79阅读模式
英文:

ArchUnit doesn't detect generic forbidden dependencies

问题

我正在使用ArchUnit来检测从控制器(C)到实体(E)的禁止依赖关系。

示例:

layeredArchitecture()
    .consideringOnlyDependenciesInLayers()
    .layer("Controller").definedBy(controllerPredicate)
    .layer("Service").definedBy(servicePredicate)
    .layer("Entity").definedBy(entityPredicate)

    .whereLayer("Controller").mayOnlyAccessLayers("Service")
    .whereLayer("Entity").mayOnlyBeAccessedByLayers("Service")
    .check(...);

如果从CE存在直接依赖关系,则会检测到依赖关系:

E entity = new E();

但如果从COptional<E>存在依赖关系,则不会检测到:

Optional<E> optionalEntity = service.returningAnOptionalEntity();

我正在使用版本1.0.1。我在这里看到泛型应该从0.0.20开始受支持,但似乎不起作用。

有办法检测这些泛型依赖关系吗?

英文:

I'm using ArchUnit to detect forbidden dependencies from the controller (C) to the entity (E).

Example :

layeredArchitecture()
    .consideringOnlyDependenciesInLayers()
    .layer(&quot;Controller&quot;).definedBy(controllerPredicate)
    .layer(&quot;Service&quot;).definedBy(servicePredicate)
    .layer(&quot;Entity&quot;).definedBy(entityPredicate)

    .whereLayer(&quot;Controller&quot;).mayOnlyAccessLayers(&quot;Service&quot;)
    .whereLayer(&quot;Entity&quot;).mayOnlyBeAccessedByLayers(&quot;Service&quot;)
    .check(...);

The dependency is detected if there is a direct dependency from C to E :

E entity = new E();

But it's not detected if there is a depenency from C to Optional&lt;E&gt; :

Optional&lt;E&gt; optionalEntity = service.returningAnOptionalEntity();

I'm using the 1.0.1 version. And I've seen here that generics should be supported since 0.0.20 but it doesn't seem to.

Any way to detect these generic dependencies ?

答案1

得分: 1

除非您在控制器中使用实体的特定API(即调用方法或访问字段),ArchUnit无法检测到依赖关系(还请参阅ArchUnit#1012,该问题基本上描述了相同的用例)。

这与泛型无关;即使您的服务返回非Optional实体E,也不会出现违规情况。

英文:

Unless you use a specific API of your entity (i.e. call a method or access a field) in your controller, ArchUnit cannot detect a dependency (see also ArchUnit#1012, which basically describes the same use case).

This is unrelated to generics; you wouldn't get a violation even if your service returned a non-Optional entity E.

huangapple
  • 本文由 发表于 2023年6月22日 18:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定