英文:
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(...);
如果从C
到E
存在直接依赖关系,则会检测到依赖关系:
E entity = new E();
但如果从C
到Optional<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("Controller").definedBy(controllerPredicate)
.layer("Service").definedBy(servicePredicate)
.layer("Entity").definedBy(entityPredicate)
.whereLayer("Controller").mayOnlyAccessLayers("Service")
.whereLayer("Entity").mayOnlyBeAccessedByLayers("Service")
.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<E>
:
Optional<E> 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论