LifestyleCreateHybrid Method (FuncBoolean, ScopedLifestyle, ScopedLifestyle)

Simple Injector
The hybrid lifestyle allows mixing two lifestyles in a single registration. Based on the supplied lifestyleSelector delegate the hybrid lifestyle will redirect the creation of the instance to the correct lifestyle. The result of the lifestyleSelector delegate will not be cached; it is invoked each time an instance is requested or injected. By nesting hybrid lifestyles, any number of lifestyles can be mixed.

Namespace:  SimpleInjector
Assembly:  SimpleInjector (in SimpleInjector.dll) Version: 5.3.0

public static ScopedLifestyle CreateHybrid(
	Func<bool> lifestyleSelector,
	ScopedLifestyle trueLifestyle,
	ScopedLifestyle falseLifestyle


Type: SystemFuncBoolean
The FuncTResult delegate that determines which lifestyle should be used. The trueLifestyle will be used if true is returned; the falseLifestyle otherwise. This delegate will be called every time an instance needs to be resolved or injected.
Type: SimpleInjectorScopedLifestyle
The scoped lifestyle to use when lifestyleSelector returns true.
Type: SimpleInjectorScopedLifestyle
The scoped lifestyle to use when lifestyleSelector returns false.

Return Value

Type: ScopedLifestyle
A new scoped hybrid lifestyle that wraps the supplied lifestyles.

ArgumentNullExceptionThrown when one of the supplied arguments is a null reference.

The following example shows the creation of a HybridLifestyle that mixes an WebRequestLifestyle and ThreadScopedLifestyle:

// NOTE: WebRequestLifestyle is located in SimpleInjector.Integration.Web.dll.
var mixedScopeLifestyle = Lifestyle.CreateHybrid(
    () => HttpContext.Current != null,
    new WebRequestLifestyle(),
    new ThreadScopedLifestyle());

// The created lifestyle can be reused for many registrations.
container.Register<IUserRepository, SqlUserRepository>(mixedScopeLifestyle);
container.Register<ICustomerRepository, SqlCustomerRepository>(mixedScopeLifestyle);
See Also