-
Notifications
You must be signed in to change notification settings - Fork 6
/
Registry.cs
287 lines (250 loc) · 11.2 KB
/
Registry.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Collections;
using Galador.Reflection.Utils;
namespace Galador.Reflection
{
#region ExportAttribute, ImportAttribute
/// <summary>
/// Out of the box helper attribute for automatic registration with <see cref="Registry.RegisterAssemblies(Assembly[])"/>
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExportAttribute : Attribute
{
}
/// <summary>
/// When one resolve a type (i.e. create an instance with <see cref="Registry.Resolve(Type)"/> or <see cref="Registry.Create(Type)"/>),
/// all their property marked with this attribute will also be set using <see cref="Registry.Resolve(Type)"/>
/// Can also be used on constructor parameter to specify a particular type to use.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false)]
public class ImportAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="ImportAttribute"/> class.
/// </summary>
/// <param name="eType">(Optional) type that is imported. Will set <see cref="ImportedType"/></param>
public ImportAttribute(Type eType = null)
{
ImportedType = eType;
}
/// <summary>
/// The required type to be imported. If null the type of the member or constructor parameter will be used.
/// </summary>
public Type ImportedType { get; set; }
}
#endregion
/// <summary>
/// This class is a IServiceProvider, IOC, and MEF clone all together.
/// Services are registered explicitly with <see cref="Register(Type, object)"/> or automatically with <see cref="RegisterAssemblies(Assembly[])"/> assembly.
/// Then service can then be acquired / resolved, using constructor and property injection lazily with <see cref="Resolve(Type)"/>.
/// Each registered service will be created only once. Imported object (with <see cref="ImportAttribute"/>) which are not registered as service will
/// be created as well and cached for reuse.
/// </summary>
public sealed class Registry : IDisposable, IServiceProvider
{
TypeTree<ServiceDefinition> services = new TypeTree<ServiceDefinition>();
TypeTree<object> scope = new TypeTree<object>();
#region ctor() Dispose()
/// <summary>
/// Initializes a new instance of the <see cref="Registry"/> class.
/// </summary>
public Registry()
{
this.services = new TypeTree<ServiceDefinition>();
this.scope = new TypeTree<object>();
services[typeof(Registry)] = new ServiceDefinition(this);
}
public Registry(TypeTree<ServiceDefinition> services, TypeTree<object> scope)
{
this.services = services ?? new TypeTree<ServiceDefinition>();
this.scope = scope ?? new TypeTree<object>();
services[typeof(Registry)] = new ServiceDefinition(this);
}
/// <summary>
/// Dispose of all <see cref="IDisposable"/> object registered with the registry and make it unusable.
/// </summary>
public void Dispose()
{
if (disposed)
return;
disposed = true;
foreach (var item in scope.GetKeyValues(typeof(object)).Select(x => x.value).OfType<IDisposable>())
item.Dispose();
scope.Clear();
}
bool disposed;
void EnsureAlive() { if (disposed) throw new ObjectDisposedException(GetType().Name); }
/// <summary>
/// Whether this registry has been disposed or not.
/// </summary>
public bool IsDisposed { get { return disposed; } }
#endregion
#region RegisterAssemblies()
/// <summary>
/// Registers all typed tagged with attribute <typeparamref name="T"/> within all the known assemblies.
/// </summary>
public void RegisterAssemblies<T>()
where T: Attribute
{
RegisterAssemblies<T>(KnownAssemblies.Current);
}
/// <summary>
/// Register some assembly, by registering all type they export that are marked with <see cref="ExportAttribute"/>.
/// </summary>
public void RegisterAssemblies<T>(params Assembly[] ass)
where T : Attribute
{
RegisterAssemblies<T>((IEnumerable<Assembly>)ass);
}
/// <summary>
/// Register some assembly, by registering all type they export that are marked with <see cref="ExportAttribute"/>.
/// </summary>
public void RegisterAssemblies<T>(IEnumerable<Assembly> assemblies)
where T : Attribute
{
foreach (var ti in assemblies.Where(x => x != null).SelectMany(x => x.DefinedTypes))
{
var ea = ti.GetCustomAttributes<T>(false);
foreach (var export in ea)
{
var t = ti.AsType();
if (!TypeTreeActivation.CanBeInstantiated(t))
{
Log.Warning(this, $"[Registry]: Type {t.Name} can't be exported, it is not Resolvable.");
continue;
}
Register(t);
break; // register only once
}
}
}
#endregion
#region Register() IsRegistered()
/// <summary>
/// Register <typeparamref name="T"/> as an exported type.
/// <br/><see cref="Register(Type, object)"/>.
/// </summary>
/// <typeparam name="T">The type that is registered.</typeparam>
public void Register<T>() where T : class { Register(typeof(T), null); }
/// <summary>
/// Register <typeparamref name="T"/> as an exported type.
/// <br/><see cref="Register(Type, object)"/>.
/// </summary>
/// <typeparam name="T">The type that is registered.</typeparam>
/// <param name="instance">The instance that is set against this type.</param>
public void Register<T>(T instance) where T : class { Register(typeof(T), instance); }
/// <summary>
/// Register all arguments types with the registry
/// </summary>
/// <param name="types">All type to register</param>
public void Register(params Type[] types)
{
if (types == null)
return;
foreach (var t in types)
{
if (t == null)
continue;
Register(t, (object)null);
}
}
/// <summary>
/// Register an instance as an instance of a given type. It will returned when resolving any
/// base class or implemented interface.
/// </summary>
/// <param name="facade">The type that is registered.</param>
/// <param name="instance">The instance for this type. If null it will be created on demand
/// on the first access to <paramref name="facade"/></param>
public void Register(Type facade, object instance)
{
EnsureAlive();
TypeTreeActivation.Register(services, facade, instance);
}
/// <summary>
/// Return whether there is an object that is registered for the requested type.
/// </summary>
/// <typeparam name="T">The type to check for registration.</typeparam>
/// <returns>Whether the given type is registered</returns>
public bool IsRegistered<T>() { return IsRegistered(typeof(T)); }
/// <summary>
/// Return whether there is an object that is registered for the requested type.
/// </summary>
/// <param name="type">The type to check for registration.</param>
/// <returns>Whether the given type is registered</returns>
public bool IsRegistered(Type type)
{
EnsureAlive();
return services.ContainsKey(type);
}
#endregion
object IServiceProvider.GetService(Type serviceType) { return Resolve(serviceType); }
/// <summary>
/// Create another registry with the same service definition, but none of the automatically created instances.
/// </summary>
public Registry CreateScope() => new Registry(services, null);
public void ResolveProperties(object instance)
{
EnsureAlive();
TypeTreeActivation.ResolveProperties(services, scope, instance);
}
public T Resolve<T>() => (T)Resolve(typeof(T));
public object Resolve(Type type)
{
EnsureAlive();
return TypeTreeActivation.ResolveSingle(services, scope, type);
}
public IEnumerable<T> ResolveAll<T>() => ResolveAll(typeof(T)).OfType<T>();
public IEnumerable<object> ResolveAll(Type type)
{
EnsureAlive();
return TypeTreeActivation.Resolve(services, scope, type);
}
public IEnumerable<object> ResolveAll(Predicate<Type> matching)
{
EnsureAlive();
return TypeTreeActivation.Resolve(services, scope, matching);
}
/// <summary>
/// Whether given type can be created. Basically it must be a concrete class with some constructors
/// with arguments that can also be created (or resolved).
/// </summary>
/// <typeparam name="T">The type to check for creation.</typeparam>
/// <returns>Whether the type can be instantiated</returns>
public bool CanCreate<T>() { return CanCreate(typeof(T)); }
/// <summary>
/// Whether given type can be created. Basically it must be a concrete class with some constructors
/// with arguments that can also be created (or resolved).
/// </summary>
/// <param name="type">The type to check for creation.</param>
/// <returns>Whether the type can be instantiated</returns>
public bool CanCreate(Type type)
{
EnsureAlive();
return TypeTreeActivation.CanCreate(services, scope, type);
}
/// <summary>
/// Create that object from scratch regardless of registration
/// </summary>
/// <typeparam name="T">The type to instantiate.</typeparam>
/// <returns>A newly created instance</returns>
/// <exception cref="InvalidOperationException">If no appropriate constructor can be found.</exception>
public T Create<T>() { return (T)Create(typeof(T)); }
public T Create<T>(params object[] parameters) { return (T)Create(typeof(T), null, parameters); }
/// <summary>
/// Create that object from scratch regardless of registration
/// </summary>
/// <param name="type">The type to instantiate.</param>
/// <returns>A newly created instance</returns>
/// <exception cref="InvalidOperationException">If no appropriate constructor can be found.</exception>
public object Create(Type type, params object[] parameters)
{
EnsureAlive();
return TypeTreeActivation.Create(services, scope, type, parameters);
}
}
}