Skip to content

Commit

Permalink
feat(DependencyInjector): throw exception in Provide<T> if no provide…
Browse files Browse the repository at this point in the history
…r exists
  • Loading branch information
jonisavo committed May 1, 2022
1 parent caa2e1b commit 4ccebe5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
14 changes: 7 additions & 7 deletions Core/DependencyInjector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using UIComponents.Core.Exceptions;
using UnityEngine;

namespace UIComponents.Core
Expand Down Expand Up @@ -72,18 +73,17 @@ private static object CreateInstance(Type dependencyType)
DependencyDictionary[typeof(T)] = instance;
}

[NotNull]
public T Provide<T>() where T : class
{
var type = typeof(T);

if (!DependencyDictionary.ContainsKey(type))
throw new MissingProviderException(type);

if (DependencyDictionary.ContainsKey(type))
return (T) DependencyDictionary[type];

Debug.LogWarningFormat("Could not get dependency {0}", type.Name);

return null;
return (T) DependencyDictionary[type];
}

private void PopulateFromDependencyAttributes(IEnumerable<DependencyAttribute> dependencyAttributes)
{
foreach (var dependencyAttribute in dependencyAttributes)
Expand Down
3 changes: 3 additions & 0 deletions Core/Exceptions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Core/Exceptions/MissingProviderException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace UIComponents.Core.Exceptions
{
public class MissingProviderException : Exception
{
public readonly Type DependencyType;

public MissingProviderException(Type dependencyType)
{
DependencyType = dependencyType;
Message = $"No provider found for {dependencyType.Name}";
}

public override string Message { get; }
}
}
3 changes: 3 additions & 0 deletions Core/Exceptions/MissingProviderException.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions Tests/DependencyInjectorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using NUnit.Framework;
using UIComponents.Core;
using UIComponents.Core.Exceptions;

namespace UIComponents.Tests
{
Expand All @@ -12,6 +13,28 @@ private interface IDependency {}
public class DependencyOne : IDependency {}

public class DependencyTwo : IDependency {}

[TestFixture]
public class Provide
{
[Test]
public void Returns_Desired_Dependency()
{
var injector = new DependencyInjector();
injector.SetDependency<IDependency>(new DependencyOne());
Assert.That(injector.Provide<IDependency>(), Is.InstanceOf<DependencyOne>());
}

[Test]
public void Throws_If_No_Provider_Exists()
{
var injector = new DependencyInjector();

Assert.Throws<MissingProviderException>(
() => injector.Provide<IDependency>()
);
}
}

[TestFixture]
public class SetDependency
Expand All @@ -22,9 +45,6 @@ public void Switches_The_Dependency()
var injector = new DependencyInjector();

injector.SetDependency<IDependency>(new DependencyOne());

Assert.That(injector.Provide<IDependency>(), Is.InstanceOf<DependencyOne>());

injector.SetDependency<IDependency>(new DependencyTwo());

Assert.That(injector.Provide<IDependency>(), Is.InstanceOf<DependencyTwo>());
Expand Down

0 comments on commit 4ccebe5

Please sign in to comment.