-
Notifications
You must be signed in to change notification settings - Fork 22
/
Program.cs
58 lines (49 loc) · 1.63 KB
/
Program.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
using Pure.DI;
using System.Diagnostics;
using static Pure.DI.Lifetime;
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable UnusedMember.Global
// Composition root
new Composition().Root.Run();
return;
// In fact, this code is never run, and the method can have any name or be a constructor, for example,
// and can be in any part of the compiled code because this is just a hint to set up an object graph.
// [Conditional("DI")] attribute avoids generating IL code for the method that follows it.
// Since this method is needed only at the compile time.
[Conditional("DI")]
static void Setup() =>
DI.Setup(nameof(Composition))
// Models a random subatomic event that may or may not occur
.Bind().As(Singleton).To<Random>()
// Quantum superposition of two states: Alive or Dead
.Bind().To((Random random) => (State)random.Next(2))
.Bind().To<ShroedingersCat>()
// Cardboard box with any contents
.Bind().To<CardboardBox<TT>>()
// Provides the composition root
.Root<Program>("Root");
public interface IBox<out T>
{
T Content { get; }
}
public interface ICat
{
State State { get; }
}
public enum State
{
Alive,
Dead
}
public record CardboardBox<T>(T Content) : IBox<T>;
public class ShroedingersCat(Lazy<State> superposition): ICat
{
// The decoherence of the superposition
// at the time of observation via an irreversible process
public State State => superposition.Value;
public override string ToString() => $"{State} cat";
}
public partial class Program(IBox<ICat> box)
{
private void Run() => Console.WriteLine(box);
}