You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A fluent API for .Net Standard that can enforce architectural rules in unit tests.
3
+
A fluent API for .Net Standard that can enforce architectural rules in unit tests and create a *self-testing architecture*. Inspired by the [ArchUnit](https://www.archunit.org/) library for Java.
4
4
5
-
Inspired by the [ArchUnit](https://www.archunit.org/) library for Java.
5
+
NetArchTest.eNhancedEdition is based on [NetArchTest v1.3.2](https://github.com/BenMorris/NetArchTest). If you are not familiar with NetArchTest, you should start by reading [introduction on Ben's blog](https://www.ben-morris.com/writing-archunit-style-tests-for-net-and-c-for-self-testing-architectures).
6
6
7
7
## Rationale
8
8
9
-
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.
10
9
11
-
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*.
12
10
13
-
The project is inspired by [ArchUnit](https://www.archunit.org/), a java-based library that attempts to address the difficulties of preserving architectural design patterns in code bases over the long term. Many patterns can only be enforced by convention, which tends to rely on a rigorous and consistent process of code review. This discipline often breaks down as projects grow, use cases become more complex and developers come and go.
11
+
## Getting started
12
+
13
+
The library is available as a package on NuGet: [NetArchTest.eNhancedEdition](https://www.nuget.org/packages/NetArchTest.eNhancedEdition/).
14
14
15
15
## Examples
16
16
@@ -20,33 +20,23 @@ var result = Types.InCurrentDomain()
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.
74
69
75
70
```csharp
76
71
varmyRule=newCustomRule();
77
72
78
73
// Write your own custom rules that can be used as both predicates and conditions
79
-
result=Types.InCurrentDomain()
80
-
.That().AreClasses()
74
+
varresult=Types.InCurrentDomain()
75
+
.That()
76
+
.AreClasses()
81
77
.Should()
82
78
.MeetCustomRule(myRule)
83
-
.GetResult().IsSuccessful;
84
-
```
85
-
86
-
### Grouping rules into Policies
87
-
88
-
Rules can be grouped into policies using the fluent interface exposed by the `Policy` class, e.g.
89
-
90
-
```csharp
91
-
vararchitecturePolicy=Policy.Define("Example Policy", "This is an example policy")
"Enforcing layered architecture", "Controllers should not directly reference repositories"
99
-
)
100
-
...
101
-
.Add(t=>
102
-
t.That()
103
-
.AreInterfaces()
104
-
.Should()
105
-
.HaveNameStartingWith("I"),
106
-
"Generic implementation rules", "Interface names should start with an 'I'"
107
-
);
108
-
109
-
```
110
-
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.
111
-
112
-
The [ExamplePolicies](https://github.com/BenMorris/NetArchTest/blob/master/samples/NetArchTest.SampleRules/ExamplePolicies.cs) class in the samples demonstrates how to do this.
113
-
114
-
## Further reading
115
-
116
-
A more extensive blog post describing the implementation detail is available in [my blog](https://www.ben-morris.com/writing-archunit-style-tests-for-net-and-c-for-self-testing-architectures).
0 commit comments