- add posibility to register types that use open generics

pull/32/head
Simon Gockner 7 years ago
parent 6782c8e1e9
commit 6e66631636
  1. 39
      LightweightIocContainer/Registrations/RegistrationFactory.cs
  2. 9
      Test.LightweightIocContainer/IocContainerTest.cs

@ -2,6 +2,7 @@
// Created: 2019-05-20
// Copyright(c) 2019 SimonG. All Rights Reserved.
using System;
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations;
@ -37,6 +38,32 @@ namespace LightweightIocContainer.Registrations
return new MultitonRegistration<TInterface>(typeof(TInterface), typeof(TImplementation), typeof(TScope));
}
/// <summary>
/// Register an Interface with a Type that implements it and create a <see cref="IDefaultRegistration{TInterface}"/>
/// </summary>
/// <param name="tInterface">The Interface to register</param>
/// <param name="tImplementation">The Type that implements the <see cref="tInterface"/></param>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IDefaultRegistration{TInterface}"/></param>
/// <returns>A new created <see cref="IDefaultRegistration{TInterface}"/> with the given parameters</returns>
public static IRegistrationBase Register(Type tInterface, Type tImplementation, Lifestyle lifestyle = Lifestyle.Transient)
{
Type defaultRegistrationType = typeof(DefaultRegistration<>).MakeGenericType(tInterface);
return (IRegistrationBase)Activator.CreateInstance(defaultRegistrationType, tInterface, tImplementation, lifestyle);
}
/// <summary>
/// Register an Interface with a Type that implements it as a multiton and create a <see cref="IMultitonRegistration{TInterface}"/>
/// </summary>
/// <param name="tInterface">The Interface to register</param>
/// <param name="tImplementation">The Type that implements the <see cref="tInterface"/></param>
/// <param name="tScope">The Type of the multiton scope</param>
/// <returns>A new created <see cref="IMultitonRegistration{TInterface}"/> with the given parameters</returns>
public static IRegistrationBase Register(Type tInterface, Type tImplementation, Type tScope)
{
Type multitonRegistrationType = typeof(MultitonRegistration<>).MakeGenericType(tInterface);
return (IRegistrationBase)Activator.CreateInstance(multitonRegistrationType, tInterface, tImplementation, tScope);
}
/// <summary>
/// Register an Interface as an abstract typed factory and create a <see cref="ITypedFactoryRegistration{TFactory}"/>
/// </summary>
@ -47,5 +74,17 @@ namespace LightweightIocContainer.Registrations
{
return new TypedFactoryRegistration<TFactory>(typeof(TFactory), container);
}
/// <summary>
/// Register an Interface as an abstract typed factory and create a <see cref="ITypedFactoryRegistration{TFactory}"/>
/// </summary>
/// <param name="tFactory">The abstract typed factory to register</param>
/// <param name="container">The current <see cref="IIocContainer"/></param>
/// <returns>A new created <see cref="ITypedFactoryRegistration{TFactory}"/> with the given parameters</returns>
public static IRegistrationBase RegisterFactory(Type tFactory, IIocContainer container)
{
Type factoryRegistrationType = typeof(TypedFactoryRegistration<>).MakeGenericType(tFactory);
return (IRegistrationBase)Activator.CreateInstance(factoryRegistrationType, tFactory, container);
}
}
}

@ -85,6 +85,15 @@ namespace Test.LightweightIocContainer
Assert.AreEqual(iocContainer, returnedContainer);
}
[Test]
public void TestRegister()
{
IIocContainer iocContainer = new IocContainer();
Assert.DoesNotThrow(() => iocContainer.Register(RegistrationFactory.Register(typeof(ITest), typeof(Test))));
Assert.DoesNotThrow(() => iocContainer.Register(RegistrationFactory.RegisterFactory(typeof(ITestFactory), iocContainer)));
}
[Test]
public void TestRegisterMultiple()
{

Loading…
Cancel
Save