diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml
index b0613d0..7872d01 100644
--- a/LightweightIocContainer/LightweightIocContainer.xml
+++ b/LightweightIocContainer/LightweightIocContainer.xml
@@ -1351,6 +1351,11 @@
The of the multiton scope
+
+
+ Validate the
+
+
for open generic types
diff --git a/LightweightIocContainer/Registrations/MultitonRegistration.cs b/LightweightIocContainer/Registrations/MultitonRegistration.cs
index 144833a..0d9a265 100644
--- a/LightweightIocContainer/Registrations/MultitonRegistration.cs
+++ b/LightweightIocContainer/Registrations/MultitonRegistration.cs
@@ -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 of the multiton scope
///
public Type Scope { get; }
+
+ ///
+ /// Validate the
+ ///
+ 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();
+ }
}
}
\ No newline at end of file
diff --git a/LightweightIocContainer/Registrations/RegistrationBase.cs b/LightweightIocContainer/Registrations/RegistrationBase.cs
index 842af64..ab53eb1 100644
--- a/LightweightIocContainer/Registrations/RegistrationBase.cs
+++ b/LightweightIocContainer/Registrations/RegistrationBase.cs
@@ -173,12 +173,12 @@ namespace LightweightIocContainer.Registrations
/// Validate the
///
/// No create method that can create the
- 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}.");
}
diff --git a/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs b/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs
index 9535138..b437f41 100644
--- a/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs
+++ b/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs
@@ -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();
}
-
+
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() => throw new System.NotImplementedException();
+ }
+
+ [UsedImplicitly]
+ public interface IMultitonTestFactory
+ {
+ ITest Create(MultitonScope scope);
+ void ClearMultitonInstance();
+ }
+
+ [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().WithFactory());
+ _iocContainer.Register(r => r.AddMultiton().WithFactory());
MultitonScope scope1 = new();
MultitonScope scope2 = new();
- ITestFactory testFactory = _iocContainer.Resolve();
+ IMultitonTestFactory testFactory = _iocContainer.Resolve();
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().WithFactory());
+ _iocContainer.Register(r => r.AddMultiton().WithFactory());
MultitonScope scope1 = new();
MultitonScope scope2 = new();
- ITestFactory testFactory = _iocContainer.Resolve();
+ IMultitonTestFactory testFactory = _iocContainer.Resolve();
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(() => _iocContainer.Register(r => r.AddMultiton().WithFactory()));
+
+ [Test]
+ public void InvalidMultitonFactoryRegistrationFactoryWithoutScopeAsFirstParameter() =>
+ Assert.Throws(() => _iocContainer.Register(r => r.AddMultiton().WithFactory()));
+
[Test]
public void TestInvalidCreateMethodReturnType() =>
Assert.Throws(() => _iocContainer.Register(r => r.Add().WithFactory()));