diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml index 46a5869..48a550f 100644 --- a/LightweightIocContainer/LightweightIocContainer.xml +++ b/LightweightIocContainer/LightweightIocContainer.xml @@ -561,6 +561,14 @@ The for this A new created with the given parameters + + + Register a without an interface and create a + + The to register + The for this + A new created with the given parameters + Register an Interface with a Type that implements it as a multiton and create a @@ -579,6 +587,14 @@ The for this A new created with the given parameters + + + Register a without an interface and create a + + The to register + The for this + A new created with the given parameters + Register an Interface with a Type that implements it as a multiton and create a diff --git a/LightweightIocContainer/Registrations/RegistrationFactory.cs b/LightweightIocContainer/Registrations/RegistrationFactory.cs index 7aded86..c07e8ca 100644 --- a/LightweightIocContainer/Registrations/RegistrationFactory.cs +++ b/LightweightIocContainer/Registrations/RegistrationFactory.cs @@ -3,6 +3,7 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; +using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Registrations; @@ -26,6 +27,20 @@ namespace LightweightIocContainer.Registrations return new DefaultRegistration(typeof(TInterface), typeof(TImplementation), lifestyle); } + /// + /// Register a without an interface and create a + /// + /// The to register + /// The for this + /// A new created with the given parameters + public static IDefaultRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) + { + if (typeof(TImplementation).IsInterface) + throw new InvalidRegistrationException("Can't register an interface without its implementation type."); + + return Register(lifestyle); + } + /// /// Register an Interface with a Type that implements it as a multiton and create a /// @@ -51,6 +66,20 @@ namespace LightweightIocContainer.Registrations return (IRegistrationBase)Activator.CreateInstance(defaultRegistrationType, tInterface, tImplementation, lifestyle); } + /// + /// Register a without an interface and create a + /// + /// The to register + /// The for this + /// A new created with the given parameters + public static IRegistrationBase Register(Type tImplementation, Lifestyle lifestyle = Lifestyle.Transient) + { + if (tImplementation.IsInterface) + throw new InvalidRegistrationException("Can't register an interface without its implementation type."); + + return Register(tImplementation, tImplementation, lifestyle); + } + /// /// Register an Interface with a Type that implements it as a multiton and create a /// diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index 9f91850..b9dc39e 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -117,6 +117,7 @@ namespace Test.LightweightIocContainer public void TestRegister() { Assert.DoesNotThrow(() => _iocContainer.Register(RegistrationFactory.Register(typeof(ITest), typeof(Test)))); + Assert.DoesNotThrow(() => _iocContainer.Register(RegistrationFactory.Register(typeof(Test)))); Assert.DoesNotThrow(() => _iocContainer.Register(RegistrationFactory.RegisterFactory(typeof(ITestFactory), _iocContainer))); } @@ -128,6 +129,13 @@ namespace Test.LightweightIocContainer Assert.AreEqual(typeof(ITest), exception.Type); } + [Test] + public void TestRegisterInterfaceWithoutImplementation() + { + Assert.Throws(() => _iocContainer.Register(RegistrationFactory.Register())); + Assert.Throws(() => _iocContainer.Register(RegistrationFactory.Register(typeof(ITest)))); + } + [Test] public void TestRegisterFactoryWithoutCreate() { @@ -157,6 +165,16 @@ namespace Test.LightweightIocContainer Assert.IsInstanceOf(resolvedTest); } + [Test] + public void TestResolveWithoutInterface() + { + _iocContainer.Register(RegistrationFactory.Register()); + + Test resolvedTest = _iocContainer.Resolve(); + + Assert.IsInstanceOf(resolvedTest); + } + [Test] public void TestResolveWithParams() { @@ -363,6 +381,9 @@ namespace Test.LightweightIocContainer _iocContainer.Register(RegistrationFactory.Register()); Assert.True(_iocContainer.IsTypeRegistered()); + + _iocContainer.Register(RegistrationFactory.Register()); + Assert.True(_iocContainer.IsTypeRegistered()); } } } \ No newline at end of file