We wrote the same architecture twice, once as ArchUnit tests and once as Sonargraph .arc files. Both catch the same violations. Only one of them tells a reader, human or AI, what the architecture actually is.
The experiment
To make the comparison concrete, we built a deliberately boring Spring Boot application: order-demo on GitHub. It has three domains (order, customer, product) and the same four layers in each domain. The rules are what you would write on a whiteboard in five minutes:
- Every domain has the layers Service → Controller → Persistence, plus Model.
- Every layer may use Model; otherwise only the layer directly beneath it.
- Order may use Customer and Product. Customer and Product use nothing else.
- A domain only exposes its Service and Model layers.
- Cross-domain calls go from the caller’s Controller to the target’s Service.
Five sentences. The interesting question is: what does it take to turn those five sentences into something a machine can check, and what does the result look like to the next person (or agent) who has to read it?
Version 1: ArchUnit
ArchUnit is a good library, and for many teams the obvious choice: it is a test dependency, it runs in mvn test, and it needs no extra tool. But ArchUnit has a flat world view. It knows packages and classes, not “a domain that contains layers”, and it has no notion of a part of a component being visible to the outside while another part is hidden.
So the two-dimensional structure (domain × layer) has to be flattened. Our first attempt uses one layered architecture per domain plus hand-written rules for everything that crosses domains. It ends up with ten rules, for example:
private static LayeredArchitecture layersOf(String domain) {
return layeredArchitecture()
.consideringOnlyDependenciesInLayers()
.layer("Service").definedBy(domain + ".service..")
.layer("Controller").definedBy(domain + ".controller..")
.layer("Persistence").definedBy(domain + ".persistence..")
.layer("Model").definedBy(domain + ".model..")
.whereLayer("Service").mayOnlyAccessLayers("Controller", "Model")
.whereLayer("Controller").mayOnlyAccessLayers("Persistence", "Model")
.whereLayer("Persistence").mayOnlyAccessLayers("Model")
.whereLayer("Model").mayNotAccessAnyLayer();
}
@ArchTest
static final ArchRule foreign_services_are_only_used_by_controllers =
noClasses().that().resideInAnyPackage(
ORDER + ".service..", ORDER + ".persistence..", ORDER + ".model..")
.should().dependOnClassesThat().resideInAnyPackage(
CUSTOMER + ".service..", PRODUCT + ".service..");
Each rule is fine on its own. The problems appear between the rules:
consideringOnlyDependenciesInLayers()is required, otherwise the legitimate Order → Customer dependencies are reported as violations. But it also makes the per-domain rules blind to everything that crosses domains, so rule 5 needs a separate rule mixing both dimensions.- “Only Service and Model are visible” can only be phrased negatively: nobody outside Customer may touch Customer’s controller or persistence. Add a fifth layer next year and it is silently public.
- Domain rules are pairwise. Every new domain needs rules against every existing one.
- A package pattern that matches nothing doesn’t fail. A class in a package that no rule mentions (say
order.util) is simply not checked. We needed an extra rule that guards the rules.
The second attempt, one big layered architecture with twelve layers (domain × layer) and an access list per layer, is about as short as the Sonargraph version. But read it and try to find the sentence “Order may use Customer”. It isn’t there. It only emerges from twelve access lists, and you have to reconstruct it in your head.
Version 2: the Sonargraph architecture DSL
Here is the complete architecture as .arc files. First the layer pattern, written once:
// layering.arc
exposed strict artifact Service { include "**/service/**" }
hidden strict artifact Controller { include "**/controller/**" }
hidden artifact Persistence { include "**/persistence/**" }
exposed public artifact Model { include "**/model/**" }
Then the domains and how they may use each other:
// domains.arc
connection-scheme DomainToDomain
{
connect Service to target.Model
connect Controller to target.Service, target.Model
connect Persistence to target.Model
connect Model to target.Model
}
artifact Order
{
include "order-demo/com/example/demo/order/**"
apply "layering"
connect to Customer, Product using DomainToDomain
}
artifact Customer
{
include "order-demo/com/example/demo/customer/**"
apply "layering"
}
artifact Product
{
include "order-demo/com/example/demo/product/**"
apply "layering"
}
Map that back to the five rules:
| Rule | Where it lives in the .arc files |
|---|---|
| 1. Same layers in every domain | apply "layering" |
| 2. Only the next layer down, Model for all | strict and public |
| 3. Order uses Customer and Product, nothing else | connect to Customer, Product; no connect elsewhere |
| 4. Only Service and Model are visible | exposed and hidden |
| 5. Cross-domain calls start in the Controller | the DomainToDomain scheme |
Every rule has exactly one place, and every place states one rule. That is the whole difference.
Connection schemes: the part that scales
With three domains, the DomainToDomain scheme saves a handful of lines. Its real value shows when the system grows. Picture a code base with thirty domains instead of three, each with the same four layers, and each using a few of the others.
Without a scheme, every relationship between two domains has to spell out which layer may talk to which: Controller to the other domain’s Service and Model, every other layer to its Model only. Thirty domains with five neighbours each make 150 relationships, and each of them repeats the same four lines. That is exactly what happens in ArchUnit: the flat variant grows to 120 layers, each with an access list that names the target layers of every domain it may use, and the rule-based variant needs its visibility and “foreign services only from the controller” rules for every domain.
A connection scheme separates the what from the how.
What depends on what is one line per domain, and reads like an adjacency list of the system:
artifact Billing
{
include "**/billing/**"
apply "layering"
connect to Customer, Order, Pricing, Tax using DomainToDomain
}
How two domains may be wired is defined once, in the scheme. If the policy changes, say Persistence should no longer see foreign models, you change one line in the scheme, and it applies to all 150 relationships at once.
Different kinds of relationships simply get their own scheme. A shared kernel that only exposes its model could be connect to SharedKernel using DomainToSharedKernel, with a scheme that only allows access to target.Model. And if even the domain artifacts are too repetitive to write by hand, templates can generate them from the package structure.
The architecture of a thirty-domain system then fits on a page: one layering aspect, two or three schemes, and one short artifact per domain. That page is something a human can review in a meeting, and an agent can load before every change.
Why this matters for humans
The architecture is readable as a document. A new team member, a reviewer or the architect who designed it can open domains.arc and see the system: three domains, one allowed direction, one layering pattern. No Java, no fluent API, no mental execution of predicates.
Architecture changes become reviewable. When someone adds a domain, the diff is connect to Customer, Product, Billing using DomainToDomain. In a pull request that line is the design decision, and it is easy to discuss. The ArchUnit equivalent is a set of edits spread over several rules, and the reviewer has to work out what changed in the architecture from what changed in the code.
Intent survives growth. Adding a domain is one artifact and one connect line. Adding a fifth layer is one line in layering.arc, hidden by default. The rules don’t multiply; they are applied.
Gaps are visible. Every component is either assigned to an artifact or reported as unassigned. In our demo the Spring Boot application class showed up as unassigned. The fix was a five-line App artifact. In ArchUnit, the same class was handled by excluding it by name, which is exactly the kind of exception nobody revisits.
Why this matters even more for AI coding agents
Everything above applies to agents too, but agents add a few twists.
1. Context is the scarce resource. An agent that is about to change code needs to know the rules of the codebase. The two .arc files are about 2 KB. The ArchUnit tests are about 13 KB of Java, and they don’t contain the architecture; they contain checks the agent has to interpret, including the subtle interaction of consideringOnlyDependenciesInLayers() with the cross-domain rules. A compact, declarative spec is cheaper to load and much harder to misread.
2. Green means green. Agents trust passing tests, and that is the right instinct. But an architecture test that silently checks nothing (a mistyped package pattern, a new package no rule mentions) is also green. A model where every component is assigned or reported doesn’t have that failure mode.
3. Ask before you write. Tests answer one question after the fact: did the build break? A model can answer questions before code is written: “May OrderServiceImpl depend on CustomerService?” With Zügel, the agent gets a direct answer with the artifacts involved:
OrderServiceImpl → CustomerService: DENIED_BY_RULES
from Order.Service (default connector) to Customer.Service (default interface)
That answer names the rule’s vocabulary (Order.Service, Customer.Service), not just two class names, which is what the agent needs to find the legal alternative: go through the controller.
4. Specs are easier to protect than tests. An agent under pressure to make the build green may “fix” a failing architecture test by loosening it. That edit hides among other test changes. A separate .arc file is a different kind of artifact: you can tell the agent never to edit it to make a violation go away, put it under CODEOWNERS, and spot any change to it immediately.
5. Agents can write it, and humans can check it. This is not a thought experiment: Claude Code, Anthropic’s coding agent, wrote both .arc files in the repository. It got the five rules above, read the documentation page on connection schemes and looked at .arc files from our other projects. Then it drafted the model, loaded it into Zügel and probed it: 0 violations, and every dependency it asked about got the expected verdict, allowed or denied. When we noticed that the application class was unassigned, it added the App artifact in one step.
Claude wrote the ArchUnit tests as well, and the difference showed. To trust them, it had to inject deliberate violations into the code and check that each one was reported, and it found that the flat variant misses a new package entirely. The .arc model needed no such detour: reviewing 44 lines of declarations took minutes, while reviewing generated test code means hunting for subtle gaps, which is exactly where humans and agents are both weakest.
6. One language for all languages. The same DSL describes Java, C# and Python code bases. ArchUnit is Java-only; its .NET port has its own API.
Where ArchUnit still fits
To be fair: ArchUnit is great for code-level conventions that are not structure, such as naming rules, “classes annotated with @Entity live in ..model..“, or “no field injection”. It needs no extra tool and runs wherever your tests run. If your architecture is a single layered stack, a layeredArchitecture() rule is perfectly readable.
The trouble starts when the architecture has structure: components that contain components, parts that are visible and parts that aren’t, patterns that repeat. That’s where a DSL designed for architecture says in one line what a test library needs a rule system to approximate.
Try it yourself
- The full comparison: github.com/hello2morrow/order-demo (
ArchitectureTest,FlatLayeredArchitectureTestandOrderDemo.sonargraph/Architecture). - Getting started with Zügel: First steps with Zügel.