-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathNanoContainer.cs
More file actions
175 lines (157 loc) · 5.85 KB
/
Copy pathNanoContainer.cs
File metadata and controls
175 lines (157 loc) · 5.85 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using NEventStore.Logging;
using Microsoft.Extensions.Logging;
namespace NEventStore
{
/// <summary>
/// Represents a simple IoC container.
/// </summary>
public class NanoContainer
{
private static readonly ILogger Logger = LogFactory.BuildLogger(typeof(NanoContainer));
private readonly Dictionary<Type, ContainerRegistration> _registrations = [];
/// <summary>
/// Registers a service with the container.
/// </summary>
public virtual ContainerRegistration Register<TService>(Func<NanoContainer, TService?> resolve)
where TService : class
{
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug(Messages.RegisteringWireupCallback, typeof(TService));
}
var registration = new ContainerRegistration(c => resolve(c));
_registrations[typeof(TService)] = registration;
return registration;
}
/// <summary>
/// Registers a service instance with the container.
/// </summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public virtual ContainerRegistration Register<TService>(TService instance)
{
if (Equals(instance, null))
{
throw new ArgumentNullException(nameof(instance), Messages.InstanceCannotBeNull);
}
if (!typeof(TService).IsValueType && !typeof(TService).IsInterface)
{
throw new ArgumentException(Messages.TypeMustBeInterface, nameof(instance));
}
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug(Messages.RegisteringServiceInstance, typeof(TService));
}
var registration = new ContainerRegistration(instance);
_registrations[typeof(TService)] = registration;
return registration;
}
/// <summary>
/// Resolves a service from the container.
/// </summary>
public virtual TService? Resolve<TService>()
{
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug(Messages.ResolvingService, typeof(TService));
}
if (_registrations.TryGetValue(typeof(TService), out ContainerRegistration? registration))
{
var obj = registration.Resolve(this);
return obj != null ? (TService)obj : default;
}
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug(Messages.UnableToResolve, typeof(TService));
}
return default;
}
}
/// <summary>
/// Represents a registration in the container.
/// </summary>
public class ContainerRegistration
{
private static readonly ILogger Logger = LogFactory.BuildLogger(typeof(ContainerRegistration));
private readonly Func<NanoContainer, object?>? _resolve;
private object? _instance;
private bool _instancePerCall;
/// <summary>
/// Initializes a new instance of the ContainerRegistration class.
/// </summary>
public ContainerRegistration(Func<NanoContainer, object?> resolve)
{
if (resolve is null)
{
throw new ArgumentNullException(nameof(resolve));
}
if (Logger.IsEnabled(LogLevel.Trace))
{
Logger.LogTrace(Messages.AddingWireupCallback);
}
_resolve = resolve;
}
/// <summary>
/// Initializes a new instance of the ContainerRegistration class.
/// </summary>
public ContainerRegistration(object instance)
{
if (instance is null)
{
throw new ArgumentNullException(nameof(instance));
}
if (Logger.IsEnabled(LogLevel.Trace))
{
Logger.LogTrace(Messages.AddingWireupRegistration, instance.GetType());
}
_instance = instance;
}
/// <summary>
/// Configures the registration to be resolved once per call.
/// </summary>
public virtual ContainerRegistration InstancePerCall()
{
if (_resolve == null)
{
throw new InvalidOperationException("Cannot configure InstancePerCall() on instance registrations.");
}
if (Logger.IsEnabled(LogLevel.Trace))
{
Logger.LogTrace(Messages.ConfiguringInstancePerCall);
}
_instancePerCall = true;
return this;
}
/// <summary>
/// Resolves the registration.
/// </summary>
public virtual object? Resolve(NanoContainer container)
{
if (Logger.IsEnabled(LogLevel.Trace))
{
Logger.LogTrace(Messages.ResolvingInstance);
}
if (_instancePerCall)
{
if (Logger.IsEnabled(LogLevel.Trace))
{
Logger.LogTrace(Messages.BuildingNewInstance);
}
return _resolve!(container);
}
if (Logger.IsEnabled(LogLevel.Trace))
{
Logger.LogTrace(Messages.AttemptingToResolveInstance);
}
if (_instance != null)
{
return _instance;
}
if (Logger.IsEnabled(LogLevel.Trace))
{
Logger.LogTrace(Messages.BuildingAndStoringNewInstance);
}
return _instance = _resolve!(container);
}
}
}