Browse Source

Cleaned up DepMap type checks

tags/1.0-rc
RogueException 8 years ago
parent
commit
11ba30c6fa
1 changed files with 13 additions and 15 deletions
  1. +13
    -15
      src/Discord.Net.Commands/Dependencies/DependencyMap.cs

+ 13
- 15
src/Discord.Net.Commands/Dependencies/DependencyMap.cs View File

@@ -1,10 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Discord.Commands
{
public class DependencyMap : IDependencyMap
{
private static readonly Type[] _typeBlacklist = new[] {
typeof(IDependencyMap),
typeof(CommandService)
};

private Dictionary<Type, Func<object>> map;

public static DependencyMap Empty => new DependencyMap();
@@ -37,26 +43,18 @@ namespace Discord.Commands
/// <inheritdoc />
public void AddFactory<T>(Func<T> factory) where T : class
{
var t = typeof(T);
if (typeof(T) == typeof(IDependencyMap))
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);
if (!TryAddFactory(factory))
throw new InvalidOperationException($"The dependency map already contains \"{typeof(T).FullName}\"");
}
/// <inheritdoc />
public bool TryAddFactory<T>(Func<T> factory) where T : class
{
var t = typeof(T);
if (map.ContainsKey(t))
var type = typeof(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;
if (typeof(T) == typeof(IDependencyMap))
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);
map.Add(type, factory);
return true;
}



Loading…
Cancel
Save