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 ArchitectureModelExample
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
| Name | Description |
|---|---|
| Assemblies | Gets the assemblies of the architecture. |
| Types | 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")). |
Assemblies
ArchitectureModel.csGets the assemblies of the architecture.
public IReadOnlyList<AssemblyElement> Assemblies { get; }Property value
Types
ArchitectureModel.csGets 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; }Property value
Methods
| Name | Description |
|---|---|
| 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.csFinds a type of the architecture by its full name as written in metadata.
public TypeElement? FindType(string fullName)Parameters
fullNamestring- 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.csFinds the type of the architecture that corresponds to a Type.
public TypeElement? FindType(Type type)Parameters
typeType- 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.csLoads the assemblies from the files they were loaded from.
public static ArchitectureModel FromAssemblies(params IEnumerable<Assembly> assemblies)Parameters
assembliesIEnumerable<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.csLoads the assembly that defines T.
public static ArchitectureModel FromAssemblyContaining<T>()FromBytes(IEnumerable<ReadOnlyMemory<byte>>)
ArchitectureModel.csLoads assemblies from memory, such as assemblies compiled at run time.
public static ArchitectureModel FromBytes(params IEnumerable<ReadOnlyMemory<byte>> images)Parameters
imagesIEnumerable<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.csLoads assemblies from files.
public static ArchitectureModel FromFiles(params IEnumerable<string> paths)Parameters
pathsIEnumerable<string>- The paths of the assemblies.
Returns
ArchitectureModel The architecture.
GetTypeElement(Type)
ArchitectureModel.csGets the type of the architecture that corresponds to a Type.
public TypeElement GetTypeElement(Type type)Parameters
typeType- 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.csLoads assemblies with the given options.
public static ArchitectureModel Load(ArchitectureLoadOptions options)Parameters
optionsArchitectureLoadOptions- The assemblies to load and the loading options.
Returns
ArchitectureModel The architecture.
Exceptions
BadImageFormatException- A file is not a .NET assembly.