Skip to content

Container configuration

Peter Csajtai edited this page Jun 11, 2020 · 30 revisions

When you are creating a new instance of Stashbox you can use an action delegate to customize it e.g.:

var container = new StashboxContainer(config => config
	.WithDisposableTransientTracking()
	.WithConstructorSelectionRule(Rules.ConstructorSelection.PreferLeastParameters)
        .WithRegistrationBehavior(Rules.RegistrationBehavior.ThrowException)
	//etc...
);

The re-configuration of the container is also supported by calling the container.Configure(...) method.

Options available

  • WithDisposableTransientTracking() - Enables or disables the tracking of disposable transient objects.

  • WithRuntimeCircularDependencyTracking() - Enables or disables the runtime circular dependency tracking.

    By default the container checks for circular dependencies when it builds the expression graph, but this could not prevent stack overflows when factory delegates passed by the user are containing circular dependencies. If you turn this feature on, the container will generate checking nodes into the expression graph which allows the tracking of circular dependencies in user-defined factory delegates.

  • WithRegistrationBehavior(behavior) - Represents the actual behavior used when a new service is going to be registered into the container. These options does not affect named registrations.

    • SkipDuplications - The container will skip new registrations when the given implementation type is already registered.
    • ThrowException - The container will throw a ServiceAlreadyRegisteredException when the given implementation type is already registered.
    • ReplaceExisting - The container will replace the already registered service with the given one when they have the same implementation type.
    • PreserveDuplications - The container will keep registering the new services with the same implementation type.
  • WithCircularDependencyWithLazy() - Allows circular dependencies through Lazy<> objects.

  • WithDefaultValueInjection() - Enables or disables the default value injection.

  • WithUnknownTypeResolution(configurator) - When the container finds an unknown but a resolvable type (not an interface, abstract or sealed class), it'll try to register that type and then resolves it. You can also use a configurator delegate to control how the unknown type registrations should behave e.g: config.WithUnknownTypeResolution(context => context.AsImplementedTypes())

  • WithAutoMemberInjection(rule, filter) - Enables the auto member injection without attributes.

    • PropertiesWithPublicSetter - With this flag the container will perform auto-injection on properties with public setters.
    • PropertiesWithLimitedAccess - With this flag the container will perform auto-injection on properties even when they don't have a public setter.
    • PrivateFields - With this flag the container will perform auto-injection on private fields too.

    Member selection filter: config.WithAutoMemberInjection(filter: member => member.Type != typeof(IDrow))

  • WithConstructorSelectionRule(selectionRule) - Sets the constructor selection rule.

    • PreferMostParameters - Prefers the constructor which has the longest parameter list.
    • PreferLeastParameters - Prefers the constructor which has the shortest parameter list.
  • TreatParameterAndMemberNameAsDependencyName() - Enables conventional resolution, which means the container treats the constructor/method parameter or member names as dependency names used by named resolution.

  • WithNamedDependencyResolutionForUnNamedRequests() - Enables the resolution of a named registration when a request ha been made without dependency name but with the same type.

  • WithExpressionCompiler() - Forces the usage of an external expression tree compiler. This can be useful on platforms where the IL generator modules are not available, therefore the expression compiler in Stashbox couldn't work.

    Usage of the Microsoft expression compiler: new StashboxContainer(c => c.WithExpressionCompiler(Rules.ExpressionCompilers.MicrosoftExpressionCompiler))

  • WithDefaultLifetime(lifetime) - Sets the default lifetime, used when a service doesn't have a configured one.

    Usage example: new StashboxContainer(c => c.WithDefaultLifetime(new ScopedLifetime())).

  • WithLifetimeValidation() - Enables the life-span and root resolution validation on the dependency tree.