Throw a better exception than an NRE if they forget to call builder.Bind(..).To(..)

Fixes #114
This commit is contained in:
Antony Male 2020-04-23 17:58:28 +01:00
parent bc068f8b27
commit 9f7b596a5e
2 changed files with 13 additions and 2 deletions

View File

@ -12,7 +12,7 @@ namespace StyletIoC.Internal.Builders
private readonly Func<IEnumerable<Assembly>, string, IEnumerable<Assembly>> getAssemblies; private readonly Func<IEnumerable<Assembly>, string, IEnumerable<Assembly>> getAssemblies;
public List<BuilderTypeKey> ServiceTypes { get; private set; } public List<BuilderTypeKey> ServiceTypes { get; private set; }
private BuilderBindingBase builderBinding; private BuilderBindingBase builderBinding;
public bool IsWeak { get { return this.builderBinding.IsWeak; } } public bool IsWeak { get { return this.builderBinding?.IsWeak ?? false; } }
public BuilderBindTo(Type serviceType, Func<IEnumerable<Assembly>, string, IEnumerable<Assembly>> getAssemblies) public BuilderBindTo(Type serviceType, Func<IEnumerable<Assembly>, string, IEnumerable<Assembly>> getAssemblies)
{ {
@ -91,6 +91,9 @@ namespace StyletIoC.Internal.Builders
internal void Build(Container container) internal void Build(Container container)
{ {
if (this.builderBinding == null)
throw new StyletIoCRegistrationException(String.Format("Service type {0} is not bound to anything", this.ServiceTypes[0].Type.GetDescription()));
this.builderBinding.Build(container); this.builderBinding.Build(container);
} }
} }

View File

@ -86,11 +86,19 @@ namespace StyletUnitTests.StyletIoC
} }
[Test] [Test]
public void AllowsInstanceTobeInterfaceType() public void AllowsInstanceToBeInterfaceType()
{ {
var builder = new StyletIoCBuilder(); var builder = new StyletIoCBuilder();
I1 i1 = new C1(); I1 i1 = new C1();
Assert.DoesNotThrow(() => builder.Bind<I1>().ToInstance(i1)); Assert.DoesNotThrow(() => builder.Bind<I1>().ToInstance(i1));
} }
[Test]
public void ThrowsIfMissingBuilderBinding()
{
var builder = new StyletIoCBuilder();
builder.Bind<C1>();
Assert.Throws<StyletIoCRegistrationException>(() => builder.BuildContainer());
}
} }
} }