TypeElementExtensions Class

Provides extension methods on collections of TypeElement.

public static class TypeElementExtensions

Methods

NameDescription
SliceByNamespace(IEnumerable<TypeElement>, string)Groups types into slices by namespace, such as the modules of an application, to check the dependencies between the slices.

SliceByNamespace(IEnumerable<TypeElement>, string)

TypeElementExtensions.cs

Groups types into slices by namespace, such as the modules of an application, to check the dependencies between the slices.

public static IReadOnlyList<IGrouping<string, TypeElement>> SliceByNamespace(this IEnumerable<TypeElement> types, string pattern)

Parameters

types IEnumerable<TypeElement>
The types to group.
pattern string
A namespace pattern with one {slice} segment, whose value names the slice: MyApp.Modules.{slice} puts MyApp.Modules.Orders and MyApp.Modules.Orders.Data in the slice Orders. A * segment matches any segment. The types whose namespace does not match the pattern are in no slice.

Returns

IReadOnlyList<IGrouping<string, TypeElement>> The slices, sorted by name, each with its types in the order of types.

Exceptions

ArgumentException
The pattern does not have exactly one {slice} segment.

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