如何区分分层架构中的使用和继承?

How to differentiate usage and inheritance in a layered architecture?

我正在尝试验证以下依赖项:

Source

本图中,禁止红色关系,允许绿色关系:

如果可能,我想将这些验证添加到分层架构中。

谢谢。

LayeredArchitecture considers all dependencies层之间。您不能禁止继承,但允许访问——反之亦然。我建议改为定义个别特定规则:

@ArchTest
ArchRule adapter_should_not_inherit_from_port = noClasses()
    .that().resideInAPackage("….adapter")
    .should().beAssignableTo(JavaClass.Predicates.resideInAPackage("….port"));

@ArchTest
ArchRule service_should_not_access_port = noClasses()
    .that().resideInAPackage("….service")
    .should().accessClassesThat().resideInAPackage("….port");