Lifestyle Class

Simple Injector
Instances returned from the container can be cached. The Container contains several overloads of the Register method that take a Lifestyle instance as argument to define how returned instances should be cached. The core library contains two lifestyles out of the box. By supplying Lifestyle.Transient, the registered instance is not cached; a new instance is returned every time it is requested or injected. By supplying Lifestyle.Singleton instances can be cached indefinitely; only a single instance of the registered component will be returned by that container instance. Other lifestyles are defined in integration and extension packages. The CreateCustom method allows defining a custom lifestyle and the CreateHybrid method allows creating a lifestyle that mixes multiple other lifestyles.
Inheritance Hierarchy

SystemObject
  SimpleInjectorLifestyle
    SimpleInjector.LifestylesSingletonLifestyle
    SimpleInjectorScopedLifestyle

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

public abstract class Lifestyle

The Lifestyle type exposes the following members.

Constructors

  NameDescription
Protected methodLifestyle
Initializes a new instance of the Lifestyle class.
Top
Properties

  NameDescription
Public propertyLength
Gets the length of the lifestyle. Implementers must implement this property. The diagnostic services use this value to compare lifestyles with each other to determine lifestyle misconfigurations.
Public propertyName
Gets the user friendly name of this lifestyle.
Top
Methods

  NameDescription
Public methodStatic memberCode exampleCreateCustom
Creates a custom lifestyle using the supplied lifestyleApplierFactory delegate.
Public methodStatic memberCode exampleCreateHybrid(ScopedLifestyle, Lifestyle)
The hybrid lifestyle allows mixing two lifestyles in a single registration. The hybrid will use the defaultLifestyle in case its GetCurrentScope method returns a scope; otherwise the fallbackLifestyle is used. The hybrid lifestyle will redirect the creation of the instance to the selected lifestyle. By nesting hybrid lifestyles, any number of lifestyles can be mixed.
Public methodStatic memberCode exampleCreateHybrid(ScopedLifestyle, ScopedLifestyle)
The hybrid lifestyle allows mixing two lifestyles in a single registration. The hybrid will use the defaultLifestyle in case its GetCurrentScope method returns a scope; otherwise the fallbackLifestyle is used. The hybrid lifestyle will redirect the creation of the instance to the selected lifestyle. By nesting hybrid lifestyles, any number of lifestyles can be mixed.
Public methodStatic memberCode exampleCreateHybrid(FuncBoolean, Lifestyle, Lifestyle)
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.
Public methodStatic memberCode exampleCreateHybrid(FuncBoolean, ScopedLifestyle, ScopedLifestyle)
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.
Public methodCreateProducer(Type, Type, Container)
Creates a new InstanceProducer instance for the given serviceType that will create new instances of specified implementationType with the caching as specified by this lifestyle.
Public methodCreateProducerTService(FuncTService, Container)
Creates a new InstanceProducer instance for the given TService that will create new instances instance using the supplied instanceCreator with the caching as specified by this lifestyle.
Public methodCreateProducerTService(Type, Container)
Creates a new InstanceProducer instance for the given TService that will create new instances of specified implementationType caching as specified by this lifestyle.
Public methodCreateProducerTService, TImplementation(Container)
Creates a new InstanceProducer instance for the given TService that will create new instances of specified TImplementation with the caching as specified by this lifestyle.
Public methodCreateRegistration(Type, Container)
Creates a new Registration instance defining the creation of the specified concreteType with the caching as specified by this lifestyle, or returns an already created Registration instance for this container + lifestyle + type combination. This method might fail when run in a partial trust sandbox when concreteType is an internal type.
Public methodCreateRegistration(Type, FuncObject, Container)
Creates a new Registration instance defining the creation of the specified serviceType using the supplied instanceCreator with the caching as specified by this lifestyle.
Public methodCreateRegistrationTConcrete(Container)
Creates a new Registration instance defining the creation of the specified TConcrete with the caching as specified by this lifestyle, or returns an already created Registration instance for this container + lifestyle + type combination.
Public methodCreateRegistrationTService(FuncTService, Container)
Creates a new Registration instance defining the creation of the specified TService using the supplied instanceCreator with the caching as specified by this lifestyle.
Protected methodCreateRegistrationCore(Type, Container)
When overridden in a derived class, creates a new Registration instance defining the creation of the specified concreteType with the caching as specified by this lifestyle.
Protected methodCreateRegistrationCoreTService(FuncTService, Container)
When overridden in a derived class, creates a new Registration instance defining the creation of the specified TService using the supplied instanceCreator with the caching as specified by this lifestyle.
Top
Fields

  NameDescription
Public fieldStatic memberCode exampleScoped

The lifestyle that caches components according to the lifetime of the container's configured scoped lifestyle.

In case the type of a cached instance implements IDisposable, the container will ensure its disposal when the active scope gets disposed.

Public fieldStatic memberCode exampleSingleton

The lifestyle that caches components during the lifetime of the Container instance and guarantees that only a single instance of that component is created for that instance. Since general use is to create a single Container instance for the lifetime of the application / AppDomain, this would mean that only a single instance of that component would exist during the lifetime of the application. In a multi-threaded applications, implementations registered using this lifestyle must be thread-safe.

In case the type of a cached instance implements IDisposable, the container will ensure its disposal when the container gets disposed.

Public fieldStatic memberCode exampleTransient
The lifestyle instance that doesn't cache instances. A new instance of the specified component is created every time the registered service is requested or injected.
Top
Remarks

This type is abstract and can be overridden to implement a custom lifestyle.
See Also

Reference