-
Notifications
You must be signed in to change notification settings - Fork 22
/
SingletonScenario.cs
73 lines (62 loc) · 2.42 KB
/
SingletonScenario.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
$v=true
$p=1
$d=Singleton
$h=The _Singleton_ lifetime ensures that there will be a single instance of the dependency for each composition.
$f=Some articles advise using objects with a _Singleton_ lifetime as often as possible, but the following details must be considered:
$f=
$f=- 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.
$f=
$f=- 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.
$f=
$f=- The thread-safety control should be automatically extended to all dependencies that _Singleton_ uses, since their state is also now shared.
$f=
$f=- Logic for thread-safety control can be resource-costly, error-prone, interlocking, and difficult to test.
$f=
$f=- _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.
$f=
$f=- Sometimes additional logic is required to dispose of _Singleton_.
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable ArrangeTypeModifiers
namespace Pure.DI.UsageTests.Lifetimes.SingletonScenario;
using Xunit;
// {
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;
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// {
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);
// }
composition.SaveClassDiagram();
}
}