Container configuration
The container's constructor has an Action<T>
parameter used to configure its behavior.
The configuration API is fluent, which means you can chain the configuration methods after each other.
var container = new StashboxContainer(options => options
.WithDisposableTransientTracking()
.WithConstructorSelectionRule(Rules.ConstructorSelection.PreferLeastParameters)
.WithRegistrationBehavior(Rules.RegistrationBehavior.ThrowException));
Re-configuration of the container is also supported by calling its .Configure()
method.
var container = new StashboxContainer();
container.Configure(options => options.WithDisposableTransientTracking());
Default configuration
These features are set by default:
- Constructor selection:
Rules.ConstructorSelection.PreferMostParameters
- Registration behavior:
Rules.RegistrationBehavior.SkipDuplications
- Default lifetime:
Lifetimes.Transient
Tracking disposable transients
With this option, you can enable or disable the tracking of disposable transient objects.
new StashboxContainer(options => options
.WithDisposableTransientTracking());
Auto member-injection
With this option, you can enable or disable the auto member-injection without attributes.
PropertiesWithPublicSetter
With this flag, the container will perform auto-injection on properties with public setters.
new StashboxContainer(options => options
.WithAutoMemberInjection(
Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter));
PropertiesWithLimitedAccess
With this flag, the container will perform auto-injection on properties even when they don't have a public setter.
new StashboxContainer(options => options
.WithAutoMemberInjection(
Rules.AutoMemberInjectionRules.PropertiesWithLimitedAccess));
PrivateFields
With this flag, the container will perform auto-injection on private fields too.
new StashboxContainer(options => options
.WithAutoMemberInjection(
Rules.AutoMemberInjectionRules.PrivateFields));
Combined rules
You can also combine these flags with bitwise logical operators to get a merged ruleset.
new StashboxContainer(options => options
.WithAutoMemberInjection(Rules.AutoMemberInjectionRules.PrivateFields |
Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter));
Member selection filter
You can pass your own member selection logic to control which members should be auto injected.
new StashboxContainer(options => options
.WithAutoMemberInjection(
filter: member => member.Type != typeof(ILogger)));
Required member injection
With this option, you can enable or disable the auto injection of members defined with C# 11's required
keyword.
new StashboxContainer(options => options
.WithRequiredMemberInjection(enabled: false));
The required member injection option is enabled by default.
Constructor selection
With this option, you can set the constructor selection rule used to determine which constructor the container should use for instantiation.
PreferMostParameters
It prefers the constructor which has the most extended parameter list.
new StashboxContainer(options => options
.WithConstructorSelectionRule(
Rules.ConstructorSelection.PreferMostParameters));
PreferLeastParameters
It prefers the constructor which has the shortest parameter list.
new StashboxContainer(options => options
.WithConstructorSelectionRule(
Rules.ConstructorSelection.PreferLeastParameters));
Registration behavior
With this option, you can set the actual behavior used when a new service is registered into the container. These options do not affect named registrations.
SkipDuplications
The container will skip new registrations when the given implementation type is already registered.
new StashboxContainer(options => options
.WithRegistrationBehavior(
Rules.RegistrationBehavior.SkipDuplications));
ThrowException
The container throws an exception when the given implementation type is already registered.
new StashboxContainer(options => options
.WithRegistrationBehavior(
Rules.RegistrationBehavior.ThrowException));
ReplaceExisting
The container will replace the already registered service with the given one when they have the same implementation type.
new StashboxContainer(options => options
.WithRegistrationBehavior(
Rules.RegistrationBehavior.ReplaceExisting));
PreserveDuplications
The container will keep registering the new services with the same implementation type.
new StashboxContainer(options => options
.WithRegistrationBehavior(
Rules.RegistrationBehavior.PreserveDuplications));
Default lifetime
With this option, you can set the default lifetime used when a service doesn't have a configured one.
new StashboxContainer(options => options
.WithDefaultLifetime(Lifetimes.Scoped));
Lifetime validation
With this option, you can enable or disable the life-span and root scope resolution validation on the dependency tree.
new StashboxContainer(options => options
.WithLifetimeValidation());
Generic variance
With this option, you can enable or disable the check for generic covariance and contravariance during the resolution of generic type collections.
This option is enabled by default.
new StashboxContainer(options => options
.WithVariantGenericTypes());
Empty collection handling
With this option, you can enable or disable the throwing of a ResolutionFailedException
when no services are found for a collection resolution request. When this feature is disabled (default), the container returns an empty array for those request.
new StashboxContainer(options => options
.WithExceptionOverEmptyCollection());
Conventional resolution
With this option, you can enable or disable conventional resolution, which means the container treats the constructor/method parameter or member names as dependency names used by named resolution.
new StashboxContainer(options => options
.TreatParameterAndMemberNameAsDependencyName());
Using named service for un-named requests
With this option, you can enable or disable the selection of named registrations when the resolution request is un-named but with the same type.
new StashboxContainer(options => options
.WithNamedDependencyResolutionForUnNamedRequests());
Named service resolution
WithUniversalName
Sets the universal name that represents a special name which allows named resolution work for any given name.
new StashboxContainer(options => options
.WithUniversalName("Any"));
WithAdditionalDependencyNameAttribute
Adds an attribute type that is considered a dependency name indicator just like the DependencyName
attribute.
new StashboxContainer(options => options
.WithAdditionalDependencyNameAttribute<CustomNameAttribute>());
WithAdditionalDependencyAttribute
Adds an attribute type that is considered a dependency indicator just like the Dependency
attribute.
new StashboxContainer(options => options
.WithAdditionalDependencyAttribute<CustomDependencyAttribute>());
Default value injection
With this option, you can enable or disable the default value injection.
new StashboxContainer(options => options
.WithDefaultValueInjection());
Unknown type resolution
With this option, you can enable or disable the resolution of unregistered types. You can also use a configurator delegate to configure the registrations the container will create from the unknown types.
new StashboxContainer(options => options
.WithUnknownTypeResolution(config => config.AsImplementedTypes()));
Custom compiler
With this option, you can set an external expression tree compiler. It can be useful on platforms where the IL generator modules are not available; therefore, the expression compiler in Stashbox couldn't work.
new StashboxContainer(options => options
.WithExpressionCompiler(
Rules.ExpressionCompilers.MicrosoftExpressionCompiler));
Re-build singletons in child containers
With this option, you can enable or disable the re-building of singletons in child containers. It allows the child containers to override singleton dependencies in the parent.
new StashboxContainer(options => options
.WithReBuildSingletonsInChildContainer());
This feature is not affecting the already built singleton instances in the parent.