diff --git a/LightweightIocContainer/Registrations/RegistrationFactory.cs b/LightweightIocContainer/Registrations/RegistrationFactory.cs index f70a807..9b59a29 100644 --- a/LightweightIocContainer/Registrations/RegistrationFactory.cs +++ b/LightweightIocContainer/Registrations/RegistrationFactory.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(typeof(TInterface), typeof(TImplementation), typeof(TScope)); } + /// + /// Register an Interface with a Type that implements it and create a + /// + /// The Interface to register + /// The Type that implements the + /// The for this + /// A new created with the given parameters + 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); + } + + /// + /// Register an Interface with a Type that implements it as a multiton and create a + /// + /// The Interface to register + /// The Type that implements the + /// The Type of the multiton scope + /// A new created with the given parameters + public static IRegistrationBase Register(Type tInterface, Type tImplementation, Type tScope) + { + Type multitonRegistrationType = typeof(MultitonRegistration<>).MakeGenericType(tInterface); + return (IRegistrationBase)Activator.CreateInstance(multitonRegistrationType, tInterface, tImplementation, tScope); + } + /// /// Register an Interface as an abstract typed factory and create a /// @@ -47,5 +74,17 @@ namespace LightweightIocContainer.Registrations { return new TypedFactoryRegistration(typeof(TFactory), container); } + + /// + /// Register an Interface as an abstract typed factory and create a + /// + /// The abstract typed factory to register + /// The current + /// A new created with the given parameters + public static IRegistrationBase RegisterFactory(Type tFactory, IIocContainer container) + { + Type factoryRegistrationType = typeof(TypedFactoryRegistration<>).MakeGenericType(tFactory); + return (IRegistrationBase)Activator.CreateInstance(factoryRegistrationType, tFactory, container); + } } } \ No newline at end of file diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index 637c7ce..d9be809 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -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() {