StructureGuard 0.0.1-preview2
dotnet add package StructureGuard --version 0.0.1-preview2
NuGet\Install-Package StructureGuard -Version 0.0.1-preview2
<PackageReference Include="StructureGuard" Version="0.0.1-preview2" />
<PackageVersion Include="StructureGuard" Version="0.0.1-preview2" />
<PackageReference Include="StructureGuard" />
paket add StructureGuard --version 0.0.1-preview2
#r "nuget: StructureGuard, 0.0.1-preview2"
#:package StructureGuard@0.0.1-preview2
#addin nuget:?package=StructureGuard&version=0.0.1-preview2&prerelease
#tool nuget:?package=StructureGuard&version=0.0.1-preview2&prerelease
StructureGuard
StructureGuard is a Roslyn-based architectural guard for C# projects. It allows you to define and enforce dependency rules between different layers or vertical slices of your application using namespaces, rather than relying solely on assembly (DLL) boundaries.
Why StructureGuard?
- Guard Vertical Slices: While layered architecture is often captured via different projects, guarding vertical slices within a single project is challenging. StructureGuard uses namespaces as the primary boundary.
- DLLs are for deployment: If you do not intend to deploy components independently, splitting your application into numerous projects/DLLs can lead to unnecessary complexity and slower build times.
- Namespaces as first-class citizens: Namespaces are more flexible and descriptive than assembly boundaries for defining internal structure.
- Shift-Left Feedback: Unlike unit tests (like NetArchTest) that run after compilation, StructureGuard provides real-time feedback directly in your IDE and fails the build during compilation if a rule is violated.
How it works
Create a "Analyzer" project in your solution in which you define your architectural rules. This project must reference StructureGuard and implements a class inheriting from SliceAnalyzer. For the analyser to work in your code, your project must reference this Analyzer project.
1. Create your Analyzer Project
Create a new netstandard2.0 class library project (e.g., MyProject.Analyzer).
Add a reference to the StructureGuard library. (use Nuget once a version is available there)
2. Configure your Rules
1. Using mermaid
---
title: Clean architecture
namespace: CleanArchitecture
---
graph TD
Infrastructure --> Domain
Infrastructure --> Application
Presentation --> Application
Presentation --> Domain
Application --> Domain
2. Using C# code
Create a class that inherits from SliceAnalyzer and override OnInitialize to define your root namespace and permitted dependencies.
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using StructureGuard;
namespace MyProject.Analyzer
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MyStructureGuard : SliceAnalyzer
{
protected override void OnInitialize(AnalysisContext context)
{
// The base namespace of your application
RootNameSpace = "MyProject.App";
// Define allowed dependencies between namespaces
// Format: new Dependency(fromLayer, toLayer)
PermittedDependencies = new List<Dependency>()
{
// Infrastructure can depend on Domain
new Dependency(new Layer("Infrastructure"), new Layer("Domain")),
// Web can depend on both Infrastructure and Domain
new Dependency(new Layer("Web"), new Layer("Infrastructure")),
new Dependency(new Layer("Web"), new Layer("Domain")),
};
base.OnInitialize(context);
}
}
}
3. Apply to your Target Project
In your main application project (.csproj), add a reference to your Analyzer project using OutputItemType="Analyzer" and ReferenceOutputAssembly="false".
<ItemGroup>
<ProjectReference Include="..\MyProject.Analyzer\MyProject.Analyzer.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
Rule Enforcement
Once configured, the analyzer will monitor your code. If you try to use a type from a namespace that isn't explicitly permitted for your current namespace, you will see a compiler error (STR001) in your IDE.
For example, if Domain tries to reference Infrastructure but no such dependency is defined in PermittedDependencies, a violation is reported.
Layer Matching
StructureGuard matches layers based on the namespace structure under your RootNameSpace.
If RootNameSpace is MyProject.App:
- A class in
MyProject.App.Domain.Modelsis considered part of theDomainlayer. - A class in
MyProject.App.Infrastructure.Datais considered part of theInfrastructurelayer.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1-preview2 | 77 | 5/21/2026 |
| 0.0.1-preview1 | 64 | 5/15/2026 |