- don't allow registration of lifestyle.multiton without iMultitonRegistration

- add iLifestyleProvider and move Lifestyle from IRegistratrionBase<> to it
- add non generic IMultitonRegistration as base class
pull/43/head
Simon G 6 years ago
parent f5fefa400a
commit 19a20ffd92
  1. 17
      LightweightIocContainer/Interfaces/Registrations/ILifestyleProvider.cs
  2. 10
      LightweightIocContainer/Interfaces/Registrations/IMultitonRegistration.cs
  3. 7
      LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs
  4. 6
      LightweightIocContainer/IocContainer.cs
  5. 20
      LightweightIocContainer/LightweightIocContainer.xml
  6. 6
      Test.LightweightIocContainer/IocContainerTest.cs

@ -0,0 +1,17 @@
// // Author: Simon Gockner
// // Created: 2020-01-29
// // Copyright(c) 2020 SimonG. All Rights Reserved.
namespace LightweightIocContainer.Interfaces.Registrations
{
/// <summary>
/// Provides a <see cref="LightweightIocContainer.Lifestyle"/> to an <see cref="IRegistration"/>
/// </summary>
public interface ILifestyleProvider
{
/// <summary>
/// The Lifestyle of Instances that are created with this <see cref="IRegistration"/>
/// </summary>
Lifestyle Lifestyle { get; }
}
}

@ -6,10 +6,18 @@ using System;
namespace LightweightIocContainer.Interfaces.Registrations namespace LightweightIocContainer.Interfaces.Registrations
{ {
/// <summary>
/// Non generic <see cref="IMultitonRegistration{TInterface}"/>
/// </summary>
public interface IMultitonRegistration
{
}
/// <summary> /// <summary>
/// A base <see cref="IMultitonRegistration{TInterface}"/> without implementation /// A base <see cref="IMultitonRegistration{TInterface}"/> without implementation
/// </summary> /// </summary>
public interface IMultitonRegistration<TInterface> : IRegistrationBase<TInterface> public interface IMultitonRegistration<TInterface> : IRegistrationBase<TInterface>, IMultitonRegistration
{ {
/// <summary> /// <summary>
/// The <see cref="Type"/> of the multiton scope /// The <see cref="Type"/> of the multiton scope

@ -10,11 +10,8 @@ namespace LightweightIocContainer.Interfaces.Registrations
/// The <see cref="IRegistrationBase{TInterface}"/> that is used to register an Interface /// The <see cref="IRegistrationBase{TInterface}"/> that is used to register an Interface
/// </summary> /// </summary>
/// <typeparam name="TInterface">The registered Interface</typeparam> /// <typeparam name="TInterface">The registered Interface</typeparam>
public interface IRegistrationBase<TInterface> : IRegistration, IWithParameters<TInterface> public interface IRegistrationBase<TInterface> : IRegistration, ILifestyleProvider, IWithParameters<TInterface>
{ {
/// <summary>
/// The Lifestyle of Instances that are created with this <see cref="IRegistrationBase{TInterface}"/>
/// </summary>
Lifestyle Lifestyle { get; }
} }
} }

@ -188,12 +188,16 @@ namespace LightweightIocContainer
/// </summary> /// </summary>
/// <param name="registration">The given <see cref="IRegistration"/></param> /// <param name="registration">The given <see cref="IRegistration"/></param>
/// <exception cref="MultipleRegistrationException">The <see cref="Type"/> is already registered in this <see cref="IocContainer"/></exception> /// <exception cref="MultipleRegistrationException">The <see cref="Type"/> is already registered in this <see cref="IocContainer"/></exception>
private void Register(IRegistration registration) //FixMe: Don't allow lifestyle.multiton without iMultitonRegistration private void Register(IRegistration registration)
{ {
//if type is already registered //if type is already registered
if (_registrations.Any(r => r.InterfaceType == registration.InterfaceType)) if (_registrations.Any(r => r.InterfaceType == registration.InterfaceType))
throw new MultipleRegistrationException(registration.InterfaceType); throw new MultipleRegistrationException(registration.InterfaceType);
//don't allow lifestyle.multiton without iMultitonRegistration
if (registration is ILifestyleProvider lifestyleProvider && lifestyleProvider.Lifestyle == Lifestyle.Multiton && !(registration is IMultitonRegistration))
throw new InvalidRegistrationException("Can't register a type as Lifestyle.Multiton without a scope (Registration is not of type IMultitonRegistration).");
_registrations.Add(registration); _registrations.Add(registration);
} }

@ -527,6 +527,16 @@
<typeparam name="TInterface">The <see cref="T:System.Type"/> of the interface</typeparam> <typeparam name="TInterface">The <see cref="T:System.Type"/> of the interface</typeparam>
<typeparam name="TImplementation">The <see cref="T:System.Type"/> of the implementation</typeparam> <typeparam name="TImplementation">The <see cref="T:System.Type"/> of the implementation</typeparam>
</member> </member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.ILifestyleProvider">
<summary>
Provides a <see cref="T:LightweightIocContainer.Lifestyle"/> to an <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/>
</summary>
</member>
<member name="P:LightweightIocContainer.Interfaces.Registrations.ILifestyleProvider.Lifestyle">
<summary>
The Lifestyle of Instances that are created with this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/>
</summary>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.IMultipleRegistration`2"> <member name="T:LightweightIocContainer.Interfaces.Registrations.IMultipleRegistration`2">
<summary> <summary>
The base interface for every <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultipleRegistration`3"/> to register multiple interfaces The base interface for every <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultipleRegistration`3"/> to register multiple interfaces
@ -577,6 +587,11 @@
<typeparam name="TInterface5">The fifth interface</typeparam> <typeparam name="TInterface5">The fifth interface</typeparam>
<typeparam name="TImplementation">The implementation</typeparam> <typeparam name="TImplementation">The implementation</typeparam>
</member> </member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration">
<summary>
Non generic <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1"/>
</summary>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1"> <member name="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1">
<summary> <summary>
A base <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1"/> without implementation A base <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1"/> without implementation
@ -615,11 +630,6 @@
</summary> </summary>
<typeparam name="TInterface">The registered Interface</typeparam> <typeparam name="TInterface">The registered Interface</typeparam>
</member> </member>
<member name="P:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.Lifestyle">
<summary>
The Lifestyle of Instances that are created with this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/>
</summary>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.ISingleTypeRegistration`1"> <member name="T:LightweightIocContainer.Interfaces.Registrations.ISingleTypeRegistration`1">
<summary> <summary>
The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> to register either only an interface or only a <see cref="T:System.Type"/> The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> to register either only an interface or only a <see cref="T:System.Type"/>

@ -182,6 +182,12 @@ namespace Test.LightweightIocContainer
Assert.DoesNotThrow(() => _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>()); Assert.DoesNotThrow(() => _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>());
} }
[Test]
public void TestInvalidMultitonRegistration()
{
Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Register<ITest, Test>(Lifestyle.Multiton));
}
[Test] [Test]
public void TestRegisterFactory() public void TestRegisterFactory()
{ {

Loading…
Cancel
Save