Skip to content

Commit e39a997

Browse files
committed
Reformat
1 parent 9ee082e commit e39a997

File tree

2 files changed

+80
-37
lines changed

2 files changed

+80
-37
lines changed

Assets/BossRoom/Scripts/Shared/Infrastructure/DIScope.cs

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,39 @@ T Resolve<T>()
3535

3636
public sealed class DIScope : IInstanceResolver, IDisposable
3737
{
38+
private struct LazyBindDescriptor
39+
{
40+
public readonly Type Type;
41+
public readonly Type[] InterfaceTypes;
42+
43+
public LazyBindDescriptor(Type type, Type[] interfaceTypes)
44+
{
45+
Type = type;
46+
InterfaceTypes = interfaceTypes;
47+
}
48+
}
49+
3850
private static DIScope m_rootScope;
3951

52+
public static DIScope RootScope
53+
{
54+
get
55+
{
56+
if (m_rootScope == null)
57+
{
58+
m_rootScope = new DIScope();
59+
}
60+
61+
return m_rootScope;
62+
}
63+
}
64+
4065
private readonly DisposableGroup m_DisposableGroup = new DisposableGroup();
4166
private readonly Dictionary<Type, LazyBindDescriptor> m_LazyBindDescriptors = new Dictionary<Type, LazyBindDescriptor>();
4267

4368
private readonly DIScope m_Parent;
4469
private readonly Dictionary<Type, object> m_TypesToInstances = new Dictionary<Type, object>();
70+
private readonly HashSet<object> m_ObjectsWithInjectedDependencies = new HashSet<object>();
4571
private bool m_Disposed;
4672

4773
private bool m_ScopeConstructionComplete;
@@ -52,32 +78,30 @@ public DIScope(DIScope parent = null)
5278
BindInstanceAsSingle<IInstanceResolver, DIScope>(this);
5379
}
5480

55-
public static DIScope RootScope
81+
~DIScope()
5682
{
57-
get
58-
{
59-
if (m_rootScope == null) m_rootScope = new DIScope();
60-
61-
return m_rootScope;
62-
}
83+
Dispose();
6384
}
6485

6586
public void Dispose()
6687
{
6788
if (!m_Disposed)
6889
{
6990
m_TypesToInstances.Clear();
91+
m_ObjectsWithInjectedDependencies.Clear();
7092
m_DisposableGroup.Dispose();
7193
m_Disposed = true;
7294
}
7395
}
7496

75-
public T Resolve<T>()
76-
where T : class
97+
public T Resolve<T>() where T : class
7798
{
7899
if (!m_ScopeConstructionComplete)
100+
{
79101
throw new ScopeNotFinalizedException(
80102
$"Trying to Resolve type {typeof(T)}, but the DISCope is not yet finalized! You should call FinalizeScopeConstruction before any of the Resolve calls.");
103+
}
104+
81105
//if we have this type as lazy-bound instance - we are going to instantiate it now
82106
if (m_LazyBindDescriptors.TryGetValue(typeof(T), out var lazyBindDescriptor))
83107
{
@@ -88,7 +112,10 @@ public T Resolve<T>()
88112

89113
if (!m_TypesToInstances.TryGetValue(typeof(T), out var value))
90114
{
91-
if (m_Parent != null) return m_Parent.Resolve<T>();
115+
if (m_Parent != null)
116+
{
117+
return m_Parent.Resolve<T>();
118+
}
92119

93120
throw new NoInstanceToInjectException($"Injection of type {typeof(T)} failed.");
94121
}
@@ -98,6 +125,11 @@ public T Resolve<T>()
98125

99126
public void InjectIn(object obj)
100127
{
128+
if (m_ObjectsWithInjectedDependencies.Contains(obj))
129+
{
130+
return;
131+
}
132+
101133
if (CachedReflectionUtility.TryGetInjectableMethod(obj.GetType(), out var injectionMethod))
102134
{
103135
var parameters = CachedReflectionUtility.GetMethodParameters(injectionMethod);
@@ -114,19 +146,18 @@ public void InjectIn(object obj)
114146
}
115147

116148
injectionMethod.Invoke(obj, paramColleciton);
149+
m_ObjectsWithInjectedDependencies.Add(obj);
117150
}
118151
}
119152

120153
public void InjectIn(GameObject go)
121154
{
122-
var components = go.GetComponentsInChildren<Component>();
123-
124-
foreach (var component in components) InjectIn(component);
125-
}
155+
var components = go.GetComponentsInChildren<Component>(includeInactive:true);
126156

127-
~DIScope()
128-
{
129-
Dispose();
157+
foreach (var component in components)
158+
{
159+
InjectIn(component);
160+
}
130161
}
131162

132163
public void BindInstanceAsSingle<T>(T instance) where T : class
@@ -172,7 +203,10 @@ private void LazyBind(Type type, params Type[] typeAliases)
172203
{
173204
var descriptor = new LazyBindDescriptor(type, typeAliases);
174205

175-
foreach (var typeAlias in typeAliases) m_LazyBindDescriptors[typeAlias] = descriptor;
206+
foreach (var typeAlias in typeAliases)
207+
{
208+
m_LazyBindDescriptors[typeAlias] = descriptor;
209+
}
176210

177211
m_LazyBindDescriptors[type] = descriptor;
178212
}
@@ -196,24 +230,34 @@ private object InstantiateLazyBoundObject(LazyBindDescriptor descriptor)
196230
BindInstanceToType(instance, descriptor.Type);
197231

198232
if (descriptor.InterfaceTypes != null)
233+
{
199234
foreach (var interfaceType in descriptor.InterfaceTypes)
235+
{
200236
BindInstanceToType(instance, interfaceType);
237+
}
238+
}
201239

202240
return instance;
203241
}
204242

205243
private void AddToDisposableGroupIfDisposable(object instance)
206244
{
207-
if (instance is IDisposable disposable) m_DisposableGroup.Add(disposable);
245+
if (instance is IDisposable disposable)
246+
{
247+
m_DisposableGroup.Add(disposable);
248+
}
208249
}
209250

210251
/// <summary>
211-
/// This method forces the finalization of construction of DI Scope. It would inject all the instances passed to it directly.
212-
/// Objects that were bound by just type will be instantiated on their first use.
252+
/// This method forces the finalization of construction of DI Scope. It would inject all the instances passed to it directly.
253+
/// Objects that were bound by just type will be instantiated on their first use.
213254
/// </summary>
214255
public void FinalizeScopeConstruction()
215256
{
216-
if (m_ScopeConstructionComplete) return;
257+
if (m_ScopeConstructionComplete)
258+
{
259+
return;
260+
}
217261

218262
m_ScopeConstructionComplete = true;
219263

@@ -261,7 +305,10 @@ public static bool TryGetInjectableConstructor(Type type, out ConstructorInfo me
261305

262306
private static void CacheTypeMethods(Type type)
263307
{
264-
if (k_ProcessedTypes.Contains(type)) return;
308+
if (k_ProcessedTypes.Contains(type))
309+
{
310+
return;
311+
}
265312

266313
var constructors = type.GetConstructors();
267314
foreach (var constructorInfo in constructors)
@@ -309,25 +356,17 @@ public static MethodInfo GetTypedResolveMethod(Type parameterType)
309356
{
310357
if (!k_CachedResolveMethods.TryGetValue(parameterType, out var resolveMethod))
311358
{
312-
if (k_ResolveMethod == null) k_ResolveMethod = typeof(DIScope).GetMethod("Resolve");
359+
if (k_ResolveMethod == null)
360+
{
361+
k_ResolveMethod = typeof(DIScope).GetMethod("Resolve");
362+
}
363+
313364
resolveMethod = k_ResolveMethod.MakeGenericMethod(parameterType);
314365
k_CachedResolveMethods[parameterType] = resolveMethod;
315366
}
316367

317368
return resolveMethod;
318369
}
319370
}
320-
321-
private struct LazyBindDescriptor
322-
{
323-
public readonly Type Type;
324-
public readonly Type[] InterfaceTypes;
325-
326-
public LazyBindDescriptor(Type type, Type[] interfaceTypes)
327-
{
328-
Type = type;
329-
InterfaceTypes = interfaceTypes;
330-
}
331-
}
332371
}
333372
}

Assets/BossRoom/Scripts/Shared/Infrastructure/DisposableGroup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ public class DisposableGroup : IDisposable
99

1010
public void Dispose()
1111
{
12-
foreach (var disposable in m_Disposables) disposable.Dispose();
12+
foreach (var disposable in m_Disposables)
13+
{
14+
disposable.Dispose();
15+
}
16+
1317
m_Disposables.Clear();
1418
}
1519

0 commit comments

Comments
 (0)