ArchitectureModel Class

The types of a set of assemblies, read from their files with System.Reflection.Metadata: the assemblies are not loaded for execution. The model is immutable, holds no file handle, and is safe to use from several threads, so it can be stored in a static field and shared by the tests.

public sealed class ArchitectureModel

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);

Properties

NameDescription
AssembliesGets the assemblies of the architecture.
TypesGets all the types of the architecture, including the nested types. The types the compiler generates (closures, state machines, anonymous types) are not included: their code belongs to the type that declares the lambda, the local function, the iterator or the async method. Filter them with LINQ and the members of TypeElement, such as architecture.Types.Where(t => t.IsInNamespaceOrChildren("MyApp.Domain")).

Gets the assemblies of the architecture.

public IReadOnlyList<AssemblyElement> Assemblies { get; }

Gets all the types of the architecture, including the nested types. The types the compiler generates (closures, state machines, anonymous types) are not included: their code belongs to the type that declares the lambda, the local function, the iterator or the async method. Filter them with LINQ and the members of TypeElement, such as architecture.Types.Where(t => t.IsInNamespaceOrChildren("MyApp.Domain")).

public IReadOnlyList<TypeElement> Types { get; }

Methods

NameDescription
FindType(string)Finds a type of the architecture by its full name as written in metadata.
FindType(Type)Finds the type of the architecture that corresponds to a Type.
FromAssemblies(IEnumerable<Assembly>)Loads the assemblies from the files they were loaded from.
FromAssemblyContaining<T>()Loads the assembly that defines T.
FromBytes(IEnumerable<ReadOnlyMemory<byte>>)Loads assemblies from memory, such as assemblies compiled at run time.
FromFiles(IEnumerable<string>)Loads assemblies from files.
GetTypeElement(Type)Gets the type of the architecture that corresponds to a Type.
Load(ArchitectureLoadOptions)Loads assemblies with the given options.

FindType overloads

FindType(string)

ArchitectureModel.cs

Finds a type of the architecture by its full name as written in metadata.

public TypeElement? FindType(string fullName)
Parameters
fullName string
The full name of the type: MyApp.Order, MyApp.Outer+Inner, MyApp.Repository`1.
Returns

TypeElement? The type, or null when no assembly of the architecture defines it.

FindType(Type)

ArchitectureModel.cs

Finds the type of the architecture that corresponds to a Type.

public TypeElement? FindType(Type type)
Parameters
type Type
The type, or a generic instantiation of it.
Returns

TypeElement? The type, or null when no assembly of the architecture defines it.

FromAssemblies(IEnumerable<Assembly>)

ArchitectureModel.cs

Loads the assemblies from the files they were loaded from.

public static ArchitectureModel FromAssemblies(params IEnumerable<Assembly> assemblies)

Parameters

assemblies IEnumerable<Assembly>
The assemblies. An assembly that has no file (single-file application, assembly loaded from memory) cannot be read: use ArchitectureModel.FromBytes.

Returns

ArchitectureModel The architecture.

Exceptions

ArgumentException
An assembly has no file.

FromAssemblyContaining<T>()

ArchitectureModel.cs

Loads the assembly that defines T.

public static ArchitectureModel FromAssemblyContaining<T>()

Type parameters

T
A type of the assembly.

Returns

ArchitectureModel The architecture.

FromBytes(IEnumerable<ReadOnlyMemory<byte>>)

ArchitectureModel.cs

Loads assemblies from memory, such as assemblies compiled at run time.

public static ArchitectureModel FromBytes(params IEnumerable<ReadOnlyMemory<byte>> images)

Parameters

images IEnumerable<ReadOnlyMemory<byte>>
The content of the assemblies (PE images). An embedded PDB is read from the image.

Returns

ArchitectureModel The architecture.

FromFiles(IEnumerable<string>)

ArchitectureModel.cs

Loads assemblies from files.

public static ArchitectureModel FromFiles(params IEnumerable<string> paths)

Parameters

paths IEnumerable<string>
The paths of the assemblies.

Returns

ArchitectureModel The architecture.

GetTypeElement(Type)

ArchitectureModel.cs

Gets the type of the architecture that corresponds to a Type.

public TypeElement GetTypeElement(Type type)

Parameters

type Type
The type, or a generic instantiation of it.

Returns

TypeElement The type.

Exceptions

ArgumentException
No assembly of the architecture defines the type.

Load(ArchitectureLoadOptions)

ArchitectureModel.cs

Loads assemblies with the given options.

public static ArchitectureModel Load(ArchitectureLoadOptions options)

Parameters

options ArchitectureLoadOptions
The assemblies to load and the loading options.

Returns

ArchitectureModel The architecture.

Exceptions

BadImageFormatException
A file is not a .NET assembly.