Cleaned up new DependencyMap system

This commit is contained in:
RogueException
2016-07-20 23:34:32 -03:00
parent 751c28a87e
commit b029725bb1
3 changed files with 58 additions and 62 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Discord.Commands
{
@@ -13,18 +12,6 @@ namespace Discord.Commands
map = new Dictionary<Type, object>();
}
public object Get(Type t)
{
if (!map.ContainsKey(t))
throw new KeyNotFoundException($"The dependency map does not contain \"{t.FullName}\"");
return map[t];
}
public T Get<T>() where T : class
{
return Get(typeof(T)) as T;
}
public void Add<T>(T obj)
{
var t = typeof(T);
@@ -32,5 +19,37 @@ namespace Discord.Commands
throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\"");
map.Add(t, obj);
}
public T Get<T>()
{
return (T)Get(typeof(T));
}
public object Get(Type t)
{
object result;
if (!TryGet(t, out result))
throw new KeyNotFoundException($"The dependency map does not contain \"{t.FullName}\"");
else
return result;
}
public bool TryGet<T>(out T result)
{
object untypedResult;
if (TryGet(typeof(T), out untypedResult))
{
result = (T)untypedResult;
return true;
}
else
{
result = default(T);
return false;
}
}
public bool TryGet(Type t, out object result)
{
return map.TryGetValue(t, out result);
}
}
}

View File

@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Discord.Commands
{
public interface IDependencyMap
{
object Get(Type t);
T Get<T>() where T : class;
void Add<T>(T obj);
T Get<T>();
bool TryGet<T>(out T result);
object Get(Type t);
bool TryGet(Type t, out object result);
}
}