2.4 KiB
2.4 KiB
name, description
| name | description |
|---|---|
| GenerateArchitectureTest | Guide to creating an Architecture Test using NetArchTest.Rules to protect the Clean Architecture structure. |
GenerateArchitectureTest Skill
When a user requests to verify or create an Architecture Test, you must use the NetArchTest.Rules library to generate tests that strictly protect the project's "Clean Architecture" boundaries.
Purpose
- Prevent developers from writing sloppy code by incorrectly importing libraries/modules across layers.
- If a violation occurs (e.g., importing Entity Framework into the Domain layer), this test will immediately turn RED during the build process. It acts as solid proof to ensure the architecture is fully protected!
Writing Test Rules Guide
You need to write test methods (using [Fact] with xUnit or NUnit) that utilize the Fluent API of NetArchTest.Rules. Below are the mandatory common rules:
-
Domain Layer Rules:
- The Domain must not depend on anything from Infrastructure, Application, or WebAPI.
[Fact] public void DomainLayer_ShouldNot_HaveDependencyOn_OtherLayers() { var result = Types.InAssembly(DomainAssembly) .ShouldNot() .HaveDependencyOnAny( "MyNewProjectName.Application", "MyNewProjectName.Infrastructure", "MyNewProjectName.WebAPI" ) .GetResult(); Assert.True(result.IsSuccessful); } -
Application Layer Rules:
- The Application Layer is only allowed to communicate with the Domain. It MUST NOT have dependencies on
InfrastructureorWebAPI.
- The Application Layer is only allowed to communicate with the Domain. It MUST NOT have dependencies on
-
Controller Rules (Naming/API Location):
- Controllers must inherit from the BaseAPIController class and have the suffix "Controller".
- Direct database queries from the Controller are strictly prohibited (prevent the injection of
DbContextorIRepositoryinto the Controller; verify Constructor dependencies).
-
Handler Rules (CQRS):
- Business logic Handlers must implement the
IRequestHandlerinterface and end withCommandHandlerorQueryHandler. They must solely reside in the Application Layer.
- Business logic Handlers must implement the
Output File Format
- Path: Placed within the test project, e.g.,
tests/MyNewProjectName.ArchitectureTests/. - File Name: Usually named after the test scope, such as
LayerTests.cs,DesignConventionTests.cs,NamingRulesTests.cs, etc.