Skip to content

Attributes ​

This page shows all attributes of the packages. For how to use each attribute, refer to the guide pages.

Runtime attributes ​

Namespace: DependencyModules.Runtime.Attributes. Package: DependencyModules.Runtime.

[DependencyModule] ​

Identifies a partial class or a partial record as a module. Targets: class, assembly.

PropertyTypeDefaultFunction
OnlyRealmboolfalseIf the value is true, the module registers only the services that set Realm to this module and the classes that its conventions select.
UsingRegistrationTypeThe MSBuild property or AddThe registration type for the services of the module.
GenerateAttributebooltrueIf the value is false, the generator does not write a module attribute.
GenerateUseMethodstring?nullThe name of an IServiceCollection extension method that the generator writes for the module.
GenerateFactoriesboolThe MSBuild property or falseIf the value is true, the registrations of the module use generated factories.
RegisterJsonSerializersboolThe MSBuild property or falseIf the value is true, the module registers the classes that have [JsonSourceGenerationOptions] as IJsonTypeInfoResolver.

If the module does not set Using, GenerateFactories, or RegisterJsonSerializers, the generator uses the applicable MSBuild property. For the properties, refer to MSBuild properties.

Guide: Modules.

[SingletonService], [ScopedService], [TransientService] ​

Registers a class, or the return value of a static method, with the lifetime in the name. Targets: class, method. You can put more than one on a class.

PropertyTypeDefaultFunction
AsType?nullThe service type. Without As, the generator selects the service type.
Keyobject?nullThe key of a keyed registration.
UsingRegistrationTypeNot setThe registration type. If it is not set, the generator uses the value of the module, then the MSBuild property, then Add.
RealmType?nullThe module that registers the service.
Orderint0The position of the registration in the module.

Guide: Services.

[CrossWireService] ​

Registers a class as each interface that it declares and as the class type. All the registrations use the instance of the class registration. If the class declares no interface, the generator registers only the class type. Targets: class, method. On a static method, the generator registers the return type and each interface that the return type declares.

PropertyTypeDefaultFunction
LifetimeServiceLifetimeSingletonThe lifetime of the registrations.
Keyobject?nullThe key of all the registrations.
UsingRegistrationTypeNot setThe registration type of all the registrations. If it is not set, the generator uses the value of the module, then the MSBuild property, then Add.
RealmType?nullThe module that registers the service.

Guide: Cross-wired services.

[Decorator] ​

Identifies a class as a decorator. Targets: class.

PropertyTypeDefaultFunction
ServiceType?nullThe service type to decorate. Without Service, the generator uses the first constructor parameter with a type that the class implements, also through a base class.
Orderint0The position of the decorator. If the value of decorator A is less than the value of decorator B, B is the outer decorator.
RealmType?nullThe module that uses the decorator.
ImplementationType?nullThe implementation to decorate. Without Implementation, the decorator changes all registrations of the service type.

Guide: Decorators.

[Decorate] ​

Adds a decorator from a module. Targets: class (the module class). You can put more than one on a module.

Constructor parameter or propertyTypeFunction
serviceTypeThe service type to decorate.
decoratorTypeThe decorator type.
OrderintThe position of the decorator. The default is 0.

Guide: Decorate from a module.

[Intercept] ​

Adds interceptors to a class. Targets: class. You can put more than one on a class.

Constructor parameter or propertyTypeDefaultFunction
interceptorsparams Type[]Not applicableThe interceptor types. The first type gets the call first.
ServiceType?nullThe interface to intercept.
Orderint0The position in the decorator sequence.
RealmType?nullThe module that uses the interception.
LifetimeServiceLifetimeSingletonThe lifetime of the interceptor registrations.
MembersInterceptedMembersAllThe members to intercept.

Guide: Interception.

Environment attributes ​

Targets: class. Put them on a service class, on a [Decorator] class, or on a decorator that [Decorate] adds. You can also put them on a class that a convention selects, also in a referenced assembly.

AttributeConstructorMore than one on a class
[IfEnvironment](params string[] environmentNames)No
[IfNotEnvironment](params string[] environmentNames)No
[IfEnvironmentValue](string key) or (string key, string value)Yes
[IfNotEnvironmentValue](string key) or (string key, string value)Yes

Guide: Environments.

Enums ​

RegistrationType:

ValueGenerated call
AddAddSingleton, AddScoped, AddTransient, or the keyed methods.
TryTryAddSingleton, TryAddScoped, TryAddTransient, or the keyed methods.
TryEnumerableTryAddEnumerable
ReplaceReplace

InterceptedMembers is a flags enum: Methods = 1, Properties = 2, Indexers = 4, Events = 8, All = 15.

The generated module attribute ​

For each module, the generator writes an attribute with the name of the module and the suffix Attribute. The attribute is in the namespace of the module.

  • Targets: class, assembly, method, parameter. You can put more than one on an item.
  • The constructor has the constructor parameters of the module.
  • The properties are the public, internal, and protected internal properties of the module that have a set accessor. The set accessor must also be public, internal, or protected internal. The properties of a nested class are not included.
  • The attribute implements IDependencyModuleProvider.

Guide: Module dependencies.

Test attributes ​

Namespace: DependencyModules.Testing.Attributes. Package: DependencyModules.Testing.

AttributeTargetsFunction
[Mock]ParameterRegisters a mock for the parameter type and gives it to the parameter.
[InjectValues(params object[] value)]ParameterGives constructor arguments when the test package makes the parameter value.
[Shared]ParameterIdentifies the parameter as shared. The test package shares the parameters by default. It does not share IServiceProvider parameters or the types from IsolatedServices.
[TestExport(Type service)]Method, class, assemblyRegisters a service for the test. More than one is permitted.

[TestExport] properties:

PropertyTypeDefaultFunction
ImplementationType?The service typeThe class that the registration makes.
LifetimeServiceLifetimeTransientThe lifetime of the registration.
SharedboolfalseIf the value is true, the service providers from ITestContainerSource give the same instance.

Guide: Testing.

Test framework attributes ​

AttributeNamespaceTargetsConstructor
[ModuleTest] (xUnit)DependencyModules.xUnit.AttributesMethod(), (Type module), (params Type[] modules)
[ModuleTest] (NUnit)DependencyModules.NUnit.AttributesMethod(params Type[] modules)
[ModuleTestCase] (NUnit)DependencyModules.NUnit.AttributesMethod(params object?[] arguments)

[ModuleTestCase] has the TestName property. You can put more than one [ModuleTestCase] on a method.

Guide: xUnit, NUnit.

Mock support attributes ​

Targets: method, class, assembly.

AttributeNamespace
[NSubstituteSupport]DependencyModules.NSubstitute
[MoqSupport]DependencyModules.Moq
[FakeItEasySupport]DependencyModules.FakeItEasy

Guide: Mocks.

MIT license