NetArchTest.Rules
1.3.2
dotnet add package NetArchTest.Rules --version 1.3.2
NuGet\Install-Package NetArchTest.Rules -Version 1.3.2
<PackageReference Include="NetArchTest.Rules" Version="1.3.2" />
<PackageVersion Include="NetArchTest.Rules" Version="1.3.2" />
<PackageReference Include="NetArchTest.Rules" />
paket add NetArchTest.Rules --version 1.3.2
#r "nuget: NetArchTest.Rules, 1.3.2"
#addin nuget:?package=NetArchTest.Rules&version=1.3.2
#tool nuget:?package=NetArchTest.Rules&version=1.3.2
NetArchTest
A fluent API for .Net Standard that can enforce architectural rules in unit tests.
Rationale
This project allows you create tests that enforce conventions for class design, naming and dependency in .Net code bases. These can be used with any unit test framework and incorporated into a build pipeline. It uses a fluid API that allows you to string together readable rules that can be used in test assertions.
There are plenty of static analysis tools that can evaluate application structure, but they are aimed more at enforcing generic best practice rather than application-specific conventions. The better tools in this space can be press-ganged into creating custom rules for a specific architecture, but the intention here is to incorporate rules into a test suite and create a self-testing architecture.
Examples
// Classes in the presentation should not directly reference repositories
var result = Types.InCurrentDomain()
.That()
.ResideInNamespace("NetArchTest.SampleLibrary.Presentation")
.ShouldNot()
.HaveDependencyOn("NetArchTest.SampleLibrary.Data")
.GetResult()
.IsSuccessful;
// Classes in the "data" namespace should implement IRepository
result = Types.InCurrentDomain()
.That().HaveDependencyOn("System.Data")
.And().ResideInNamespace(("ArchTest"))
.Should().ResideInNamespace(("NetArchTest.SampleLibrary.Data"))
.GetResult()
.IsSuccessful;
// All the service classes should be sealed
result = Types.InCurrentDomain()
.That().ImplementInterface(typeof(IWidgetService))
.Should().BeSealed()
.GetResult()
.IsSuccessful;
Getting started
It is a .Net Standard 2.0 library that is compatible with .Net Framework 4.6.1 or better and .Net Core 2.0 or better.
Writing rules
The fluent API should direct you in building up a rule based on a combination of predicates, conditions and conjunctions.
The starting point for any rule is the static Types
class, where you load a set of types from a path, assembly or namespace.
var types = Types.InAssembly(typeof(MyClass).Assembly);
Once you have selected the types you can filter them using one or more predicates. These can be chained together using And()
or Or()
conjunctions:
types.That().ResideInNamespace(“MyProject.Data”);
Once the set of classes have been filtered you can apply a set of conditions using the Should()
or ShouldNot()
methods, e.g.
types.That().ResideInNamespace(“MyProject.Data”).Should().BeSealed();
Finally, you obtain a result from the rule by using an executor, i.e. use GetTypes()
to return the types that match the rule or GetResult()
to determine whether the rule has been met. Note that the result will also return a list of types that failed to meet the conditions.
var isValid = types.That().ResideInNamespace(“MyProject.Data”).Should().BeSealed().GetResult().IsSuccessful;
Custom rules
You can extend the library by writing custom rules that implement the ICustomRule
interface. These can be applied as both predicates and conditions using a MeetsCustomRule()
method, e.g.
var myRule = new CustomRule();
// Write your own custom rules that can be used as both predicates and conditions
result = Types.InCurrentDomain()
.That().AreClasses()
.Should()
.MeetCustomRule(myRule)
.GetResult().IsSuccessful;
Grouping rules into Policies
Rules can be grouped into policies using the fluent interface exposed by the Policy
class, e.g.
var architecturePolicy = Policy.Define("Example Policy", "This is an example policy")
.For(Types.InCurrentDomain)
.Add(t =>
t.That()
.ResideInNamespace("NetArchTest.SampleLibrary.Presentation")
.ShouldNot()
.HaveDependencyOn("NetArchTest.SampleLibrary.Data"),
"Enforcing layered architecture", "Controllers should not directly reference repositories"
)
...
.Add(t =>
t.That()
.AreInterfaces()
.Should()
.HaveNameStartingWith("I"),
"Generic implementation rules", "Interface names should start with an 'I'"
);
The rules are loaded lazily and executed when the Evaluate()
method is called. This method returns a PolicyResults
instance that can be passed to a reporting mechanism.
The ExamplePolicies class in the samples demonstrates how to do this.
Further reading
A more extensive blog post describing the implementation detail is available in my blog.
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. |
.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
- Mono.Cecil (>= 0.11.3)
NuGet packages (18)
Showing the top 5 NuGet packages that depend on NetArchTest.Rules:
Package | Downloads |
---|---|
Pipoburgos.SharedKernel.Testing
C# Testing |
|
Daya.CodeGenerator
Package Description |
|
ArchTestDotNet
This utility will only execute Architecture Tests. |
|
DotNetArchTesting
This utility will only execute Architecture Tests. |
|
DotNetArchTestPack
This utility will only execute Architecture Tests. |
GitHub repositories (12)
Showing the top 5 popular GitHub repositories that depend on NetArchTest.Rules:
Repository | Stars |
---|---|
kgrzybek/modular-monolith-with-ddd
Full Modular Monolith application with Domain-Driven Design approach.
|
|
riok/mapperly
A .NET source generator for generating object mappings. No runtime reflection.
|
|
evolutionary-architecture/evolutionary-architecture-by-example
Navigate the complex landscape of .NET software architecture with our step-by-step, story-like guide. Unpack the interplay between modular monoliths, microservices, domain-driven design, and various architectural patterns. Go beyond the one-size-fits-all solutions and understand how to blend these approaches based on your unique needs.
|
|
phongnguyend/Practical.CleanArchitecture
Full-stack .Net 9 Clean Architecture (Microservices, Modular Monolith, Monolith), Blazor, Angular 19, React 19, Vue 3.5, BFF with YARP, NextJs 15, Domain-Driven Design, CQRS, SOLID, Asp.Net Core Identity Custom Storage, OpenID Connect, EF Core, OpenTelemetry, SignalR, Background Services, Health Checks, Rate Limiting, Clouds (Azure, AWS, GCP), ..
|
|
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
|
Version | Downloads | Last updated |
---|---|---|
1.3.2 | 4,420,451 | 5/23/2021 |
1.3.1 | 2,725 | 5/16/2021 |
1.3.0 | 146,357 | 10/29/2020 |
1.2.6 | 39,594 | 8/13/2020 |
1.2.5 | 133,482 | 3/6/2020 |
1.2.4 | 27,652 | 1/22/2020 |
1.2.3 | 81,226 | 10/21/2019 |
1.2.2 | 19,218 | 8/24/2019 |
1.2.1 | 21,283 | 7/1/2019 |
1.1.4 | 6,681 | 4/28/2019 |
1.1.3 | 1,796 | 3/22/2019 |
1.1.2 | 1,705 | 3/3/2019 |
1.1.1 | 3,494 | 12/27/2018 |
1.0.2 | 5,061 | 11/27/2018 |