ArchitectureAssert Class
The assertions on the types and the members of an ArchitectureModel, available as Assert.Architecture. Each assertion throws an AssertionException that lists every type or member that breaks it, with the statements that cause it. To check the names or the shape of types and members, use ArchitectureAssert.AllMatch.
public sealed class ArchitectureAssertRemarks
Each assertion fails when the types or the members to check are empty, so that a typo in a namespace does not make it pass silently. Add your own assertions with an extension block on ArchitectureAssert.
Methods
| Name | Description |
|---|---|
| AllMatch(IEnumerable<TypeElement>, Func<TypeElement, bool>, string?, string?, string?) | Asserts that all the types match a predicate, such as a naming convention or a modifier. Unlike Assert.All, the message lists every type that does not match, with its location. |
| AllMatch<TMember>(IEnumerable<TMember>, Func<TMember, bool>, string?, string?, string?) | Asserts that all the members match a predicate, such as "the public methods of the controllers are async" or "the fields are not public". Unlike Assert.All, the message lists every member that does not match, with its location. |
| DependsOnlyOn(IEnumerable<MemberElement>, Func<TypeReference, bool>, string?, string?, string?) | Asserts that the members only use the types that match a predicate, including the types defined outside the loaded assemblies. The types that declare the checked members are always allowed. |
| DependsOnlyOn(IEnumerable<TypeElement>, IEnumerable<TypeElement>, string?, string?, string?) | Asserts that the types only use the allowed types and each other. Only the types of the loaded assemblies are checked: use the overload that takes a predicate to also check the types of .NET and of the packages. |
| DependsOnlyOn(IEnumerable<TypeElement>, Func<TypeReference, bool>, string?, string?, string?) | Asserts that the types only use each other and the types that match a predicate, including the types defined outside the loaded assemblies. |
| DoesNotAccessMember(IEnumerable<MemberElement>, Type, string, string?, string?) | Asserts that the members do not use a member of type: call a method or a constructor, read or write a field, a property or an event. |
| DoesNotAccessMember(IEnumerable<TypeElement>, Type, string, string?, string?) | Asserts that the types do not use a member of type: call a method or a constructor, read or write a field, a property or an event. |
| DoesNotAccessMember<T>(IEnumerable<MemberElement>, string, string?, string?) | Asserts that the members do not use a member of T: call a method or a constructor, read or write a field, a property or an event. |
| DoesNotAccessMember<T>(IEnumerable<TypeElement>, string, string?, string?) | Asserts that the types do not use a member of T: call a method or a constructor, read or write a field, a property or an event. |
| DoesNotDependOn(IEnumerable<MemberElement>, IEnumerable<TypeElement>, string?, string?, string?) | Asserts that the members do not use any of the forbidden types, in their declaration or in their code. |
| DoesNotDependOn(IEnumerable<MemberElement>, Func<TypeReference, bool>, string?, string?, string?) | Asserts that the members do not use any type that matches a predicate, including the types defined outside the loaded assemblies. |
| DoesNotDependOn(IEnumerable<MemberElement>, Type, string?, string?) | Asserts that the members do not use type, in their declaration or in their code. |
| DoesNotDependOn(IEnumerable<TypeElement>, IEnumerable<TypeElement>, string?, string?, string?) | Asserts that the types do not use any of the forbidden types. The dependencies between the checked types themselves are ignored. |
| DoesNotDependOn(IEnumerable<TypeElement>, Func<TypeReference, bool>, string?, string?, string?) | Asserts that the types do not use any type that matches a predicate. Unlike the other overloads, the predicate also sees the types defined outside the loaded assemblies. |
| DoesNotDependOn(IEnumerable<TypeElement>, Type, string?, string?) | Asserts that the types do not use type. |
| DoesNotDependOn<T>(IEnumerable<MemberElement>, string?, string?) | Asserts that the members do not use T, in their declaration or in their code. |
| DoesNotDependOn<T>(IEnumerable<TypeElement>, string?, string?) | Asserts that the types do not use T. |
| DoesNotDependOnEachOther(IEnumerable<IGrouping<string, TypeElement>>, string?, string?) | Asserts that the slices do not use each other at all. Each type that uses another slice is reported. |
| DoesNotHaveCycles(IEnumerable<IGrouping<string, TypeElement>>, string?, string?) | Asserts that the slices do not depend on each other in a cycle (Orders → Billing → Orders). A slice may use another one. Each group of slices that depend on each other is reported once, with one of the shortest cycles and the dependencies that make it. |
AllMatch overloads
AllMatch(IEnumerable<TypeElement>, Func<TypeElement, bool>, string?, string?, string?)
ArchitectureAssert.AllMatch.csAsserts that all the types match a predicate, such as a naming convention or a modifier. Unlike Assert.All, the message lists every type that does not match, with its location.
public void AllMatch(IEnumerable<TypeElement> types, Func<TypeElement, bool> predicate, string? message = null, string? typesExpression = null, string? predicateExpression = null)Parameters
typesIEnumerable<TypeElement>- The types to check.
predicateFunc<TypeElement, bool>- Returns
truefor a valid type, such ast => t.IsSealed. messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types. predicateExpressionstring?- The expression of
predicate.
Exceptions
AssertionException- A type does not match the predicate, or
typesis empty.
Example
var architecture = ArchitectureModel.FromAssemblyContaining<Order>();
var repositories = architecture.Types.Where(t => t.IsClass && t.Implements<IOrderRepository>());
Assert.Architecture.AllMatch(repositories, t => t.Name.EndsWith("Repository", StringComparison.Ordinal));
Assert.Architecture.AllMatch(repositories, t => t.IsSealed);
Assert.Architecture.AllMatch(architecture.Types.Where(t => t.IsInNamespaceOrChildren("SampleApp.Domain") && !t.IsInterface), t => t.IsImmutable);AllMatch<TMember>(IEnumerable<TMember>, Func<TMember, bool>, string?, string?, string?)
ArchitectureAssert.AllMatch.csAsserts that all the members match a predicate, such as "the public methods of the controllers are async" or "the fields are not public". Unlike Assert.All, the message lists every member that does not match, with its location.
public void AllMatch<TMember>(IEnumerable<TMember> members, Func<TMember, bool> predicate, string? message = null, string? membersExpression = null, string? predicateExpression = null) where TMember : MemberElementType parameters
TMember- The type of the members:
MethodElement,PropertyElement,FieldElement,EventElementorMemberElement.
Parameters
membersIEnumerable<TMember>- The members to check, such as
types.SelectMany(t => t.Methods). predicateFunc<TMember, bool>- Returns
truefor a valid member, such asmethod => method.IsAsync. messagestring?- The message that describes the failure.
membersExpressionstring?- The expression of
members. predicateExpressionstring?- The expression of
predicate.
Exceptions
AssertionException- A member does not match the predicate, or
membersis empty.
Example
var architecture = ArchitectureModel.FromAssemblyContaining<Order>();
var handlers = architecture.Types.Where(t => t.IsInNamespace("SampleApp.Application") && t.Name.EndsWith("Handler", StringComparison.Ordinal));
var controllers = architecture.Types.Where(t => t.IsInNamespace("SampleApp.Web") && t.Name.EndsWith("Controller", StringComparison.Ordinal));
Assert.Architecture.AllMatch(handlers.SelectMany(t => t.Methods).Where(method => method.IsPublic), method => method.IsAsync);
var domain = architecture.Types.Where(t => t.IsInNamespaceOrChildren("SampleApp.Domain"));
Assert.Architecture.AllMatch(domain.SelectMany(t => t.Properties), property => property.Setter is null || property.IsInitOnly, "the domain is immutable");
// OrdersController.CreateRepository is a public method that does not return a Task
var actions = controllers.SelectMany(t => t.Methods).Where(method => method.IsPublic);
var exception = Assert.Throws<AssertionException>(() => Assert.Architecture.AllMatch(actions, method => method.ReturnType.Is(typeof(Task<>))));
Assert.StartsWith("""
Assert.Architecture.AllMatch() assertion failed: 1 member does not match the predicate.
Members expression: actions
Predicate expression: method => method.ReturnType.Is(typeof(Task<>))
- SampleApp.Web.OrdersController.CreateRepository()
""".ReplaceLineEndings("\n"), exception.Message);DependsOnlyOn overloads
DependsOnlyOn(IEnumerable<MemberElement>, Func<TypeReference, bool>, string?, string?, string?)
ArchitectureAssert.Members.csAsserts that the members only use the types that match a predicate, including the types defined outside the loaded assemblies. The types that declare the checked members are always allowed.
public void DependsOnlyOn(IEnumerable<MemberElement> members, Func<TypeReference, bool> predicate, string? message = null, string? membersExpression = null, string? predicateExpression = null)Parameters
membersIEnumerable<MemberElement>- The members to check.
predicateFunc<TypeReference, bool>- Returns
truefor an allowed type, such ast => t.IsInNamespaceOrChildren("System"). messagestring?- The message that describes the failure.
membersExpressionstring?- The expression of
members. predicateExpressionstring?- The expression of
predicate.
Exceptions
AssertionException- A member uses a type that is not allowed, or
membersis empty.
DependsOnlyOn(IEnumerable<TypeElement>, IEnumerable<TypeElement>, string?, string?, string?)
ArchitectureAssert.csAsserts that the types only use the allowed types and each other. Only the types of the loaded assemblies are checked: use the overload that takes a predicate to also check the types of .NET and of the packages.
public void DependsOnlyOn(IEnumerable<TypeElement> types, IEnumerable<TypeElement> allowedTypes, string? message = null, string? typesExpression = null, string? allowedTypesExpression = null)Parameters
typesIEnumerable<TypeElement>- The types to check.
allowedTypesIEnumerable<TypeElement>- The types that may be used.
messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types. allowedTypesExpressionstring?- The expression of
allowedTypes.
Exceptions
AssertionException- A type uses a type that is not allowed, or
typesis empty.
DependsOnlyOn(IEnumerable<TypeElement>, Func<TypeReference, bool>, string?, string?, string?)
ArchitectureAssert.csAsserts that the types only use each other and the types that match a predicate, including the types defined outside the loaded assemblies.
public void DependsOnlyOn(IEnumerable<TypeElement> types, Func<TypeReference, bool> predicate, string? message = null, string? typesExpression = null, string? predicateExpression = null)Parameters
typesIEnumerable<TypeElement>- The types to check.
predicateFunc<TypeReference, bool>- Returns
truefor an allowed type, such ast => t.IsInNamespaceOrChildren("System"). messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types. predicateExpressionstring?- The expression of
predicate.
Exceptions
AssertionException- A type uses a type that is not allowed, or
typesis empty.
Example
var architecture = ArchitectureModel.FromAssemblyContaining<Order>();
var domain = architecture.Types.Where(t => t.IsInNamespaceOrChildren("SampleApp.Domain"));
// The types of the domain may use each other and the types of .NET, nothing else
Assert.Architecture.DependsOnlyOn(domain, t => t.IsInNamespaceOrChildren("System"));DoesNotAccessMember overloads
DoesNotAccessMember(IEnumerable<MemberElement>, Type, string, string?, string?)
ArchitectureAssert.Members.csAsserts that the members do not use a member of type: call a method or a constructor, read or write a field, a property or an event.
public void DoesNotAccessMember(IEnumerable<MemberElement> members, Type type, string memberName, string? message = null, string? membersExpression = null)Parameters
membersIEnumerable<MemberElement>- The members to check.
typeType- The type that declares the member.
memberNamestring- The name of the member (
nameof(DateTime.Now)), or.ctorfor the constructors. messagestring?- The message that describes the failure.
membersExpressionstring?- The expression of
members.
Exceptions
AssertionException- A member uses the member, or
membersis empty.
DoesNotAccessMember(IEnumerable<TypeElement>, Type, string, string?, string?)
ArchitectureAssert.csAsserts that the types do not use a member of type: call a method or a constructor, read or write a field, a property or an event.
public void DoesNotAccessMember(IEnumerable<TypeElement> types, Type type, string memberName, string? message = null, string? typesExpression = null)Parameters
typesIEnumerable<TypeElement>- The types to check.
typeType- The type that declares the member.
memberNamestring- The name of the member (
nameof(DateTime.Now)), or.ctorfor the constructors. messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types.
Exceptions
AssertionException- A type uses the member, or
typesis empty.
DoesNotAccessMember<T>(IEnumerable<MemberElement>, string, string?, string?)
ArchitectureAssert.Members.csAsserts that the members do not use a member of T: call a method or a constructor, read or write a field, a property or an event.
public void DoesNotAccessMember<T>(IEnumerable<MemberElement> members, string memberName, string? message = null, string? membersExpression = null)Type parameters
T- The type that declares the member.
Parameters
membersIEnumerable<MemberElement>- The members to check.
memberNamestring- The name of the member (
nameof(DateTime.Now)), or.ctorfor the constructors. messagestring?- The message that describes the failure.
membersExpressionstring?- The expression of
members.
Exceptions
AssertionException- A member uses the member, or
membersis empty.
DoesNotAccessMember<T>(IEnumerable<TypeElement>, string, string?, string?)
ArchitectureAssert.csAsserts that the types do not use a member of T: call a method or a constructor, read or write a field, a property or an event.
public void DoesNotAccessMember<T>(IEnumerable<TypeElement> types, string memberName, string? message = null, string? typesExpression = null)Type parameters
T- The type that declares the member.
Parameters
typesIEnumerable<TypeElement>- The types to check.
memberNamestring- The name of the member (
nameof(DateTime.Now)), or.ctorfor the constructors. messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types.
Exceptions
AssertionException- A type uses the member, or
typesis empty.
Example
var architecture = ArchitectureModel.FromAssemblyContaining<Order>();
var types = architecture.Types.Where(t => t.IsInNamespaceOrChildren("SampleApp"));
Assert.Architecture.DoesNotAccessMember<DateTime>(types, "Now", "the current time comes from TimeProvider");
Assert.Architecture.DoesNotAccessMember<DateTime>(types, nameof(DateTime.UtcNow), "the current time comes from TimeProvider");DoesNotDependOn overloads
DoesNotDependOn(IEnumerable<MemberElement>, IEnumerable<TypeElement>, string?, string?, string?)
ArchitectureAssert.Members.csAsserts that the members do not use any of the forbidden types, in their declaration or in their code.
public void DoesNotDependOn(IEnumerable<MemberElement> members, IEnumerable<TypeElement> forbiddenTypes, string? message = null, string? membersExpression = null, string? forbiddenTypesExpression = null)Parameters
membersIEnumerable<MemberElement>- The members to check.
forbiddenTypesIEnumerable<TypeElement>- The types that must not be used, such as the types of another layer.
messagestring?- The message that describes the failure.
membersExpressionstring?- The expression of
members. forbiddenTypesExpressionstring?- The expression of
forbiddenTypes.
Exceptions
AssertionException- A member uses a forbidden type, or
membersorforbiddenTypesis empty.
DoesNotDependOn(IEnumerable<MemberElement>, Func<TypeReference, bool>, string?, string?, string?)
ArchitectureAssert.Members.csAsserts that the members do not use any type that matches a predicate, including the types defined outside the loaded assemblies.
public void DoesNotDependOn(IEnumerable<MemberElement> members, Func<TypeReference, bool> predicate, string? message = null, string? membersExpression = null, string? predicateExpression = null)Parameters
membersIEnumerable<MemberElement>- The members to check.
predicateFunc<TypeReference, bool>- Returns
truefor a forbidden type, such ast => t.IsInNamespaceOrChildren("Microsoft.EntityFrameworkCore"). messagestring?- The message that describes the failure.
membersExpressionstring?- The expression of
members. predicateExpressionstring?- The expression of
predicate.
Exceptions
AssertionException- A member uses a forbidden type, or
membersis empty.
Example
var architecture = ArchitectureModel.FromAssemblyContaining<Order>();
var publicMembers = architecture.Types.Where(t => t.IsInNamespaceOrChildren("SampleApp") && t.IsPublic).SelectMany(t => t.Members).Where(member => member.IsPublic);
var infrastructure = architecture.Types.Where(t => t.IsInNamespaceOrChildren("SampleApp.Infrastructure"));
// The signatures and the code of the public members do not use Entity Framework Core
Assert.Architecture.DoesNotDependOn(publicMembers, t => t.IsInNamespaceOrChildren("Microsoft.EntityFrameworkCore"));
// The controllers get the infrastructure by injection: OrdersController.CreateRepository breaks the rule
var controllerMethods = architecture.Types.Where(t => t.IsInNamespace("SampleApp.Web")).SelectMany(t => t.Methods);
var exception = Assert.Throws<AssertionException>(() => Assert.Architecture.DoesNotDependOn(controllerMethods, infrastructure));
Assert.StartsWith("""
Assert.Architecture.DoesNotDependOn() assertion failed: 1 member depends on a forbidden type.
Members expression: controllerMethods
Forbidden expression: infrastructure
- SampleApp.Web.OrdersController.CreateRepository() depends on SampleApp.Infrastructure.SqlOrderRepository
SampleApp.Web.OrdersController.CreateRepository creates SampleApp.Infrastructure.SqlOrderRepository..ctor
""".ReplaceLineEndings("\n"), exception.Message);DoesNotDependOn(IEnumerable<MemberElement>, Type, string?, string?)
ArchitectureAssert.Members.csAsserts that the members do not use type, in their declaration or in their code.
public void DoesNotDependOn(IEnumerable<MemberElement> members, Type type, string? message = null, string? membersExpression = null)Parameters
membersIEnumerable<MemberElement>- The members to check.
typeType- The forbidden type, or a generic type definition (
typeof(List<>)) to forbid all its instantiations. messagestring?- The message that describes the failure.
membersExpressionstring?- The expression of
members.
Exceptions
AssertionException- A member uses
type, ormembersis empty.
DoesNotDependOn(IEnumerable<TypeElement>, IEnumerable<TypeElement>, string?, string?, string?)
ArchitectureAssert.csAsserts that the types do not use any of the forbidden types. The dependencies between the checked types themselves are ignored.
public void DoesNotDependOn(IEnumerable<TypeElement> types, IEnumerable<TypeElement> forbiddenTypes, string? message = null, string? typesExpression = null, string? forbiddenTypesExpression = null)Parameters
typesIEnumerable<TypeElement>- The types to check.
forbiddenTypesIEnumerable<TypeElement>- The types that must not be used, such as the types of another layer.
messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types. forbiddenTypesExpressionstring?- The expression of
forbiddenTypes.
Exceptions
AssertionException- A type uses a forbidden type, or
typesorforbiddenTypesis empty.
Example
var architecture = ArchitectureModel.FromAssemblyContaining<Order>();
var domain = architecture.Types.Where(t => t.IsInNamespaceOrChildren("SampleApp.Domain"));
var infrastructure = architecture.Types.Where(t => t.IsInNamespaceOrChildren("SampleApp.Infrastructure"));
Assert.Architecture.DoesNotDependOn(domain, infrastructure, "the domain stays persistence-ignorant");
Assert.Architecture.DoesNotDependOn(domain, t => t.IsInNamespaceOrChildren("Microsoft.EntityFrameworkCore"));
Assert.Architecture.DoesNotDependOn<HttpClient>(domain);DoesNotDependOn(IEnumerable<TypeElement>, Func<TypeReference, bool>, string?, string?, string?)
ArchitectureAssert.csAsserts that the types do not use any type that matches a predicate. Unlike the other overloads, the predicate also sees the types defined outside the loaded assemblies.
public void DoesNotDependOn(IEnumerable<TypeElement> types, Func<TypeReference, bool> predicate, string? message = null, string? typesExpression = null, string? predicateExpression = null)Parameters
typesIEnumerable<TypeElement>- The types to check.
predicateFunc<TypeReference, bool>- Returns
truefor a forbidden type, such ast => t.IsInNamespaceOrChildren("Microsoft.EntityFrameworkCore"). messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types. predicateExpressionstring?- The expression of
predicate.
Exceptions
AssertionException- A type uses a forbidden type, or
typesis empty.
DoesNotDependOn(IEnumerable<TypeElement>, Type, string?, string?)
ArchitectureAssert.csAsserts that the types do not use type.
public void DoesNotDependOn(IEnumerable<TypeElement> types, Type type, string? message = null, string? typesExpression = null)Parameters
typesIEnumerable<TypeElement>- The types to check.
typeType- The forbidden type, or a generic type definition (
typeof(List<>)) to forbid all its instantiations. messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types.
Exceptions
AssertionException- A type uses
type, ortypesis empty.
DoesNotDependOn<T>(IEnumerable<MemberElement>, string?, string?)
ArchitectureAssert.Members.csAsserts that the members do not use T, in their declaration or in their code.
public void DoesNotDependOn<T>(IEnumerable<MemberElement> members, string? message = null, string? membersExpression = null)Type parameters
T- The forbidden type.
Parameters
membersIEnumerable<MemberElement>- The members to check.
messagestring?- The message that describes the failure.
membersExpressionstring?- The expression of
members.
Exceptions
AssertionException- A member uses
T, ormembersis empty.
DoesNotDependOn<T>(IEnumerable<TypeElement>, string?, string?)
ArchitectureAssert.csAsserts that the types do not use T.
public void DoesNotDependOn<T>(IEnumerable<TypeElement> types, string? message = null, string? typesExpression = null)Type parameters
T- The forbidden type.
Parameters
typesIEnumerable<TypeElement>- The types to check.
messagestring?- The message that describes the failure.
typesExpressionstring?- The expression of
types.
Exceptions
AssertionException- A type uses
T, ortypesis empty.
DoesNotDependOnEachOther(IEnumerable<IGrouping<string, TypeElement>>, string?, string?)
ArchitectureAssert.csAsserts that the slices do not use each other at all. Each type that uses another slice is reported.
public void DoesNotDependOnEachOther(IEnumerable<IGrouping<string, TypeElement>> slices, string? message = null, string? slicesExpression = null)Parameters
slicesIEnumerable<IGrouping<string, TypeElement>>- The slices, such as the result of
TypeElementExtensions.SliceByNamespaceor ofGroupBy. A type must not be in several slices. messagestring?- The message that describes the failure.
slicesExpressionstring?- The expression of
slices.
Exceptions
AssertionException- A type uses another slice, or
slicesis empty. ArgumentException- Two slices have the same name, or a type is in several slices.
DoesNotHaveCycles(IEnumerable<IGrouping<string, TypeElement>>, string?, string?)
ArchitectureAssert.csAsserts that the slices do not depend on each other in a cycle (Orders → Billing → Orders). A slice may use another one. Each group of slices that depend on each other is reported once, with one of the shortest cycles and the dependencies that make it.
public void DoesNotHaveCycles(IEnumerable<IGrouping<string, TypeElement>> slices, string? message = null, string? slicesExpression = null)Parameters
slicesIEnumerable<IGrouping<string, TypeElement>>- The slices, such as the result of
TypeElementExtensions.SliceByNamespaceor ofGroupBy. A type must not be in several slices. messagestring?- The message that describes the failure.
slicesExpressionstring?- The expression of
slices.
Exceptions
AssertionException- The slices depend on each other in a cycle, or
slicesis empty. ArgumentException- Two slices have the same name, or a type is in several slices.
Example
var architecture = ArchitectureModel.FromAssemblyContaining<Order>();
var modules = architecture.Types.SliceByNamespace("SampleApp.Modules.{slice}");
var exception = Assert.Throws<AssertionException>(() => Assert.Architecture.DoesNotHaveCycles(modules));
Assert.StartsWith("""
Assert.Architecture.DoesNotHaveCycles() assertion failed: 1 cycle between slices.
Slices expression: modules
- Billing -> Shipping -> Billing
SampleApp.Modules.Billing.Invoice.Shipment has a property of type SampleApp.Modules.Shipping.Shipment
""".ReplaceLineEndings("\n"), exception.Message);