The Scoped lifetime ensures that there will be a single instance of the dependency for each scope.
interface IDependency
{
bool IsDisposed { get; }
}
class Dependency : IDependency, IDisposable
{
public bool IsDisposed { get; private set; }
public void Dispose() => IsDisposed = true;
}
interface IService
{
IDependency Dependency { get; }
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency => dependency;
}
// Implements a session
class Session(Composition composition) : Composition(composition);
class Program(Func<Session> sessionFactory)
{
public Session CreateSession() => sessionFactory();
}
partial class Composition
{
static void Setup() =>
DI.Setup()
// This hint indicates to not generate methods such as Resolve
.Hint(Hint.Resolve, "Off")
.Bind().As(Scoped).To<Dependency>()
.Bind().To<Service>()
// Session composition root
.Root<IService>("SessionRoot")
// Program composition root
.Root<Program>("ProgramRoot");
}
var composition = new Composition();
var program = composition.ProgramRoot;
// Creates session #1
var session1 = program.CreateSession();
var dependency1 = session1.SessionRoot.Dependency;
var dependency12 = session1.SessionRoot.Dependency;
// Checks the identity of scoped instances in the same session
dependency1.ShouldBe(dependency12);
// Creates session #2
var session2 = program.CreateSession();
var dependency2 = session2.SessionRoot.Dependency;
// Checks that the scoped instances are not identical in different sessions
dependency1.ShouldNotBe(dependency2);
// Disposes of session #1
session1.Dispose();
// Checks that the scoped instance is finalized
dependency1.IsDisposed.ShouldBeTrue();
// Disposes of session #2
session2.Dispose();
// Checks that the scoped instance is finalized
dependency2.IsDisposed.ShouldBeTrue();
The following partial class will be generated:
partial class Composition: IDisposable
{
private readonly Composition _root;
private readonly Lock _lock;
private object[] _disposables;
private int _disposeIndex;
private Dependency? _scopedDependency43;
[OrdinalAttribute(256)]
public Composition()
{
_root = this;
_lock = new Lock();
_disposables = new object[1];
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_lock = _root._lock;
_disposables = new object[1];
}
public IService SessionRoot
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_scopedDependency43 is null)
{
using (_lock.EnterScope())
{
if (_scopedDependency43 is null)
{
_scopedDependency43 = new Dependency();
_disposables[_disposeIndex++] = _scopedDependency43;
}
}
}
return new Service(_scopedDependency43!);
}
}
public Program ProgramRoot
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
Func<Session> perBlockFunc1 = new Func<Session>([MethodImpl(MethodImplOptions.AggressiveInlining)] () =>
{
Composition transientComposition3 = this;
Session localValue83 = new Session(transientComposition3);
return localValue83;
});
return new Program(perBlockFunc1);
}
}
public void Dispose()
{
int disposeIndex;
object[] disposables;
using (_lock.EnterScope())
{
disposeIndex = _disposeIndex;
_disposeIndex = 0;
disposables = _disposables;
_disposables = new object[1];
_scopedDependency43 = null;
}
while (disposeIndex-- > 0)
{
switch (disposables[disposeIndex])
{
case IDisposable disposableInstance:
try
{
disposableInstance.Dispose();
}
catch (Exception exception)
{
OnDisposeException(disposableInstance, exception);
}
break;
}
}
}
partial void OnDisposeException<T>(T disposableInstance, Exception exception) where T : IDisposable;
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Composition --|> IDisposable
Service --|> IService
Dependency --|> IDependency
Composition ..> Program : Program ProgramRoot
Composition ..> Service : IService SessionRoot
Program o-- "PerBlock" FuncᐸSessionᐳ : FuncᐸSessionᐳ
Service o-- "Scoped" Dependency : IDependency
FuncᐸSessionᐳ *-- Session : Session
Session *-- Composition : Composition
namespace Pure.DI.UsageTests.Lifetimes.ScopeScenario {
class Composition {
<<partial>>
+Program ProgramRoot
+IService SessionRoot
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Program {
}
class Service {
+Service(IDependency dependency)
}
class Session {
+Session(Composition composition)
}
}
namespace System {
class FuncᐸSessionᐳ {
<<delegate>>
}
class IDisposable {
<<abstract>>
}
}