Defines a lifestyle that caches instances during the lifetime of an explicitly defined scope using the
BeginScope
method. A scope is thread-specific, each thread should define its own scope. Scopes can be nested and
nested scopes will get their own instance. Instances created by this lifestyle can be disposed when
the created scope gets disposed.
Inheritance Hierarchy
SimpleInjectorLifestyle
SimpleInjectorScopedLifestyle
SimpleInjector.LifestylesThreadScopedLifestyle
Namespace: SimpleInjector.Lifestyles
Assembly: SimpleInjector (in SimpleInjector.dll) Version: 5.3.0
Syntax
The ThreadScopedLifestyle type exposes the following members.
Constructors
Name | Description | |
---|---|---|
ThreadScopedLifestyle | Initializes a new instance of the ThreadScopedLifestyle class.
The created and cached instance will be disposed when the created
Scope instance gets disposed and when the created object implements
IDisposable.
|
Properties
Name | Description | |
---|---|---|
Length |
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.
(Inherited from ScopedLifestyle.) | |
Name | Gets the user friendly name of this lifestyle. (Inherited from Lifestyle.) |
Methods
Name | Description | |
---|---|---|
BeginScope |
Begins a new scope for the given container.
Services, registered using the ThreadScopedLifestyle are cached during the
lifetime of that scope. The scope should be disposed explicitly.
| |
CreateCurrentScopeProvider |
Creates a delegate that upon invocation return the current Scope for this
lifestyle and the given container, or null when the delegate is executed outside
the context of such scope.
(Overrides ScopedLifestyleCreateCurrentScopeProvider(Container).) | |
CreateProducer(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.
(Inherited from Lifestyle.) | |
CreateProducerTService(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.
(Inherited from Lifestyle.) | |
CreateProducerTService(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.
(Inherited from Lifestyle.) | |
CreateProducerTService, 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.
(Inherited from Lifestyle.) | |
CreateRegistration(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.
(Inherited from Lifestyle.) | |
CreateRegistration(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.
(Inherited from Lifestyle.) | |
CreateRegistrationTConcrete(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.
(Inherited from Lifestyle.) | |
CreateRegistrationTService(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.
(Inherited from Lifestyle.) | |
CreateRegistrationCore(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.
(Inherited from ScopedLifestyle.) | |
CreateRegistrationCoreTService(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.
(Inherited from Lifestyle.) | |
CreateRegistrationCoreTService(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.
(Inherited from ScopedLifestyle.) | |
GetCurrentScope |
Returns the current Scope for this lifestyle and the given
container, or null when this method is executed outside the context of a scope.
(Inherited from ScopedLifestyle.) | |
GetCurrentScopeCore |
Returns the current Scope for this lifestyle and the given
container, or null when this method is executed outside the context of a scope.
(Overrides ScopedLifestyleGetCurrentScopeCore(Container).) | |
RegisterForDisposal |
Adds the disposable to the list of items that will get disposed when the
scope ends.
(Inherited from ScopedLifestyle.) | |
SetCurrentScope |
Sets the given scope as current scope in the given context. An existing scope
will be overridden and not disposed of. If the overridden scope must be disposed of, this
must be done manually.
(Inherited from ScopedLifestyle.) | |
SetCurrentScopeCore |
Sets the given scope as current scope in the given context.
(Overrides ScopedLifestyleSetCurrentScopeCore(Scope).) | |
WhenScopeEnds |
Allows registering an action delegate that will be called when the scope ends,
but before the scope disposes any instances.
(Inherited from ScopedLifestyle.) |
Examples
C#
var container = new Container(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); container.Register<IUnitOfWork, EntityFrameworkUnitOfWork>(Lifestyle.Scoped); using (container.BeginLifetimeScope()) { var instance1 = container.GetInstance<IUnitOfWork>(); // This call will return the same instance. var instance2 = container.GetInstance<IUnitOfWork>(); Assert.IsTrue(object.ReferenceEquals(instance1, instance2)); // Create a nested scope. using (container.BeginLifetimeScope()) { // A nested scope gets its own instance. var instance3 = container.GetInstance<IUnitOfWork>(); Assert.IsFalse(object.ReferenceEquals(instance1, instance3)); // This call will return the same instance. var instance4 = container.GetInstance<IUnitOfWork>(); Assert.IsTrue(object.ReferenceEquals(instance3, instance4)); } }
See Also