-
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 DependencyScope utility class
- Loading branch information
Showing
6 changed files
with
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using NUnit.Framework; | ||
using UIComponents.Utilities; | ||
|
||
namespace UIComponents.Tests | ||
{ | ||
[TestFixture] | ||
public class DependencyScopeTests | ||
{ | ||
public interface IDependency {} | ||
|
||
public class DependencyClass : IDependency {} | ||
|
||
public class NewDependencyClass : IDependency {} | ||
|
||
public class UIComponentWithNoDependencyAttribute : UIComponent | ||
{ | ||
public IDependency GetDependency() => Provide<IDependency>(); | ||
} | ||
|
||
[Dependency(typeof(IDependency), provide: typeof(DependencyClass))] | ||
public class UIComponentWithDependency : UIComponentWithNoDependencyAttribute {} | ||
|
||
[Test] | ||
public void Should_Set_Dependency_And_Restore_Previous() | ||
{ | ||
var component = new UIComponentWithDependency(); | ||
|
||
using (new DependencyScope<UIComponentWithDependency, IDependency>(new NewDependencyClass())) | ||
{ | ||
Assert.That(component.GetDependency(), Is.InstanceOf<NewDependencyClass>()); | ||
} | ||
|
||
Assert.That(component.GetDependency(), Is.InstanceOf<DependencyClass>()); | ||
} | ||
|
||
[Test] | ||
public void Should_Set_Dependency_And_Clear_It_If_No_Previous_Exists() | ||
{ | ||
var component = new UIComponentWithNoDependencyAttribute(); | ||
|
||
using (new DependencyScope<UIComponentWithNoDependencyAttribute, IDependency>(new NewDependencyClass())) | ||
{ | ||
Assert.That(component.GetDependency(), Is.InstanceOf<NewDependencyClass>()); | ||
} | ||
|
||
Assert.Throws<MissingProviderException>(() => component.GetDependency()); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
|
||
namespace UIComponents.Utilities | ||
{ | ||
/// <summary> | ||
/// A helper class for setting a dependency temporarily. | ||
/// When disposed, restores the previous provider, or the lack thereof. | ||
/// Built for unit tests. | ||
/// </summary> | ||
/// <typeparam name="TConsumer">Type of the consumer</typeparam> | ||
/// <typeparam name="TDependency">Type of the dependency</typeparam> | ||
public class DependencyScope<TConsumer, TDependency> : IDisposable | ||
where TConsumer : class | ||
where TDependency : class | ||
{ | ||
private readonly TDependency _previousDependencyProvider; | ||
|
||
public DependencyScope(TDependency instance) | ||
{ | ||
var injector = DependencyInjector.GetInjector(typeof(TConsumer)); | ||
|
||
if (injector.TryProvide<TDependency>(out var currentProvider)) | ||
_previousDependencyProvider = currentProvider; | ||
|
||
DependencyInjector.SetDependency<TConsumer, TDependency>(instance); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_previousDependencyProvider == null) | ||
DependencyInjector.ClearDependency<TConsumer, TDependency>(); | ||
else | ||
DependencyInjector.SetDependency<TConsumer, TDependency>(_previousDependencyProvider); | ||
} | ||
} | ||
} |
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