{"id":13414241,"url":"https://github.com/BenMorris/NetArchTest","last_synced_at":"2025-03-14T21:32:18.463Z","repository":{"id":37735800,"uuid":"159235961","full_name":"BenMorris/NetArchTest","owner":"BenMorris","description":"A fluent API for .Net that can enforce architectural rules in unit tests.","archived":false,"fork":false,"pushed_at":"2024-07-29T18:02:58.000Z","size":424,"stargazers_count":1382,"open_issues_count":19,"forks_count":82,"subscribers_count":34,"default_branch":"master","last_synced_at":"2024-10-16T19:14:56.977Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BenMorris.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-11-26T21:36:01.000Z","updated_at":"2024-10-16T09:37:31.000Z","dependencies_parsed_at":"2024-01-02T22:49:59.148Z","dependency_job_id":null,"html_url":"https://github.com/BenMorris/NetArchTest","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenMorris%2FNetArchTest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenMorris%2FNetArchTest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenMorris%2FNetArchTest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenMorris%2FNetArchTest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BenMorris","download_url":"https://codeload.github.com/BenMorris/NetArchTest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221508977,"owners_count":16834807,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T21:00:17.029Z","updated_at":"2024-10-26T07:30:45.182Z","avatar_url":"https://github.com/BenMorris.png","language":"C#","readme":"﻿# NetArchTest\n\n[![Build status](https://dev.azure.com/ben-morris-uk/Open-Source/_apis/build/status/NetArchTest-CI-Build)](https://dev.azure.com/ben-morris-uk/Open-Source/_build/latest?definitionId=2)\n\nA fluent API for .Net Standard that can enforce architectural rules in unit tests. \n\nInspired by the [ArchUnit](https://www.archunit.org/) library for Java.\n\n## Rationale\n\nThis 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.\n\nThere 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*.\n\nThe 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. \n\n## Examples\n\n```csharp\n// Classes in the presentation should not directly reference repositories\nvar result = Types.InCurrentDomain()\n    .That()\n    .ResideInNamespace(\"NetArchTest.SampleLibrary.Presentation\")\n    .ShouldNot()\n    .HaveDependencyOn(\"NetArchTest.SampleLibrary.Data\")\n    .GetResult()\n    .IsSuccessful;\n\n// Classes in the \"data\" namespace should implement IRepository\nresult = Types.InCurrentDomain()\n    .That().HaveDependencyOn(\"System.Data\")\n    .And().ResideInNamespace((\"ArchTest\"))\n    .Should().ResideInNamespace((\"NetArchTest.SampleLibrary.Data\"))\n    .GetResult()\n    .IsSuccessful;\n\n// All the service classes should be sealed\nresult = Types.InCurrentDomain()\n    .That().ImplementInterface(typeof(IWidgetService))\n    .Should().BeSealed()\n    .GetResult()\n    .IsSuccessful;\n```\n\n## Getting started\n\nThe main rules library is available as a package on NuGet: [NetArchTest.Rules](https://www.nuget.org/packages/NetArchTest.Rules/).\n\nIt 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.\n\n### Writing rules\n\nThe fluent API should direct you in building up a rule based on a combination of predicates, conditions and conjunctions. \n\nThe starting point for any rule is the static `Types` class, where you load a set of types from a path, assembly or namespace.\n\n```csharp\nvar types = Types.InAssembly(typeof(MyClass).Assembly);\n```\nOnce you have selected the types you can filter them using one or more predicates. These can be chained together using `And()` or `Or()` conjunctions:\n```csharp\ntypes.That().ResideInNamespace(“MyProject.Data”);\n```\nOnce the set of classes have been filtered you can apply a set of conditions using the `Should()` or `ShouldNot()` methods, e.g.\n```csharp\ntypes.That().ResideInNamespace(“MyProject.Data”).Should().BeSealed();\n```\nFinally, 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.\n```csharp\nvar isValid = types.That().ResideInNamespace(“MyProject.Data”).Should().BeSealed().GetResult().IsSuccessful;\n```\n\n### Custom rules\n\nYou 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.\n\n```csharp\nvar myRule = new CustomRule();\n\n// Write your own custom rules that can be used as both predicates and conditions\nresult = Types.InCurrentDomain()\n    .That().AreClasses()\n    .Should()\n    .MeetCustomRule(myRule)\n    .GetResult().IsSuccessful;\n```\n\n### Grouping rules into Policies\n\nRules can be grouped into policies using the fluent interface exposed by the `Policy` class, e.g. \n\n```csharp\nvar architecturePolicy = Policy.Define(\"Example Policy\", \"This is an example policy\")\n                .For(Types.InCurrentDomain)\n                .Add(t =\u003e\n                   t.That()\n                   .ResideInNamespace(\"NetArchTest.SampleLibrary.Presentation\")\n                   .ShouldNot()\n                   .HaveDependencyOn(\"NetArchTest.SampleLibrary.Data\"),\n                   \"Enforcing layered architecture\", \"Controllers should not directly reference repositories\"\n                )\n                ...\n                .Add(t =\u003e\n                    t.That()\n                    .AreInterfaces()\n                    .Should()\n                    .HaveNameStartingWith(\"I\"),\n                    \"Generic implementation rules\", \"Interface names should start with an 'I'\"\n                );\n\n```\nThe 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.\n\nThe [ExamplePolicies](https://github.com/BenMorris/NetArchTest/blob/master/samples/NetArchTest.SampleRules/ExamplePolicies.cs) class in the samples demonstrates how to do this.\n\n## Source code\n\n\u003e I welcome contributions. Please refer to the [contributing guidelines](CONTRIBUTING.md).\n\nThe solution contains projects in three directories:\n\n - *src*: The main Rules library that is available as a package on NuGet. The main dependency is Mono.Cecil.\n - *test*: A set of unit tests for the rules based on XUnit.\n - *samples*: A couple of sample projects that demonstrate some of the possible usage scenarios.\n\n## Further reading\n\nA 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).\n","funding_links":[],"categories":["Architecture test","Testing"],"sub_categories":["Datbase to Struct"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBenMorris%2FNetArchTest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBenMorris%2FNetArchTest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBenMorris%2FNetArchTest/lists"}