Add docstrings, per volt's feedback
This commit is contained in:
@@ -2,6 +2,12 @@
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that this property should be filled in by dependency injection.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This property **MUST** have a setter.
|
||||
/// </remarks>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public sealed class InjectAttribute : Attribute
|
||||
{
|
||||
|
||||
@@ -14,14 +14,18 @@ namespace Discord.Commands
|
||||
map = new Dictionary<Type, Func<object>>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Add<T>(T obj) where T : class
|
||||
=> AddFactory(() => obj);
|
||||
/// <inheritdoc />
|
||||
public void AddTransient<T>() where T : class, new()
|
||||
=> AddFactory(() => new T());
|
||||
/// <inheritdoc />
|
||||
public void AddTransient<TKey, TImpl>() where TKey : class
|
||||
where TImpl : class, TKey, new()
|
||||
=> AddFactory<TKey>(() => new TImpl());
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public void AddFactory<T>(Func<T> factory) where T : class
|
||||
{
|
||||
var t = typeof(T);
|
||||
@@ -30,10 +34,12 @@ namespace Discord.Commands
|
||||
map.Add(t, factory);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public T Get<T>()
|
||||
{
|
||||
return (T)Get(typeof(T));
|
||||
}
|
||||
/// <inheritdoc />
|
||||
public object Get(Type t)
|
||||
{
|
||||
object result;
|
||||
@@ -43,6 +49,7 @@ namespace Discord.Commands
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryGet<T>(out T result)
|
||||
{
|
||||
object untypedResult;
|
||||
@@ -57,6 +64,7 @@ namespace Discord.Commands
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <inheritdoc />
|
||||
public bool TryGet(Type t, out object result)
|
||||
{
|
||||
Func<object> func;
|
||||
|
||||
@@ -4,15 +4,59 @@ namespace Discord.Commands
|
||||
{
|
||||
public interface IDependencyMap
|
||||
{
|
||||
/// <summary>
|
||||
/// 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>
|
||||
void Add<T>(T obj) where T : class;
|
||||
/// <summary>
|
||||
/// Add a service that will be injected by a new instance every time.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of instance to inject.</typeparam>
|
||||
void AddTransient<T>() where T : class, new();
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// <example>
|
||||
/// map.AddTransient<IService, Service>
|
||||
/// </example>
|
||||
void AddTransient<TKey, TImpl>() where TKey: class where TImpl : class, TKey, new();
|
||||
/// <summary>
|
||||
/// 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>
|
||||
void AddFactory<T>(Func<T> factory) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// Pull an object from the map.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of service.</typeparam>
|
||||
/// <returns>An instance of this service.</returns>
|
||||
T Get<T>();
|
||||
/// <summary>
|
||||
/// Try to pull an object from the map.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of service.</typeparam>
|
||||
/// <param name="result">The instance of this service.</param>
|
||||
/// <returns>Whether or not this object could be found in the map.</returns>
|
||||
bool TryGet<T>(out T result);
|
||||
|
||||
/// <summary>
|
||||
/// Pull an object from the map.
|
||||
/// </summary>
|
||||
/// <param name="t">The type of service.</param>
|
||||
/// <returns>An instance of this service.</returns>
|
||||
object Get(Type t);
|
||||
/// <summary>
|
||||
/// Try to pull an object from the map.
|
||||
/// </summary>
|
||||
/// <param name="t">The type of service.</param>
|
||||
/// <param name="result">An instance of this service.</param>
|
||||
/// <returns>Whether or not this object could be found in the map.</returns>
|
||||
bool TryGet(Type t, out object result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user