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 ArchitectureAssert

Remarks

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

NameDescription
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.cs

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.

public void AllMatch(IEnumerable<TypeElement> types, Func<TypeElement, bool> predicate, string? message = null, string? typesExpression = null, string? predicateExpression = null)
Parameters
types IEnumerable<TypeElement>
The types to check.
predicate Func<TypeElement, bool>
Returns true for a valid type, such as t => t.IsSealed.
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
predicateExpression string?
The expression of predicate.
Exceptions
AssertionException
A type does not match the predicate, or types is 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.cs

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.

public void AllMatch<TMember>(IEnumerable<TMember> members, Func<TMember, bool> predicate, string? message = null, string? membersExpression = null, string? predicateExpression = null) where TMember : MemberElement
Type parameters
TMember
The type of the members: MethodElement, PropertyElement, FieldElement, EventElement or MemberElement.
Parameters
members IEnumerable<TMember>
The members to check, such as types.SelectMany(t => t.Methods).
predicate Func<TMember, bool>
Returns true for a valid member, such as method => method.IsAsync.
message string?
The message that describes the failure.
membersExpression string?
The expression of members.
predicateExpression string?
The expression of predicate.
Exceptions
AssertionException
A member does not match the predicate, or members is 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.cs

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.

public void DependsOnlyOn(IEnumerable<MemberElement> members, Func<TypeReference, bool> predicate, string? message = null, string? membersExpression = null, string? predicateExpression = null)
Parameters
members IEnumerable<MemberElement>
The members to check.
predicate Func<TypeReference, bool>
Returns true for an allowed type, such as t => t.IsInNamespaceOrChildren("System").
message string?
The message that describes the failure.
membersExpression string?
The expression of members.
predicateExpression string?
The expression of predicate.
Exceptions
AssertionException
A member uses a type that is not allowed, or members is empty.

DependsOnlyOn(IEnumerable<TypeElement>, IEnumerable<TypeElement>, string?, string?, string?)

ArchitectureAssert.cs

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.

public void DependsOnlyOn(IEnumerable<TypeElement> types, IEnumerable<TypeElement> allowedTypes, string? message = null, string? typesExpression = null, string? allowedTypesExpression = null)
Parameters
types IEnumerable<TypeElement>
The types to check.
allowedTypes IEnumerable<TypeElement>
The types that may be used.
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
allowedTypesExpression string?
The expression of allowedTypes.
Exceptions
AssertionException
A type uses a type that is not allowed, or types is empty.

DependsOnlyOn(IEnumerable<TypeElement>, Func<TypeReference, bool>, string?, string?, string?)

ArchitectureAssert.cs

Asserts 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
types IEnumerable<TypeElement>
The types to check.
predicate Func<TypeReference, bool>
Returns true for an allowed type, such as t => t.IsInNamespaceOrChildren("System").
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
predicateExpression string?
The expression of predicate.
Exceptions
AssertionException
A type uses a type that is not allowed, or types is 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.cs

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.

public void DoesNotAccessMember(IEnumerable<MemberElement> members, Type type, string memberName, string? message = null, string? membersExpression = null)
Parameters
members IEnumerable<MemberElement>
The members to check.
type Type
The type that declares the member.
memberName string
The name of the member (nameof(DateTime.Now)), or .ctor for the constructors.
message string?
The message that describes the failure.
membersExpression string?
The expression of members.
Exceptions
AssertionException
A member uses the member, or members is empty.

DoesNotAccessMember(IEnumerable<TypeElement>, Type, string, string?, string?)

ArchitectureAssert.cs

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.

public void DoesNotAccessMember(IEnumerable<TypeElement> types, Type type, string memberName, string? message = null, string? typesExpression = null)
Parameters
types IEnumerable<TypeElement>
The types to check.
type Type
The type that declares the member.
memberName string
The name of the member (nameof(DateTime.Now)), or .ctor for the constructors.
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
Exceptions
AssertionException
A type uses the member, or types is empty.

DoesNotAccessMember<T>(IEnumerable<MemberElement>, string, string?, string?)

ArchitectureAssert.Members.cs

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.

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
members IEnumerable<MemberElement>
The members to check.
memberName string
The name of the member (nameof(DateTime.Now)), or .ctor for the constructors.
message string?
The message that describes the failure.
membersExpression string?
The expression of members.
Exceptions
AssertionException
A member uses the member, or members is empty.

DoesNotAccessMember<T>(IEnumerable<TypeElement>, string, string?, string?)

ArchitectureAssert.cs

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.

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
types IEnumerable<TypeElement>
The types to check.
memberName string
The name of the member (nameof(DateTime.Now)), or .ctor for the constructors.
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
Exceptions
AssertionException
A type uses the member, or types is 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.cs

Asserts 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
members IEnumerable<MemberElement>
The members to check.
forbiddenTypes IEnumerable<TypeElement>
The types that must not be used, such as the types of another layer.
message string?
The message that describes the failure.
membersExpression string?
The expression of members.
forbiddenTypesExpression string?
The expression of forbiddenTypes.
Exceptions
AssertionException
A member uses a forbidden type, or members or forbiddenTypes is empty.

DoesNotDependOn(IEnumerable<MemberElement>, Func<TypeReference, bool>, string?, string?, string?)

ArchitectureAssert.Members.cs

Asserts 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
members IEnumerable<MemberElement>
The members to check.
predicate Func<TypeReference, bool>
Returns true for a forbidden type, such as t => t.IsInNamespaceOrChildren("Microsoft.EntityFrameworkCore").
message string?
The message that describes the failure.
membersExpression string?
The expression of members.
predicateExpression string?
The expression of predicate.
Exceptions
AssertionException
A member uses a forbidden type, or members is 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.cs

Asserts 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
members IEnumerable<MemberElement>
The members to check.
type Type
The forbidden type, or a generic type definition (typeof(List<>)) to forbid all its instantiations.
message string?
The message that describes the failure.
membersExpression string?
The expression of members.
Exceptions
AssertionException
A member uses type, or members is empty.

DoesNotDependOn(IEnumerable<TypeElement>, IEnumerable<TypeElement>, string?, string?, string?)

ArchitectureAssert.cs

Asserts 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
types IEnumerable<TypeElement>
The types to check.
forbiddenTypes IEnumerable<TypeElement>
The types that must not be used, such as the types of another layer.
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
forbiddenTypesExpression string?
The expression of forbiddenTypes.
Exceptions
AssertionException
A type uses a forbidden type, or types or forbiddenTypes is 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.cs

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.

public void DoesNotDependOn(IEnumerable<TypeElement> types, Func<TypeReference, bool> predicate, string? message = null, string? typesExpression = null, string? predicateExpression = null)
Parameters
types IEnumerable<TypeElement>
The types to check.
predicate Func<TypeReference, bool>
Returns true for a forbidden type, such as t => t.IsInNamespaceOrChildren("Microsoft.EntityFrameworkCore").
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
predicateExpression string?
The expression of predicate.
Exceptions
AssertionException
A type uses a forbidden type, or types is empty.

DoesNotDependOn(IEnumerable<TypeElement>, Type, string?, string?)

ArchitectureAssert.cs

Asserts that the types do not use type.

public void DoesNotDependOn(IEnumerable<TypeElement> types, Type type, string? message = null, string? typesExpression = null)
Parameters
types IEnumerable<TypeElement>
The types to check.
type Type
The forbidden type, or a generic type definition (typeof(List<>)) to forbid all its instantiations.
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
Exceptions
AssertionException
A type uses type, or types is empty.

DoesNotDependOn<T>(IEnumerable<MemberElement>, string?, string?)

ArchitectureAssert.Members.cs

Asserts 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
members IEnumerable<MemberElement>
The members to check.
message string?
The message that describes the failure.
membersExpression string?
The expression of members.
Exceptions
AssertionException
A member uses T, or members is empty.

DoesNotDependOn<T>(IEnumerable<TypeElement>, string?, string?)

ArchitectureAssert.cs

Asserts 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
types IEnumerable<TypeElement>
The types to check.
message string?
The message that describes the failure.
typesExpression string?
The expression of types.
Exceptions
AssertionException
A type uses T, or types is empty.

DoesNotDependOnEachOther(IEnumerable<IGrouping<string, TypeElement>>, string?, string?)

ArchitectureAssert.cs

Asserts 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

slices IEnumerable<IGrouping<string, TypeElement>>
The slices, such as the result of TypeElementExtensions.SliceByNamespace or of GroupBy. A type must not be in several slices.
message string?
The message that describes the failure.
slicesExpression string?
The expression of slices.

Exceptions

AssertionException
A type uses another slice, or slices is empty.
ArgumentException
Two slices have the same name, or a type is in several slices.

DoesNotHaveCycles(IEnumerable<IGrouping<string, TypeElement>>, string?, string?)

ArchitectureAssert.cs

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.

public void DoesNotHaveCycles(IEnumerable<IGrouping<string, TypeElement>> slices, string? message = null, string? slicesExpression = null)

Parameters

slices IEnumerable<IGrouping<string, TypeElement>>
The slices, such as the result of TypeElementExtensions.SliceByNamespace or of GroupBy. A type must not be in several slices.
message string?
The message that describes the failure.
slicesExpression string?
The expression of slices.

Exceptions

AssertionException
The slices depend on each other in a cycle, or slices is 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);