26 Temmuz 2023 Çarşamba

ArchUnit Layered Architecture Enforcement Kullanımı

Giriş
Açıklaması şöyle
In layered architectures, we define different layers and how those interact with each other.
Örnek
Şöyle yaparız
@ArchTest
static final ArchRule persistence_should_not_access_services =
  noClasses()
  .that()
  .resideInAPackage("..persistence..")
  .should()
  .accessClassesThat()
  .resideInAPackage("..service..");

@ArchTest
static final ArchRule services_should_only_be_accessed_by_controllers_or_other_services =
  classes()
  .that()
  .resideInAPackage("..service..")
  .should()
  .onlyBeAccessed()
  .byAnyPackage("..controller..", "..service..");
Örnek
Şöyle yaparız
@ArchTest
static final ArchRule layer_dependencies_are_respected_with_exception =
  layeredArchitecture().consideringAllDependencies()
  .layer("Controllers").definedBy("com.tngtech.archunit.example.layers.controller..")
  .layer("Services").definedBy("com.tngtech.archunit.example.layers.service..")
  .layer("Persistence").definedBy("com.tngtech.archunit.example.layers.persistence..")
  
  .whereLayer("Controllers").mayNotBeAccessedByAnyLayer()
  .whereLayer("Services").mayOnlyBeAccessedByLayers("Controllers")
  .whereLayer("Persistence").mayOnlyBeAccessedByLayers("Services")
  .ignoreDependency(SomeMediator.class, ServiceViolatingLayerRules.class);
Örnek
Şöyle yaparız
@Test
public void testThatHexagonalLayerIsRespected() {
  JavaClasses classes = new ClassFileImporter()
    .importPackages("com.modern.app");

  layeredArchitecture()
    .consideringAllDependencies()
    .layer("application").definedBy("..application..")
    .layer("domain").definedBy("..domain..")
    .layer("infrastructure").definedBy("..infrastructure..")
    
    .whereLayer("domain").mayOnlyBeAccessedByLayers("application", "infrastructure")
    .whereLayer("application").mayOnlyBeAccessedByLayers("infrastructure")
    .whereLayer("infrastructure").mayNotBeAccessedByAnyLayer()
    .check(classes);
}

Hiç yorum yok:

Yorum Gönder