The Singleton lifetime ensures that there will be a single instance of the dependency for each composition.
interface IDependency;
class Dependency : IDependency;
interface IService
{
public IDependency Dependency1 { get; }
public IDependency Dependency2 { get; }
}
class Service(
IDependency dependency1,
IDependency dependency2)
: IService
{
public IDependency Dependency1 { get; } = dependency1;
public IDependency Dependency2 { get; } = dependency2;
}
DI.Setup(nameof(Composition))
// This hint indicates to not generate methods such as Resolve
.Hint(Hint.Resolve, "Off")
.Bind().As(Lifetime.Singleton).To<Dependency>()
.Bind().To<Service>()
.Root<IService>("Root");
var composition = new Composition();
var service1 = composition.Root;
var service2 = composition.Root;
service1.Dependency1.ShouldBe(service1.Dependency2);
service2.Dependency1.ShouldBe(service1.Dependency1);
Some articles advise using objects with a Singleton lifetime as often as possible, but the following details must be considered:
-
For .NET the default behavior is to create a new instance of the type each time it is needed, other behavior requires, additional logic that is not free and requires additional resources.
-
The use of Singleton, adds a requirement for thread-safety controls on their use, since singletons are more likely to share their state between different threads without even realizing it.
-
The thread-safety control should be automatically extended to all dependencies that Singleton uses, since their state is also now shared.
-
Logic for thread-safety control can be resource-costly, error-prone, interlocking, and difficult to test.
-
Singleton can retain dependency references longer than their expected lifetime, this is especially significant for objects that hold "non-renewable" resources, such as the operating system Handler.
-
Sometimes additional logic is required to dispose of Singleton.
The following partial class will be generated:
partial class Composition
{
private readonly Composition _root;
private readonly Lock _lock;
private Dependency? _singletonDependency43;
[OrdinalAttribute(256)]
public Composition()
{
_root = this;
_lock = new Lock();
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_lock = _root._lock;
}
public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_root._singletonDependency43 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonDependency43 is null)
{
_root._singletonDependency43 = new Dependency();
}
}
}
return new Service(_root._singletonDependency43!, _root._singletonDependency43!);
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
Dependency --|> IDependency
Composition ..> Service : IService Root
Service o-- "2 Singleton instances" Dependency : IDependency
namespace Pure.DI.UsageTests.Lifetimes.SingletonScenario {
class Composition {
<<partial>>
+IService Root
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(IDependency dependency1, IDependency dependency2)
}
}