-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose resets and remove scene monitoring
- Loading branch information
1 parent
77c4669
commit 5b133fa
Showing
6 changed files
with
86 additions
and
112 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
FrictionlessUnity/Assets/Frictionless/Scripts/HandlerPersistence.cs
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
FrictionlessUnity/Assets/Frictionless/Scripts/HandlerPersistence.cs.meta
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 57 additions & 50 deletions
107
FrictionlessUnity/Assets/Frictionless/Scripts/ServiceFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// A simple, *single-threaded*, dependency injection container appropriate for use with Unity. | ||
/// </summary> | ||
using UnityEngine; | ||
|
||
namespace Frictionless | ||
public class ServiceFactory | ||
{ | ||
/// <summary> | ||
/// A simple, *single-threaded*, dependency injection container appropriate for use with Unity. | ||
/// </summary> | ||
public static class ServiceFactory | ||
private static Dictionary<Type,Type> singletons = new Dictionary<Type, Type>(); | ||
private static Dictionary<Type,Type> transients = new Dictionary<Type, Type>(); | ||
private static Dictionary<Type,object> singletonInstances = new Dictionary<Type, object>(); | ||
|
||
private ServiceFactory() | ||
{ | ||
private static Dictionary<Type,Type> singletons = new Dictionary<Type, Type>(); | ||
private static Dictionary<Type,Type> transients = new Dictionary<Type, Type>(); | ||
private static Dictionary<Type,object> singletonInstances = new Dictionary<Type, object>(); | ||
} | ||
|
||
public static void RegisterSingleton<TConcrete>() | ||
{ | ||
singletons[typeof(TConcrete)] = typeof(TConcrete); | ||
} | ||
public static void Reset() | ||
{ | ||
singletons.Clear(); | ||
transients.Clear(); | ||
singletonInstances.Clear(); | ||
} | ||
|
||
public static void RegisterSingleton<TAbstract,TConcrete>() | ||
{ | ||
singletons[typeof(TAbstract)] = typeof(TConcrete); | ||
} | ||
|
||
public static void RegisterSingleton<TConcrete>(TConcrete instance) | ||
{ | ||
singletons[typeof(TConcrete)] = typeof(TConcrete); | ||
singletonInstances[typeof(TConcrete)] = instance; | ||
} | ||
public static void RegisterSingleton<TConcrete>() | ||
{ | ||
singletons[typeof(TConcrete)] = typeof(TConcrete); | ||
} | ||
|
||
public static void RegisterTransient<TAbstract,TConcrete>() | ||
{ | ||
transients[typeof(TAbstract)] = typeof(TConcrete); | ||
} | ||
public static void RegisterSingleton<TAbstract,TConcrete>() | ||
{ | ||
singletons[typeof(TAbstract)] = typeof(TConcrete); | ||
} | ||
|
||
public static void RegisterSingleton<TConcrete>(TConcrete instance) | ||
{ | ||
singletons[typeof(TConcrete)] = typeof(TConcrete); | ||
singletonInstances[typeof(TConcrete)] = instance; | ||
} | ||
|
||
public static T Resolve<T>() where T : class | ||
public static void RegisterTransient<TAbstract,TConcrete>() | ||
{ | ||
transients[typeof(TAbstract)] = typeof(TConcrete); | ||
} | ||
|
||
public static T Resolve<T>() where T : class | ||
{ | ||
T result = default(T); | ||
Type concreteType = null; | ||
if (singletons.TryGetValue(typeof(T), out concreteType)) | ||
{ | ||
T result = default(T); | ||
Type concreteType = null; | ||
if (singletons.TryGetValue(typeof(T), out concreteType)) | ||
object r = null; | ||
if (!singletonInstances.TryGetValue(typeof(T), out r)) | ||
{ | ||
object r = null; | ||
if (!singletonInstances.TryGetValue(typeof(T), out r)) | ||
if (concreteType.IsSubclassOf(typeof(MonoBehaviour))) | ||
{ | ||
if (concreteType.IsSubclassOf(typeof(MonoBehaviour))) | ||
{ | ||
GameObject singletonGameObject = new GameObject(); | ||
r = singletonGameObject.AddComponent(concreteType); | ||
singletonGameObject.name = typeof(T).ToString() + " (singleton)"; | ||
} | ||
else | ||
r = concreteType.GetConstructor(Type.EmptyTypes).Invoke(new object[] { }); | ||
singletonInstances[typeof(T)] = r; | ||
GameObject singletonGameObject = new GameObject(); | ||
r = singletonGameObject.AddComponent(concreteType); | ||
singletonGameObject.name = typeof(T).ToString() + " (singleton)"; | ||
} | ||
result = (T)r; | ||
} | ||
else if (transients.TryGetValue(typeof(T), out concreteType)) | ||
{ | ||
object r = concreteType.GetConstructor(Type.EmptyTypes).Invoke(new object[] { }); | ||
result = (T)r; | ||
else | ||
r = concreteType.GetConstructor(Type.EmptyTypes).Invoke(new object[] { }); | ||
singletonInstances[typeof(T)] = r; | ||
} | ||
else | ||
UnityEngine.Debug.LogError("Failed to resolve injected type for [" + typeof(T) + "] - it needs to be registered before being used!"); | ||
return result; | ||
result = (T)r; | ||
} | ||
else if (transients.TryGetValue(typeof(T), out concreteType)) | ||
{ | ||
object r = concreteType.GetConstructor(Type.EmptyTypes).Invoke(new object[] { }); | ||
result = (T)r; | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters