AutoArchitecture 1.2.0
dotnet add package AutoArchitecture --version 1.2.0
NuGet\Install-Package AutoArchitecture -Version 1.2.0
<PackageReference Include="AutoArchitecture" Version="1.2.0" />
<PackageVersion Include="AutoArchitecture" Version="1.2.0" />
<PackageReference Include="AutoArchitecture" />
paket add AutoArchitecture --version 1.2.0
#r "nuget: AutoArchitecture, 1.2.0"
#:package AutoArchitecture@1.2.0
#addin nuget:?package=AutoArchitecture&version=1.2.0
#tool nuget:?package=AutoArchitecture&version=1.2.0
AutoArchitecture
Free, MIT-licensed compile-time architecture rule enforcement for .NET. No commercial license, no separate desktop tool, no CI-only step — violations show up as build warnings/errors the moment they're introduced, right in your IDE.
Why AutoArchitecture?
Tools like NDepend are excellent for deep codebase analysis but require a paid commercial license. If all you need is "the UI layer must never reference the data-access layer directly," you shouldn't need a separate license and a separate analysis pass for that. AutoArchitecture is a Roslyn source generator and analyzer: declare your rules once as assembly attributes, and every build enforces them for free.
[assembly: AutoArchitecture.ForbidDependency("MyApp.UI", "MyApp.DataAccess",
Because = "UI must go through the service layer, not the repositories directly")]
The moment any type in MyApp.UI (or a sub-namespace like MyApp.UI.Views) references a
type in MyApp.DataAccess (or a sub-namespace), you get:
warning AA001: 'OrderController' in namespace 'MyApp.UI' references 'OrderRepository' in
namespace 'MyApp.DataAccess', which is forbidden by
[assembly: ForbidDependency("MyApp.UI", "MyApp.DataAccess")]
(UI must go through the service layer, not the repositories directly)
AutoArchitecture can also detect namespace cycles — the capability NDepend is best known for — with an opt-in assembly attribute:
[assembly: AutoArchitecture.DetectCircularDependencies]
Install
dotnet add package AutoArchitecture
Usage
Add one [assembly: ForbidDependency(from, to)] attribute per rule anywhere in your
project (e.g. in AssemblyInfo.cs or alongside your Program.cs):
[assembly: AutoArchitecture.ForbidDependency("MyApp.Presentation", "MyApp.Infrastructure")]
[assembly: AutoArchitecture.ForbidDependency("MyApp.Domain", "MyApp.Infrastructure")]
- Namespace matching includes sub-namespaces: a rule for
MyApp.DataAccessalso coversMyApp.DataAccess.Sql,MyApp.DataAccess.Migrations, etc. - Rules only match on exact/dot-boundary prefixes, so
MyApp.DataAccessdoes not accidentally match an unrelatedMyApp.DataAccessLegacynamespace. - The
Becausenamed argument is optional and is included verbatim in the diagnostic message — handy for explaining why the rule exists to whoever hits it next. - AA001 is a
Warningby default. Promote it to a build-breaking error per-project with a standard.editorconfigseverity override:dotnet_diagnostic.AA001.severity = error
Circular Dependency Detection
Add [assembly: DetectCircularDependencies] when you want AutoArchitecture to analyze the
entire compilation's namespace graph and flag actual dependency cycles:
[assembly: AutoArchitecture.DetectCircularDependencies]
namespace MyApp.A
{
public class AType
{
private readonly B.BType _dependency = new B.BType();
}
}
namespace MyApp.B
{
public class BType
{
private readonly C.CType _dependency = new C.CType();
}
}
namespace MyApp.C
{
public class CType
{
private readonly A.AType _dependency = new A.AType();
}
}
That produces warnings on the participating references, for example:
warning AA002: 'AType' in namespace 'MyApp.A' references 'BType' in namespace 'MyApp.B',
creating a circular dependency: MyApp.A -> MyApp.B -> MyApp.C -> MyApp.A
- Circular dependency detection is opt-in because it analyzes the full namespace dependency graph for the current compilation.
- AA002 is a
Warningby default and can be promoted/demoted with standard Roslyn severity configuration:dotnet_diagnostic.AA002.severity = error - AA001 and AA002 can be enabled together: explicit
ForbidDependencyrules still catch disallowed directions even when those references are also part of a cycle. - References between types in the same namespace are never reported as cycles (there is
no
MyApp.Auth -> MyApp.Auth"cycle"). - AA002 needs the whole compilation, so it is reported at compilation end: always in
dotnet build, and in the IDE when full solution analysis is enabled.
Razor components and generated code
AutoArchitecture analyses generated code, including the C# that the Razor SDK generates for
.razor components and .cshtml views, so a rule such as
ForbidDependency("MyApp.Web", "MyApp.DataAccess") applies to an @inject or @code
block in a component. Diagnostics are reported against the .razor file and line.
(Before 1.2.0 the rules ran inside the source generator. A source generator never sees the output of other generators, such as the Razor generator, so components were silently skipped. The rules now run in a regular analyzer, which sees the final compilation.)
Choose which generated files are analysed with an MSBuild property:
<PropertyGroup>
<AutoArchitectureGeneratedCode>razor</AutoArchitectureGeneratedCode>
</PropertyGroup>
or with an .editorconfig key (global or per file):
[*.cs]
autoarchitecture_generated_code = razor
all: analyse all code, generated or not. This is the default and matches how checked-in// <auto-generated/>files were already handled.razor: analyse hand-written code and Razor-generated code, and skip other generated code (other source generators,*.g.cs,*.designer.csand// <auto-generated/>files).none: analyse hand-written code only.
Multiple projects
The attribute types are generated into each project as internal types marked with
[Microsoft.CodeAnalysis.Embedded]. Several projects in one reference chain, such as an app
and its test project with InternalsVisibleTo, can all use AutoArchitecture without CS0436
type conflicts. This uses the Roslyn 4.14 API (AddEmbeddedAttributeDefinition), so it
needs the .NET 9.0.300 SDK / Visual Studio 17.14 or later.
Design goals
- MIT licensed, forever. No commercial tier, no per-seat fees, no desktop app to buy.
- Zero runtime cost — this is a source generator/analyzer; nothing ships in your compiled output.
- IDE-first feedback — violations appear as squiggles while you type, not just in a nightly CI report.
- Simple, declarative rules — no XML rule files or query languages to learn.
Roadmap
- Allow-list exceptions for specific types within an otherwise-forbidden namespace pair are planned for a future release, to support gradual migrations.
Related Packages
| Package | Downloads | Description |
|---|---|---|
| AutoWire | Compile-time dependency injection auto-registration for | |
| AutoMap.Generator | Compile-time object mapping for | |
| AutoQuery.Generator | Compile-time query composition for IQueryable using Roslyn incremental source generators | |
| AutoHttpClient.Generator | Compile-time typed HTTP client generation for | |
| AutoDispatch.Generator | Compile-time CQRS dispatcher for | |
| AutoLog.Generator | Compile-time high-performance logging for | |
| AutoValidate.Generator | Compile-time FluentValidation wiring for |
💼 Need .NET consulting?
I'm the author of AutoArchitecture and a suite of compile-time source generators (AutoWire, AutoMap.Generator) and 28+ Polly v8 resilience packages. I'm available for consulting on Polly v8 resilience, Azure cloud architecture, and clean .NET design.
→ solidqualitysolutions.com · LinkedIn
Also by the same author
🌐 Full suite overview: swevo.github.io
| Package | Description |
|---|---|
| FluentPdf | Free, MIT-licensed fluent PDF generation — alternative to QuestPDF's commercial license. |
| AutoBus | Free, MIT-licensed message bus — alternative to MassTransit's commercial license. |
| AutoAssert | Free, MIT-licensed fluent assertions — alternative to FluentAssertions' commercial license. |
| EFCore.BulkOperations | Free, MIT-licensed bulk insert/update/delete for EF Core. |
| AutoWire | Compile-time DI auto-registration — [Scoped]/[Singleton]/[Transient] generates IServiceCollection registration code. |
| AutoMap.Generator | Compile-time object mapping — [Map(typeof(Dto))] generates ToDto() extension methods. |
| AutoDispatch.Generator | Compile-time CQRS dispatcher — free alternative to MediatR's commercial license. |
| PollyAnalyzers | Free Roslyn analyzers for async/resilience anti-patterns — blocking calls, async void, fire-and-forget tasks, swallowed exceptions. |
| PollyAction | Free retry/backoff GitHub Action — wrap any CI step with exponential-backoff retries. |
License
MIT © Justin Bannister
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.2.0: AA002 no longer reports a namespace referencing itself as a cycle (e.g. 'Auth -> Auth'), and references inside nested types are no longer reported twice. Generated attributes are now internal and [Embedded], which fixes CS0436 when several projects in a reference chain use AutoArchitecture. Rules are enforced by an analyzer, so Razor components (.razor/.cshtml) are now checked. Analysis of generated code can be set with the AutoArchitectureGeneratedCode MSBuild property or the autoarchitecture_generated_code .editorconfig key (all|razor|none). Requires .NET SDK 9.0.300+ / VS 17.14+. 1.1.0: Add opt-in [assembly: DetectCircularDependencies] namespace cycle detection with AA002 diagnostics. 1.0.1: Add missing package icon. 1.0.0: Initial release. [assembly: ForbidDependency(from, to)] with AA001 diagnostic.