Cleaned up DepMap type checks
This commit is contained in:
@@ -1,10 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
public class DependencyMap : IDependencyMap
|
public class DependencyMap : IDependencyMap
|
||||||
{
|
{
|
||||||
|
private static readonly Type[] _typeBlacklist = new[] {
|
||||||
|
typeof(IDependencyMap),
|
||||||
|
typeof(CommandService)
|
||||||
|
};
|
||||||
|
|
||||||
private Dictionary<Type, Func<object>> map;
|
private Dictionary<Type, Func<object>> map;
|
||||||
|
|
||||||
public static DependencyMap Empty => new DependencyMap();
|
public static DependencyMap Empty => new DependencyMap();
|
||||||
@@ -37,26 +43,18 @@ namespace Discord.Commands
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void AddFactory<T>(Func<T> factory) where T : class
|
public void AddFactory<T>(Func<T> factory) where T : class
|
||||||
{
|
{
|
||||||
var t = typeof(T);
|
if (!TryAddFactory(factory))
|
||||||
if (typeof(T) == typeof(IDependencyMap))
|
throw new InvalidOperationException($"The dependency map already contains \"{typeof(T).FullName}\"");
|
||||||
throw new InvalidOperationException("IDependencyMap is used internally and cannot be added as a dependency");
|
|
||||||
if (typeof(T) == typeof(CommandService))
|
|
||||||
throw new InvalidOperationException("CommandService is used internally and cannot be added as a dependency");
|
|
||||||
if (map.ContainsKey(t))
|
|
||||||
throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\"");
|
|
||||||
map.Add(t, factory);
|
|
||||||
}
|
}
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool TryAddFactory<T>(Func<T> factory) where T : class
|
public bool TryAddFactory<T>(Func<T> factory) where T : class
|
||||||
{
|
{
|
||||||
var t = typeof(T);
|
var type = typeof(T);
|
||||||
if (map.ContainsKey(t))
|
if (_typeBlacklist.Contains(type))
|
||||||
|
throw new InvalidOperationException($"{type.FullName} is used internally and cannot be added as a dependency");
|
||||||
|
if (map.ContainsKey(type))
|
||||||
return false;
|
return false;
|
||||||
if (typeof(T) == typeof(IDependencyMap))
|
map.Add(type, factory);
|
||||||
throw new InvalidOperationException("IDependencyMap is used internally and cannot be added as a dependency");
|
|
||||||
if (typeof(T) == typeof(CommandService))
|
|
||||||
throw new InvalidOperationException("CommandService is used internally and cannot be added as a dependency");
|
|
||||||
map.Add(t, factory);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user