Cleaned up new DependencyMap system
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
@@ -13,18 +12,6 @@ namespace Discord.Commands
|
|||||||
map = new Dictionary<Type, object>();
|
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)
|
public void Add<T>(T obj)
|
||||||
{
|
{
|
||||||
var t = typeof(T);
|
var t = typeof(T);
|
||||||
@@ -32,5 +19,37 @@ namespace Discord.Commands
|
|||||||
throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\"");
|
throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\"");
|
||||||
map.Add(t, obj);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
public interface IDependencyMap
|
public interface IDependencyMap
|
||||||
{
|
{
|
||||||
object Get(Type t);
|
|
||||||
T Get<T>() where T : class;
|
|
||||||
void Add<T>(T obj);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
@@ -9,62 +8,39 @@ namespace Discord.Commands
|
|||||||
{
|
{
|
||||||
internal static object CreateObject(TypeInfo typeInfo, CommandService service, IDependencyMap map = null)
|
internal static object CreateObject(TypeInfo typeInfo, CommandService service, IDependencyMap map = null)
|
||||||
{
|
{
|
||||||
if (typeInfo.DeclaredConstructors.Count() > 1)
|
var constructors = typeInfo.DeclaredConstructors.ToArray();
|
||||||
throw new InvalidOperationException($"Found too many constructors for \"{typeInfo.FullName}\"");
|
if (constructors.Length == 0)
|
||||||
|
throw new InvalidOperationException($"No constructor found for \"{typeInfo.FullName}\"");
|
||||||
var constructor = typeInfo.DeclaredConstructors.FirstOrDefault();
|
else if (constructors.Length > 1)
|
||||||
|
throw new InvalidOperationException($"Multiple constructors found for \"{typeInfo.FullName}\"");
|
||||||
if (constructor == null)
|
|
||||||
throw new InvalidOperationException($"Found no constructor for \"{typeInfo.FullName}\"");
|
|
||||||
|
|
||||||
object[] arguments = null;
|
|
||||||
|
|
||||||
|
var constructor = constructors[0];
|
||||||
|
|
||||||
ParameterInfo[] parameters = constructor.GetParameters();
|
ParameterInfo[] parameters = constructor.GetParameters();
|
||||||
|
object[] args = new object[parameters.Length];
|
||||||
// TODO: can this logic be made better/cleaner?
|
for (int i = 0; i < parameters.Length; i++)
|
||||||
if (parameters.Length == 1)
|
|
||||||
{
|
{
|
||||||
if (parameters[0].ParameterType == typeof(IDependencyMap))
|
var parameter = parameters[i];
|
||||||
|
object arg;
|
||||||
|
if (map == null || !map.TryGet(parameter.ParameterType, out arg))
|
||||||
{
|
{
|
||||||
if (map != null)
|
if (parameter.ParameterType == typeof(CommandService))
|
||||||
arguments = new object[] { map };
|
arg = service;
|
||||||
|
else if (parameter.ParameterType == typeof(IDependencyMap))
|
||||||
|
arg = map;
|
||||||
else
|
else
|
||||||
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)");
|
throw new InvalidOperationException($"Failed to create \"{typeInfo.FullName}\", dependency \"{parameter.ParameterType.Name}\" was not found.");
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (parameters.Length == 2)
|
|
||||||
{
|
|
||||||
if (parameters[0].ParameterType == typeof(CommandService) && parameters[1].ParameterType == typeof(IDependencyMap))
|
|
||||||
if (map != null)
|
|
||||||
arguments = new object[] { service, map };
|
|
||||||
else
|
|
||||||
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arguments == null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// TODO: probably change this ternary into something sensible?
|
|
||||||
arguments = parameters.Select(x => x.ParameterType == typeof(CommandService) ? service : map.Get(x.ParameterType)).ToArray();
|
|
||||||
}
|
|
||||||
catch (KeyNotFoundException ex) // tried to inject an invalid dependency
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (could not provide parameter)", ex);
|
|
||||||
}
|
|
||||||
catch (NullReferenceException ex) // tried to find a dependency
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)", ex);
|
|
||||||
}
|
}
|
||||||
|
args[i] = arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return constructor.Invoke(arguments);
|
return constructor.Invoke(args);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Could not create \"{typeInfo.FullName}\"", ex);
|
throw new Exception($"Failed to create \"{typeInfo.FullName}\"", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user