- add validation for multiton factories

pull/57/head
Simon G 4 years ago
parent 3acd6adfea
commit 775afe4e45
  1. 5
      LightweightIocContainer/LightweightIocContainer.xml
  2. 19
      LightweightIocContainer/Registrations/MultitonRegistration.cs
  3. 4
      LightweightIocContainer/Registrations/RegistrationBase.cs
  4. 35
      Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs

@ -1351,6 +1351,11 @@
The <see cref="T:System.Type"/> of the multiton scope
</summary>
</member>
<member name="M:LightweightIocContainer.Registrations.MultitonRegistration`2.ValidateFactory">
<summary>
Validate the <see cref="P:LightweightIocContainer.Registrations.RegistrationBase.Factory"/>
</summary>
</member>
<member name="T:LightweightIocContainer.Registrations.OpenGenericRegistration">
<summary>
<see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/> for open generic types

@ -3,6 +3,8 @@
// Copyright(c) 2019 SimonG. All Rights Reserved.
using System;
using System.Linq;
using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Registrations;
@ -30,5 +32,22 @@ namespace LightweightIocContainer.Registrations
/// The <see cref="Type"/> of the multiton scope
/// </summary>
public Type Scope { get; }
/// <summary>
/// Validate the <see cref="RegistrationBase.Factory"/>
/// </summary>
protected override void ValidateFactory()
{
if (Factory == null)
return;
if (Factory.CreateMethods.Any(c => c.GetParameters().Length == 0))
throw new InvalidFactoryRegistrationException($"Create methods without parameters are not valid for multitons (Type: {InterfaceType}).");
if (Factory.CreateMethods.Any(c => c.GetParameters()[0].ParameterType != Scope))
throw new InvalidFactoryRegistrationException($"Create methods without scope type ({Scope}) as first parameter are not valid for multitons (Type: {InterfaceType}).");
base.ValidateFactory();
}
}
}

@ -173,12 +173,12 @@ namespace LightweightIocContainer.Registrations
/// Validate the <see cref="Factory"/>
/// </summary>
/// <exception cref="InvalidFactoryRegistrationException">No create method that can create the <see cref="InterfaceType"/></exception>
private void ValidateFactory()
protected virtual void ValidateFactory()
{
if (Factory == null)
return;
if (Factory.CreateMethods.Any(c => c.ReturnType == InterfaceType) != true)
if (Factory.CreateMethods.All(c => c.ReturnType != InterfaceType))
throw new InvalidFactoryRegistrationException($"No create method that can create {InterfaceType}.");
}

@ -61,21 +61,30 @@ namespace Test.LightweightIocContainer
{
ITest Create();
ITest Create(string name);
ITest Create(MultitonScope scope);
ITest CreateTest(string name = null);
ITest Create(byte id);
void ClearMultitonInstance<T>();
}
private class TestFactory : ITestFactory
{
public ITest Create() => new Test();
public ITest Create(string name) => throw new System.NotImplementedException();
public ITest Create(MultitonScope scope) => throw new System.NotImplementedException();
public ITest CreateTest(string name = null) => throw new System.NotImplementedException();
public ITest Create(byte id) => throw new System.NotImplementedException();
public void ClearMultitonInstance<T>() => throw new System.NotImplementedException();
}
[UsedImplicitly]
public interface IMultitonTestFactory
{
ITest Create(MultitonScope scope);
void ClearMultitonInstance<T>();
}
[UsedImplicitly]
public interface IInvalidMultitonTestFactory
{
ITest Create(MultitonScope scope);
ITest Create(int number);
}
[UsedImplicitly]
@ -188,12 +197,12 @@ namespace Test.LightweightIocContainer
[Test]
public void TestResolveMultitonFromFactory()
{
_iocContainer.Register(r => r.AddMultiton<ITest, Test, MultitonScope>().WithFactory<ITestFactory>());
_iocContainer.Register(r => r.AddMultiton<ITest, Test, MultitonScope>().WithFactory<IMultitonTestFactory>());
MultitonScope scope1 = new();
MultitonScope scope2 = new();
ITestFactory testFactory = _iocContainer.Resolve<ITestFactory>();
IMultitonTestFactory testFactory = _iocContainer.Resolve<IMultitonTestFactory>();
ITest resolvedTest1 = testFactory.Create(scope1);
ITest resolvedTest2 = testFactory.Create(scope1);
@ -207,12 +216,12 @@ namespace Test.LightweightIocContainer
[Test]
public void TestResolveMultitonFromFactoryClearInstances()
{
_iocContainer.Register(r => r.AddMultiton<ITest, Test, MultitonScope>().WithFactory<ITestFactory>());
_iocContainer.Register(r => r.AddMultiton<ITest, Test, MultitonScope>().WithFactory<IMultitonTestFactory>());
MultitonScope scope1 = new();
MultitonScope scope2 = new();
ITestFactory testFactory = _iocContainer.Resolve<ITestFactory>();
IMultitonTestFactory testFactory = _iocContainer.Resolve<IMultitonTestFactory>();
ITest resolvedTest1 = testFactory.Create(scope1);
ITest resolvedTest2 = testFactory.Create(scope1);
@ -232,6 +241,14 @@ namespace Test.LightweightIocContainer
Assert.AreNotSame(resolvedTest3, resolvedTest5);
}
[Test]
public void InvalidMultitonFactoryRegistrationFactoryWithoutParameter() =>
Assert.Throws<InvalidFactoryRegistrationException>(() => _iocContainer.Register(r => r.AddMultiton<ITest, Test, MultitonScope>().WithFactory<ITestFactory>()));
[Test]
public void InvalidMultitonFactoryRegistrationFactoryWithoutScopeAsFirstParameter() =>
Assert.Throws<InvalidFactoryRegistrationException>(() => _iocContainer.Register(r => r.AddMultiton<ITest, Test, MultitonScope>().WithFactory<IInvalidMultitonTestFactory>()));
[Test]
public void TestInvalidCreateMethodReturnType() =>
Assert.Throws<InvalidFactoryRegistrationException>(() => _iocContainer.Register(r => r.Add<ITest, Test>().WithFactory<ITestFactoryWrongReturn>()));

Loading…
Cancel
Save