Browse Source

Add Transients/Factories to Dependency Injection

pull/417/head
Christopher F 8 years ago
parent
commit
0f334d24a0
2 changed files with 24 additions and 6 deletions
  1. +20
    -5
      src/Discord.Net.Commands/Dependencies/DependencyMap.cs
  2. +4
    -1
      src/Discord.Net.Commands/Dependencies/IDependencyMap.cs

+ 20
- 5
src/Discord.Net.Commands/Dependencies/DependencyMap.cs View File

@@ -5,21 +5,29 @@ namespace Discord.Commands
{
public class DependencyMap : IDependencyMap
{
private Dictionary<Type, object> map;
private Dictionary<Type, Func<object>> map;

public static DependencyMap Empty => new DependencyMap();

public DependencyMap()
{
map = new Dictionary<Type, object>();
map = new Dictionary<Type, Func<object>>();
}

public void Add<T>(T obj)
public void Add<T>(T obj) where T : class
=> AddFactory(() => obj);
public void AddTransient<T>() where T : class, new()
=> AddFactory(() => new T());
public void AddTransient<TKey, TImpl>() where TKey : class
where TImpl : class, TKey, new()
=> AddFactory<TKey>(() => new TImpl());

public void AddFactory<T>(Func<T> factory) where T : class
{
var t = typeof(T);
if (map.ContainsKey(t))
throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\"");
map.Add(t, obj);
map.Add(t, factory);
}

public T Get<T>()
@@ -51,7 +59,14 @@ namespace Discord.Commands
}
public bool TryGet(Type t, out object result)
{
return map.TryGetValue(t, out result);
Func<object> func;
if (map.TryGetValue(t, out func))
{
result = func();
return true;
}
result = null;
return false;
}
}
}

+ 4
- 1
src/Discord.Net.Commands/Dependencies/IDependencyMap.cs View File

@@ -4,7 +4,10 @@ namespace Discord.Commands
{
public interface IDependencyMap
{
void Add<T>(T obj);
void Add<T>(T obj) where T : class;
void AddTransient<T>() where T : class, new();
void AddTransient<TKey, TImpl>() where TKey: class where TImpl : class, TKey, new();
void AddFactory<T>(Func<T> factory) where T : class;

T Get<T>();
bool TryGet<T>(out T result);


Loading…
Cancel
Save