dotnet-netarchtest
Use the open-source free `NetArchTest.Rules` library for architecture rules in .NET unit tests. Use when a repo wants lightweight, fluent architecture assertions for namespaces, dependencies, or layering.
Install
npx skills add https://github.com/Postpartum-genushyacinthus29/dotnet-skills/tree/main/skills/dotnet-netarchtest
claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install postpartum-genushyacinthus29-dotnet-skills@llmmart
git clone https://github.com/Postpartum-genushyacinthus29/dotnet-skills.git
The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole postpartum-genushyacinthus29/dotnet-skills collection as a plugin from our marketplace. Git is the plain clone.
Skill manifest
NetArchTest
Trigger On
- the repo uses or wants
NetArchTest.Rules - architecture rules should be enforced in automated tests
Value
- produce a concrete project delta: code, docs, config, tests, CI, or review artifact
- reduce ambiguity through explicit planning, verification, and final validation skills
- leave reusable project context so future tasks are faster and safer
Do Not Use For
- very rich architecture modeling that needs a heavier DSL
Inputs
- the nearest
AGENTS.md - architecture boundaries to enforce
- target assemblies
Quick Start
- Read the nearest
AGENTS.mdand confirm scope and constraints. - Run this skill's
Workflowthrough theRalph Loopuntil outcomes are acceptable. - Return the
Required Result Formatwith concrete artifacts and verification evidence.
Workflow
- Encode only durable architecture rules:
- forbidden dependencies
- namespace layering
- type shape conventions
- Keep rules readable and close to the boundary they protect.
- Fail tests on architecture drift, not on temporary style noise.
Bootstrap When Missing
If NetArchTest.Rules is not configured yet:
- Detect existing setup:
rg -n "NetArchTest\\.Rules" -g '*.csproj' .
- Add the package to the architecture test project:
dotnet add TEST_PROJECT.csproj package NetArchTest.Rules
- Add at least one executable boundary rule test.
- Wire architecture tests into the standard
testcommand inAGENTS.mdand CI. - Run
dotnet test TEST_PROJECT.csprojand returnstatus: configuredorstatus: improved. - If richer modeling is required and
ArchUnitNETis chosen as the standard, returnstatus: not_applicable.
Deliver
- architecture tests that are understandable and stable
- boundary checks wired into the normal test path used by agents and CI
Validate
- the rules map to real boundaries the team cares about
- failures point to actionable dependency drift
Ralph Loop
Use the Ralph Loop for every task, including docs, architecture, testing, and tooling work.
- Plan first (mandatory):
- analyze current state
- define target outcome, constraints, and risks
- write a detailed execution plan
- list final validation skills to run at the end, with order and reason
- Execute one planned step and produce a concrete delta.
- Review the result and capture findings with actionable next fixes.
- Apply fixes in small batches and rerun the relevant checks or review steps.
- Update the plan after each iteration.
- Repeat until outcomes are acceptable or only explicit exceptions remain.
- If a dependency is missing, bootstrap it or return
status: not_applicablewith explicit reason and fallback path.
Required Result Format
status:complete|clean|improved|configured|not_applicable|blockedplan: concise plan and current iteration stepactions_taken: concrete changes madevalidation_skills: final skills run, or skipped with reasonsverification: commands, checks, or review evidence summaryremaining: top unresolved items ornone
For setup-only requests with no execution, return status: configured and exact next commands.
Load References
references/patterns.mdreferences/examples.mdreferences/netarchtest.md
Example Requests
- "Add architecture tests with NetArchTest."
- "Block UI from referencing data directly."
Files (dotnet-skills)
-
references
-
examples.md 8.6 KB
# NetArchTest Common Architecture Rules This reference provides ready-to-use architecture test examples for common .NET project structures. ## Clean Architecture / Onion Architecture ### Domain Layer Independence The domain layer should have no dependencies on infrastructure or application layers: ```csharp [Fact] public void Domain_ShouldNotDependOn_Infrastructure() { var result = Types .InAssembly(typeof(DomainAssemblyMarker).Assembly) .ShouldNot() .HaveDependencyOn("MyApp.Infrastructure") .GetResult(); Assert.True(result.IsSuccessful, $"Domain depends on Infrastructure: {FormatFailures(result)}"); } [Fact] public void Domain_ShouldNotDependOn_Application() { var result = Types .InAssembly(typeof(DomainAssemblyMarker).Assembly) .ShouldNot() .HaveDependencyOn("MyApp.Application") .GetResult(); Assert.True(result.IsSuccessful, $"Domain depends on Application: {FormatFailures(result)}"); } ``` ### Application Layer Allowed Dependencies ```csharp [Fact] public void Application_ShouldOnlyDependOn_AllowedLayers() { var result = Types .InAssembly(typeof(ApplicationAssemblyMarker).Assembly) .That() .ResideInNamespace("MyApp.Application") .ShouldNot() .HaveDependencyOnAny( "MyApp.Infrastructure", "MyApp.Web", "MyApp.Api") .GetResult(); Assert.True(result.IsSuccessful, $"Application layer violation: {FormatFailures(result)}"); } ``` ### Infrastructure Implements Domain Interfaces ```csharp [Fact] public void Repositories_ShouldImplement_DomainInterfaces() { var result = Types .InAssembly(typeof(InfrastructureAssemblyMarker).Assembly) .That() .HaveNameEndingWith("Repository") .Should() .ImplementInterface(typeof(IRepository<>)) .GetResult(); Assert.True(result.IsSuccessful, $"Repositories missing interface: {FormatFailures(result)}"); } ``` ## Layered Architecture (N-Tier) ### Layer Dependency Direction ```csharp [Fact] public void DataLayer_ShouldNotDependOn_BusinessLayer() { var result = Types .InAssembly(typeof(DataLayerMarker).Assembly) .ShouldNot() .HaveDependencyOn("MyApp.Business") .GetResult(); Assert.True(result.IsSuccessful); } [Fact] public void BusinessLayer_ShouldNotDependOn_PresentationLayer() { var result = Types .InAssembly(typeof(BusinessLayerMarker).Assembly) .ShouldNot() .HaveDependencyOn("MyApp.Web") .GetResult(); Assert.True(result.IsSuccessful); } ``` ## CQRS Pattern ### Command Handlers Location ```csharp [Fact] public void CommandHandlers_ShouldResideIn_ApplicationLayer() { var result = Types .InCurrentDomain() .That() .HaveNameEndingWith("CommandHandler") .Should() .ResideInNamespaceStartingWith("MyApp.Application") .GetResult(); Assert.True(result.IsSuccessful); } ``` ### Commands Should Be Immutable ```csharp [Fact] public void Commands_ShouldBe_Sealed() { var result = Types .InAssembly(typeof(ApplicationAssemblyMarker).Assembly) .That() .ImplementInterface(typeof(ICommand)) .Should() .BeSealed() .GetResult(); Assert.True(result.IsSuccessful); } ``` ### Queries Should Not Modify State ```csharp [Fact] public void QueryHandlers_ShouldNotDependOn_WriteRepositories() { var result = Types .InAssembly(typeof(ApplicationAssemblyMarker).Assembly) .That() .HaveNameEndingWith("QueryHandler") .ShouldNot() .HaveDependencyOnAny( "MyApp.Infrastructure.Persistence.Write", "MyApp.Application.Commands") .GetResult(); Assert.True(result.IsSuccessful); } ``` ## Domain-Driven Design ### Aggregates Encapsulation ```csharp [Fact] public void Entities_ShouldNotExpose_PublicSetters() { // Use custom rule for property analysis var result = Types .InAssembly(typeof(DomainAssemblyMarker).Assembly) .That() .Inherit(typeof(Entity)) .And() .AreNotAbstract() .Should() .MeetCustomRule(new NoPublicSettersRule()) .GetResult(); Assert.True(result.IsSuccessful); } ``` ### Value Objects Are Immutable ```csharp [Fact] public void ValueObjects_ShouldBe_Sealed() { var result = Types .InAssembly(typeof(DomainAssemblyMarker).Assembly) .That() .Inherit(typeof(ValueObject)) .Should() .BeSealed() .GetResult(); Assert.True(result.IsSuccessful); } ``` ### Domain Events Location ```csharp [Fact] public void DomainEvents_ShouldResideIn_DomainLayer() { var result = Types .InCurrentDomain() .That() .ImplementInterface(typeof(IDomainEvent)) .Should() .ResideInNamespaceStartingWith("MyApp.Domain") .GetResult(); Assert.True(result.IsSuccessful); } ``` ## API / Web Layer ### Controllers Naming Convention ```csharp [Fact] public void Controllers_ShouldHave_CorrectSuffix() { var result = Types .InAssembly(typeof(WebAssemblyMarker).Assembly) .That() .Inherit(typeof(ControllerBase)) .Should() .HaveNameEndingWith("Controller") .GetResult(); Assert.True(result.IsSuccessful); } ``` ### Controllers Should Be In Controllers Namespace ```csharp [Fact] public void Controllers_ShouldResideIn_ControllersNamespace() { var result = Types .InAssembly(typeof(WebAssemblyMarker).Assembly) .That() .Inherit(typeof(ControllerBase)) .Should() .ResideInNamespaceContaining("Controllers") .GetResult(); Assert.True(result.IsSuccessful); } ``` ## Service Registration ### Services Should Have Interfaces ```csharp [Fact] public void Services_ShouldImplement_Interface() { var result = Types .InAssembly(typeof(ApplicationAssemblyMarker).Assembly) .That() .HaveNameEndingWith("Service") .And() .AreClasses() .Should() .ImplementInterface(typeof(object)) // Any interface .GetResult(); // Better: check for matching I{Name} interface pattern var services = Types .InAssembly(typeof(ApplicationAssemblyMarker).Assembly) .That() .HaveNameEndingWith("Service") .And() .AreClasses() .GetTypes(); foreach (var service in services) { var expectedInterface = $"I{service.Name}"; Assert.True( service.GetInterfaces().Any(i => i.Name == expectedInterface), $"{service.Name} should implement {expectedInterface}"); } } ``` ## No Circular Dependencies ### Namespace Slices Should Not Have Cycles ```csharp [Fact] public void Namespaces_ShouldNotHave_CircularDependencies() { var slices = Types .InAssembly(typeof(ApplicationAssemblyMarker).Assembly) .Slice() .ByNamespacePrefix("MyApp.Application.Features"); var result = slices.Should().NotHaveDependenciesBetweenSlices(); Assert.True(result.IsSuccessful); } ``` ## Utility Helpers ### Format Failure Message ```csharp private static string FormatFailures(TestResult result) { if (result.FailingTypeNames == null || !result.FailingTypeNames.Any()) return "No details available"; return string.Join(Environment.NewLine, result.FailingTypeNames.Select(t => $" - {t}")); } ``` ### Assembly Marker Pattern Create empty marker interfaces in each project for easy assembly reference: ```csharp // In MyApp.Domain project namespace MyApp.Domain; public interface IDomainAssemblyMarker { } // In tests var domainAssembly = typeof(IDomainAssemblyMarker).Assembly; ``` ## Test Organization ### Recommended Test Class Structure ```csharp public class ArchitectureTests { private readonly Assembly _domainAssembly; private readonly Assembly _applicationAssembly; private readonly Assembly _infrastructureAssembly; public ArchitectureTests() { _domainAssembly = typeof(DomainMarker).Assembly; _applicationAssembly = typeof(ApplicationMarker).Assembly; _infrastructureAssembly = typeof(InfrastructureMarker).Assembly; } [Fact] public void DomainLayer_HasNoDependencyOn_OtherLayers() { var result = Types .InAssembly(_domainAssembly) .ShouldNot() .HaveDependencyOnAny( "MyApp.Application", "MyApp.Infrastructure", "MyApp.Web") .GetResult(); Assert.True(result.IsSuccessful, $"Domain layer violations:{Environment.NewLine}{FormatFailures(result)}"); } } ``` -
netarchtest.md 838 B
# NetArchTest.Rules ## Open/Free Status - open source - free to use ## Install ```bash dotnet add package NetArchTest.Rules ``` ## Verify First Before adding the package, check whether the repo already references it: ```bash rg -n "NetArchTest\\.Rules" -g '*.csproj' . ``` ## Common Usage Use inside your test project: ```csharp var result = Types.InAssembly(typeof(MyType).Assembly) .That().ResideInNamespace("MyApp.Presentation") .ShouldNot().HaveDependencyOn("MyApp.Data") .GetResult() .IsSuccessful; ``` ## CI Fit - runs as part of the normal test suite - good for clean architecture and layering rules ## When Not To Use - when the repo needs richer architecture modeling and custom assertions than NetArchTest provides comfortably ## Sources - [NetArchTest](https://github.com/BenMorris/NetArchTest) -
patterns.md 4.1 KB
# NetArchTest Rule Patterns This reference covers the core fluent API patterns for `NetArchTest.Rules`. ## Basic Pattern Structure All NetArchTest rules follow a three-part fluent pattern: ```csharp var result = Types .InAssembly(assembly) // 1. Select types .That() // 2. Apply predicates .ShouldNot().HaveDependencyOn("SomeNamespace") // 3. Assert condition .GetResult(); Assert.True(result.IsSuccessful); ``` ## Type Selection Patterns ### From Assembly ```csharp // Current assembly Types.InCurrentDomain() // Specific assembly Types.InAssembly(typeof(MyClass).Assembly) // Multiple assemblies Types.InAssemblies(assemblies) ``` ### From Namespace ```csharp Types.InAssembly(assembly) .That() .ResideInNamespace("MyApp.Domain") ``` ### Namespace Matching Variants ```csharp // Exact namespace .ResideInNamespace("MyApp.Domain") // Namespace prefix (includes sub-namespaces) .ResideInNamespaceStartingWith("MyApp.Domain") // Namespace suffix .ResideInNamespaceEndingWith(".Handlers") // Namespace contains .ResideInNamespaceContaining("Services") ``` ## Type Predicates ### By Naming Convention ```csharp .HaveNameStartingWith("I") // Interfaces .HaveNameEndingWith("Service") // Services .HaveNameMatching(".*Handler$") // Regex match ``` ### By Inheritance ```csharp .Inherit(typeof(BaseClass)) .ImplementInterface(typeof(IService)) .BeAssignableTo(typeof(ICommand)) ``` ### By Attributes ```csharp .HaveCustomAttribute(typeof(SerializableAttribute)) .HaveCustomAttributeOrInherit(typeof(MyAttribute)) ``` ### By Type Kind ```csharp .BeClasses() .BeInterfaces() .BeSealed() .BeAbstract() .BePublic() .BeInternal() .BeStatic() .BeGeneric() ``` ### By Dependencies ```csharp .HaveDependencyOn("System.Data") .HaveDependencyOnAny("Lib1", "Lib2") .HaveDependencyOnAll("Required1", "Required2") .OnlyHaveDependenciesOn("Allowed1", "Allowed2") ``` ## Assertion Patterns ### Positive Assertions ```csharp .Should().BePublic() .Should().BeSealed() .Should().HaveDependencyOn("Required.Namespace") .Should().ImplementInterface(typeof(IRequired)) ``` ### Negative Assertions ```csharp .ShouldNot().BePublic() .ShouldNot().HaveDependencyOn("Forbidden.Namespace") .ShouldNot().HaveDependencyOnAny("Bad1", "Bad2") ``` ## Combining Predicates ### And (implicit) Chained predicates are ANDed: ```csharp .That() .ResideInNamespace("MyApp.Services") .And() .HaveNameEndingWith("Service") ``` ### Or ```csharp .That() .ResideInNamespace("MyApp.Services") .Or() .ResideInNamespace("MyApp.Handlers") ``` ## Result Handling ### Basic Result Check ```csharp var result = Types.InAssembly(assembly) .That() .ResideInNamespace("MyApp.Domain") .ShouldNot().HaveDependencyOn("MyApp.Infrastructure") .GetResult(); Assert.True(result.IsSuccessful, $"Domain layer violation: {string.Join(", ", result.FailingTypeNames ?? Array.Empty<string>())}"); ``` ### Detailed Failure Inspection ```csharp if (!result.IsSuccessful) { foreach (var failingType in result.FailingTypes) { Console.WriteLine($"Violation: {failingType.FullName}"); } } ``` ## Custom Predicates ### Using MeetCustomRule ```csharp .MeetCustomRule(new MyCustomRule()) ``` ### Implementing ICustomRule ```csharp public class NoPublicFieldsRule : ICustomRule { public bool MeetsRule(TypeDefinition type) { return !type.Fields.Any(f => f.IsPublic && !f.IsStatic); } } ``` ## Slices Pattern (Dependency Cycles) ```csharp var slices = Types.InAssembly(assembly) .Slice() .ByNamespacePrefix("MyApp"); slices.Should().NotHaveDependenciesBetweenSlices(); ``` ## Excluding Types ```csharp Types.InAssembly(assembly) .That() .ResideInNamespace("MyApp.Domain") .And() .DoNotHaveNameMatching(".*Tests$") // Exclude test types .And() .AreNotNested() // Exclude nested types ``` ## Assembly-Level Rules ```csharp // Check assembly references var assembly = typeof(MyClass).Assembly; var references = assembly.GetReferencedAssemblies(); // Ensure no reference to forbidden assembly Assert.DoesNotContain(references, r => r.Name == "ForbiddenAssembly"); ```
-
-
SKILL.md 3.7 KB
--- name: dotnet-netarchtest version: "1.0.0" category: "Architecture" description: "Use the open-source free `NetArchTest.Rules` library for architecture rules in .NET unit tests. Use when a repo wants lightweight, fluent architecture assertions for namespaces, dependencies, or layering." compatibility: "Requires a .NET test project; works with any unit-test framework." --- # NetArchTest ## Trigger On - the repo uses or wants `NetArchTest.Rules` - architecture rules should be enforced in automated tests ## Value - produce a concrete project delta: code, docs, config, tests, CI, or review artifact - reduce ambiguity through explicit planning, verification, and final validation skills - leave reusable project context so future tasks are faster and safer ## Do Not Use For - very rich architecture modeling that needs a heavier DSL ## Inputs - the nearest `AGENTS.md` - architecture boundaries to enforce - target assemblies ## Quick Start 1. Read the nearest `AGENTS.md` and confirm scope and constraints. 2. Run this skill's `Workflow` through the `Ralph Loop` until outcomes are acceptable. 3. Return the `Required Result Format` with concrete artifacts and verification evidence. ## Workflow 1. Encode only durable architecture rules: - forbidden dependencies - namespace layering - type shape conventions 2. Keep rules readable and close to the boundary they protect. 3. Fail tests on architecture drift, not on temporary style noise. ## Bootstrap When Missing If `NetArchTest.Rules` is not configured yet: 1. Detect existing setup: - `rg -n "NetArchTest\\.Rules" -g '*.csproj' .` 2. Add the package to the architecture test project: - `dotnet add TEST_PROJECT.csproj package NetArchTest.Rules` 3. Add at least one executable boundary rule test. 4. Wire architecture tests into the standard `test` command in `AGENTS.md` and CI. 5. Run `dotnet test TEST_PROJECT.csproj` and return `status: configured` or `status: improved`. 6. If richer modeling is required and `ArchUnitNET` is chosen as the standard, return `status: not_applicable`. ## Deliver - architecture tests that are understandable and stable - boundary checks wired into the normal test path used by agents and CI ## Validate - the rules map to real boundaries the team cares about - failures point to actionable dependency drift ## Ralph Loop Use the Ralph Loop for every task, including docs, architecture, testing, and tooling work. 1. Plan first (mandatory): - analyze current state - define target outcome, constraints, and risks - write a detailed execution plan - list final validation skills to run at the end, with order and reason 2. Execute one planned step and produce a concrete delta. 3. Review the result and capture findings with actionable next fixes. 4. Apply fixes in small batches and rerun the relevant checks or review steps. 5. Update the plan after each iteration. 6. Repeat until outcomes are acceptable or only explicit exceptions remain. 7. If a dependency is missing, bootstrap it or return `status: not_applicable` with explicit reason and fallback path. ### Required Result Format - `status`: `complete` | `clean` | `improved` | `configured` | `not_applicable` | `blocked` - `plan`: concise plan and current iteration step - `actions_taken`: concrete changes made - `validation_skills`: final skills run, or skipped with reasons - `verification`: commands, checks, or review evidence summary - `remaining`: top unresolved items or `none` For setup-only requests with no execution, return `status: configured` and exact next commands. ## Load References - `references/patterns.md` - `references/examples.md` - `references/netarchtest.md` ## Example Requests - "Add architecture tests with NetArchTest." - "Block UI from referencing data directly."
Comments (0)
Sign in to join the conversation.
Reviews (0)
No reviews yet.
No comments yet.