Skip to content

Attributes

Every attribute this library defines, with its properties — for looking one up once you know what you are after. If you are working out which attribute you want, the guide covers that: registering services, modules, decorators and environments.

All of them live in DependencyModules.Runtime.Attributes.

Modules

[DependencyModule]

Marks a partial class as a module. Generates an attribute of the same name for composition.

Property
OnlyRealmthe module takes only registrations that named it as their realm
GenerateAttributeset false to suppress the generated composition attribute
RegisterJsonSerializersregister discovered JsonSerializerContext types

[Decorate(service, decorator)]

Declares a decorator on the module rather than on the decorator class — for when the service, the decorator or both come from an assembly you do not control.

Property
Ordernesting; lower sits closer to the implementation

Services

[SingletonService] · [ScopedService] · [TransientService]

Registers the class, or a static factory method, with that lifetime.

Property
Asthe service type to register as
Keya service key
UsingAdd, Try, TryEnumerable or Replace
Realmscope the registration to one module

[CrossWireService]

Registers the implementation and every interface it declares, sharing one instance.

Takes the same properties, plus Lifetime.

Decoration and interception

[Decorator]

Marks a class as a decorator of the interface it implements. The first constructor parameter is the wrapped instance; the rest are resolved from the container.

A [Decorator] is never a convention candidate — it is not a service.

Property
Ordernesting; lower sits closer to the implementation
Servicethe decorated interface, when it cannot be inferred
Realmrestrict the decorator to one module, matching Realm on the service attributes

An unrestricted decorator belongs to every module that is not OnlyRealm, exactly as an unrestricted service registration does — so a decorator with no Realm is not picked up by an OnlyRealm module.

[Intercept(params Type[])]

Wraps a service so every call through its interface passes through the given interceptors, in order.

Property
Servicethe interface to intercept, when the service implements more than one
Ordernesting relative to decorators and other interceptors

Environment conditions

All four take effect on a service registered by attribute or by convention, and on a [Decorator] — where a condition that does not hold means the decorator is never applied. Conditions of different kinds combine with and.

The same tests are available on a convention itself, as IfEnvironment(…) and friends.

[IfEnvironment(params string[])] · [IfNotEnvironment(params string[])]

Compares the environment name, case-insensitively.

[IfEnvironmentValue(key)] · [IfEnvironmentValue(key, value)]

Presence of the key, or an exact ordinal match on its value. AllowMultiple.

[IfNotEnvironmentValue(…)]

The inverse of either form.

Released under the MIT License.