Add TryAdd to DependencyMaps
This commit is contained in:
@@ -18,12 +18,21 @@ namespace Discord.Commands
|
|||||||
public void Add<T>(T obj) where T : class
|
public void Add<T>(T obj) where T : class
|
||||||
=> AddFactory(() => obj);
|
=> AddFactory(() => obj);
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
public bool TryAdd<T>(T obj) where T : class
|
||||||
|
=> TryAddFactory(() => obj);
|
||||||
|
/// <inheritdoc />
|
||||||
public void AddTransient<T>() where T : class, new()
|
public void AddTransient<T>() where T : class, new()
|
||||||
=> AddFactory(() => new T());
|
=> AddFactory(() => new T());
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
public bool TryAddTransient<T>() where T : class, new()
|
||||||
|
=> TryAddFactory(() => new T());
|
||||||
|
/// <inheritdoc />
|
||||||
public void AddTransient<TKey, TImpl>() where TKey : class
|
public void AddTransient<TKey, TImpl>() where TKey : class
|
||||||
where TImpl : class, TKey, new()
|
where TImpl : class, TKey, new()
|
||||||
=> AddFactory<TKey>(() => new TImpl());
|
=> AddFactory<TKey>(() => new TImpl());
|
||||||
|
public bool TryAddTransient<TKey, TImpl>() where TKey : class
|
||||||
|
where TImpl : class, TKey, new()
|
||||||
|
=> TryAddFactory<TKey>(() => new TImpl());
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void AddFactory<T>(Func<T> factory) where T : class
|
public void AddFactory<T>(Func<T> factory) where T : class
|
||||||
@@ -33,6 +42,15 @@ 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, factory);
|
map.Add(t, factory);
|
||||||
}
|
}
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool TryAddFactory<T>(Func<T> factory) where T : class
|
||||||
|
{
|
||||||
|
var t = typeof(T);
|
||||||
|
if (map.ContainsKey(t))
|
||||||
|
return false;
|
||||||
|
map.Add(t, factory);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public T Get<T>()
|
public T Get<T>()
|
||||||
|
|||||||
@@ -11,11 +11,24 @@ namespace Discord.Commands
|
|||||||
/// <param name="obj">The instance of a service.</param>
|
/// <param name="obj">The instance of a service.</param>
|
||||||
void Add<T>(T obj) where T : class;
|
void Add<T>(T obj) where T : class;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// Tries to add an instance of a service to be injected.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of service.</typeparam>
|
||||||
|
/// <param name="obj">The instance of a service.</param>
|
||||||
|
/// <returns>A bool, indicating if the service was successfully added to the DependencyMap.</returns>
|
||||||
|
bool TryAdd<T>(T obj) where T : class;
|
||||||
|
/// <summary>
|
||||||
/// Add a service that will be injected by a new instance every time.
|
/// Add a service that will be injected by a new instance every time.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of instance to inject.</typeparam>
|
/// <typeparam name="T">The type of instance to inject.</typeparam>
|
||||||
void AddTransient<T>() where T : class, new();
|
void AddTransient<T>() where T : class, new();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// Tries to add a service that will be injected by a new instance every time.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of instance to inject.</typeparam>
|
||||||
|
/// <returns>A bool, indicating if the service was successfully added to the DependencyMap.</returns>
|
||||||
|
bool TryAddTransient<T>() where T : class, new();
|
||||||
|
/// <summary>
|
||||||
/// Add a service that will be injected by a new instance every time.
|
/// Add a service that will be injected by a new instance every time.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TKey">The type to look for when injecting.</typeparam>
|
/// <typeparam name="TKey">The type to look for when injecting.</typeparam>
|
||||||
@@ -25,11 +38,25 @@ namespace Discord.Commands
|
|||||||
/// </example>
|
/// </example>
|
||||||
void AddTransient<TKey, TImpl>() where TKey: class where TImpl : class, TKey, new();
|
void AddTransient<TKey, TImpl>() where TKey: class where TImpl : class, TKey, new();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// Tries to add a service that will be injected by a new instance every time.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey">The type to look for when injecting.</typeparam>
|
||||||
|
/// <typeparam name="TImpl">The type to inject when injecting.</typeparam>
|
||||||
|
/// <returns>A bool, indicating if the service was successfully added to the DependencyMap.</returns>
|
||||||
|
bool TryAddTransient<TKey, TImpl>() where TKey : class where TImpl : class, TKey, new();
|
||||||
|
/// <summary>
|
||||||
/// Add a service that will be injected by a factory.
|
/// Add a service that will be injected by a factory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type to look for when injecting.</typeparam>
|
/// <typeparam name="T">The type to look for when injecting.</typeparam>
|
||||||
/// <param name="factory">The factory that returns a type of this service.</param>
|
/// <param name="factory">The factory that returns a type of this service.</param>
|
||||||
void AddFactory<T>(Func<T> factory) where T : class;
|
void AddFactory<T>(Func<T> factory) where T : class;
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to add a service that will be injected by a factory.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type to look for when injecting.</typeparam>
|
||||||
|
/// <param name="factory">The factory that returns a type of this service.</param>
|
||||||
|
/// <returns>A bool, indicating if the service was successfully added to the DependencyMap.</returns>
|
||||||
|
bool TryAddFactory<T>(Func<T> factory) where T : class;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pull an object from the map.
|
/// Pull an object from the map.
|
||||||
|
|||||||
Reference in New Issue
Block a user