Roslyn analysers for enforcing architectural rules in C# projects
$ dotnet add package ArchonAnalysersRoslyn analysers for enforcing architectural rules in C# projects.
dotnet add package ArchonAnalysers
ArchonAnalysers provides Roslyn analysers that enforce namespace-based architectural rules:
Ensures that all types within namespaces containing .Internal are properly restricted with internal or private access modifiers. This prevents accidental exposure of internal implementation details.
*.Internal* (e.g., MyApp.Internal, MyApp.Services.Internal)internal, private, private protectedinternal or privateEnsures that top-level types within namespaces containing .Public are appropriately exposed with public or protected access modifiers. This enforces discoverability of your public API surface.
*.Public* (e.g., MyApp.Public, MyApp.Api.Public)public, protected, protected internalPrevents specified projects from referencing forbidden assemblies at compile time, enforcing architectural layering rules.
archon_003.forbidden_references (directional rules: Source->Target)ARCHON003 provides a code fix that automatically removes forbidden <ProjectReference> entries from .csproj files.
Limitations:
.csproj with <Project Sdk="...">)<ProjectReference> elements (not <PackageReference> or <Reference>)To use: Right-click the diagnostic in the Error List and select "Remove project reference to [AssemblyName]"
Once installed, the analysers will automatically run during compilation and highlight violations in your IDE.
Both analysers support customising which namespace patterns trigger the rules via .editorconfig:
[*.cs]
# Single namespace slug (default: Internal)
archon_001.internal_namespace_slugs = Internal
# Multiple namespace slugs
archon_001.internal_namespace_slugs = Internal, Private, Hidden, Impl
Types in namespaces matching *.Internal.*, *.Private.*, *.Hidden.*, or *.Impl.* must be internal, private, or private protected.
[*.cs]
# Single namespace slug (default: Public)
archon_002.public_namespace_slugs = Public
# Multiple namespace slugs
archon_002.public_namespace_slugs = Public, Api, Exposed, Contract
Top-level types in namespaces matching *.Public.*, *.Api.*, *.Exposed.*, or *.Contract.* must be public, protected, or protected internal.
Notes:
App.Internal.Services but not App.InternalStuff)Configure directional rules in a single global EditorConfig file:
# Single global .editorconfig at solution root
[*.cs]
archon_003.forbidden_references = Contracts->Domain, Contracts->Application, Domain->Application
This enforces:
Directional format:
archon_003.forbidden_references = Source->Target, AnotherSource->AnotherTarget
Notes:
.dll extension is optional in configuration-> is automatically trimmedSource->Target formatConfigure severity levels in your .editorconfig:
[*.cs]
# Enforce internal types in configured namespaces (default: error)
dotnet_diagnostic.ARCHON001.severity = error
# Enforce public types in configured namespaces (default: warning)
dotnet_diagnostic.ARCHON002.severity = warning
# Enforce forbidden assembly references (default: error)
dotnet_diagnostic.ARCHON003.severity = error
namespace MyApp.Internal
{
// ✅ Correct - internal type in .Internal namespace
internal class InternalService { }
// ❌ ARCHON001 violation - public type in .Internal namespace
public class PublicService { }
}
namespace MyApp.Public
{
// ✅ Correct - public type in .Public namespace
public class PublicApi { }
// ❌ ARCHON002 violation - internal type in .Public namespace
internal class InternalApi { }
}
act for local CI/CD testingcd src
dotnet restore Archon.slnx
dotnet build Archon.slnx
cd src/ArchonAnalysers.Tests.Unit/bin/Release/net10.0
dotnet ArchonAnalysers.Tests.Unit.dll
Test the CI pipeline locally:
./scripts/test-ci.sh
Test the CD pipeline locally:
./scripts/test-cd.sh
This project is licensed under the MIT Licence - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.