-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add abstract DependencyConsumer base class for DI consumers (#108)
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace UIComponents.DependencyInjection | ||
{ | ||
/// <summary> | ||
/// An abstract base class for classes which want to access | ||
/// UIComponents's dependency injection system. | ||
/// <example> | ||
/// <code> | ||
/// [Dependency(typeof(IMyDependency), provide: typeof(MyDependency))] | ||
/// public class MyClass : DependencyConsumer | ||
/// { | ||
/// [Provide] | ||
/// private IMyDependency _myDependency; | ||
/// } | ||
/// </code> | ||
/// </example> | ||
/// <seealso cref="DependencyAttribute"/> | ||
/// <seealso cref="ProvideAttribute"/> | ||
/// </summary> | ||
public abstract class DependencyConsumer : IDependencyConsumer | ||
{ | ||
private static readonly IDependency[] EmptyDependencies = Array.Empty<IDependency>(); | ||
|
||
private readonly DependencyInjector _dependencyInjector; | ||
|
||
internal DependencyConsumer() | ||
{ | ||
DiContext.Current.RegisterConsumer(this); | ||
_dependencyInjector = DiContext.Current.GetInjector(GetType()); | ||
|
||
// ReSharper disable once VirtualMemberCallInConstructor | ||
UIC_PopulateProvideFields(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a dependency. Throws a <see cref="MissingProviderException"/> | ||
/// if the dependency can not be provided. | ||
/// </summary> | ||
/// <typeparam name="T">Dependency type</typeparam> | ||
/// <exception cref="MissingProviderException"> | ||
/// Thrown if the dependency can not be provided | ||
/// </exception> | ||
/// <returns>Dependency instance</returns> | ||
protected T Provide<T>() where T : class | ||
{ | ||
return _dependencyInjector.Provide<T>(); | ||
} | ||
|
||
/// <summary> | ||
/// Attempts to provide a dependency. Returns whether | ||
/// the dependency could be provided. | ||
/// </summary> | ||
/// <param name="instance">Dependency instance</param> | ||
/// <typeparam name="T">Dependency type</typeparam> | ||
/// <returns>Whether the dependency could be fetched</returns> | ||
protected bool TryProvide<T>(out T instance) where T : class | ||
{ | ||
return _dependencyInjector.TryProvide(out instance); | ||
} | ||
|
||
public virtual IEnumerable<IDependency> GetDependencies() => EmptyDependencies; | ||
|
||
protected virtual void UIC_PopulateProvideFields() {} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters