#21: allow registration of a type without an interface

pull/32/head
Simon Gockner 6 years ago
parent d91392e382
commit de702e72a9
  1. 16
      LightweightIocContainer/LightweightIocContainer.xml
  2. 29
      LightweightIocContainer/Registrations/RegistrationFactory.cs
  3. 21
      Test.LightweightIocContainer/IocContainerTest.cs

@ -561,6 +561,14 @@
<param name="lifestyle">The <see cref="T:LightweightIocContainer.Lifestyle"/> for this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/></param> <param name="lifestyle">The <see cref="T:LightweightIocContainer.Lifestyle"/> for this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/></param>
<returns>A new created <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/> with the given parameters</returns> <returns>A new created <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/> with the given parameters</returns>
</member> </member>
<member name="M:LightweightIocContainer.Registrations.RegistrationFactory.Register``1(LightweightIocContainer.Lifestyle)">
<summary>
Register a <see cref="T:System.Type"/> without an interface and create a <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/>
</summary>
<typeparam name="TImplementation">The <see cref="T:System.Type"/> to register</typeparam>
<param name="lifestyle">The <see cref="T:LightweightIocContainer.Lifestyle"/> for this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/></param>
<returns>A new created <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/> with the given parameters</returns>
</member>
<member name="M:LightweightIocContainer.Registrations.RegistrationFactory.Register``3"> <member name="M:LightweightIocContainer.Registrations.RegistrationFactory.Register``3">
<summary> <summary>
Register an Interface with a Type that implements it as a multiton and create a <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1"/> Register an Interface with a Type that implements it as a multiton and create a <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1"/>
@ -579,6 +587,14 @@
<param name="lifestyle">The <see cref="T:LightweightIocContainer.Lifestyle"/> for this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/></param> <param name="lifestyle">The <see cref="T:LightweightIocContainer.Lifestyle"/> for this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/></param>
<returns>A new created <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/> with the given parameters</returns> <returns>A new created <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/> with the given parameters</returns>
</member> </member>
<member name="M:LightweightIocContainer.Registrations.RegistrationFactory.Register(System.Type,LightweightIocContainer.Lifestyle)">
<summary>
Register a <see cref="T:System.Type"/> without an interface and create a <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/>
</summary>
<param name="tImplementation">The <see cref="T:System.Type"/> to register</param>
<param name="lifestyle">The <see cref="T:LightweightIocContainer.Lifestyle"/> for this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/></param>
<returns>A new created <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/> with the given parameters</returns>
</member>
<member name="M:LightweightIocContainer.Registrations.RegistrationFactory.Register(System.Type,System.Type,System.Type)"> <member name="M:LightweightIocContainer.Registrations.RegistrationFactory.Register(System.Type,System.Type,System.Type)">
<summary> <summary>
Register an Interface with a Type that implements it as a multiton and create a <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1"/> Register an Interface with a Type that implements it as a multiton and create a <see cref="T:LightweightIocContainer.Interfaces.Registrations.IMultitonRegistration`1"/>

@ -3,6 +3,7 @@
// Copyright(c) 2019 SimonG. All Rights Reserved. // Copyright(c) 2019 SimonG. All Rights Reserved.
using System; using System;
using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Interfaces.Registrations;
@ -26,6 +27,20 @@ namespace LightweightIocContainer.Registrations
return new DefaultRegistration<TInterface>(typeof(TInterface), typeof(TImplementation), lifestyle); return new DefaultRegistration<TInterface>(typeof(TInterface), typeof(TImplementation), lifestyle);
} }
/// <summary>
/// Register a <see cref="Type"/> without an interface and create a <see cref="IDefaultRegistration{TInterface}"/>
/// </summary>
/// <typeparam name="TImplementation">The <see cref="Type"/> to register</typeparam>
/// <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 IDefaultRegistration<TImplementation> Register<TImplementation>(Lifestyle lifestyle = Lifestyle.Transient)
{
if (typeof(TImplementation).IsInterface)
throw new InvalidRegistrationException("Can't register an interface without its implementation type.");
return Register<TImplementation, TImplementation>(lifestyle);
}
/// <summary> /// <summary>
/// Register an Interface with a Type that implements it as a multiton and create a <see cref="IMultitonRegistration{TInterface}"/> /// Register an Interface with a Type that implements it as a multiton and create a <see cref="IMultitonRegistration{TInterface}"/>
/// </summary> /// </summary>
@ -51,6 +66,20 @@ namespace LightweightIocContainer.Registrations
return (IRegistrationBase)Activator.CreateInstance(defaultRegistrationType, tInterface, tImplementation, lifestyle); return (IRegistrationBase)Activator.CreateInstance(defaultRegistrationType, tInterface, tImplementation, lifestyle);
} }
/// <summary>
/// Register a <see cref="Type"/> without an interface and create a <see cref="IDefaultRegistration{TInterface}"/>
/// </summary>
/// <param name="tImplementation">The <see cref="Type"/> to register</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 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);
}
/// <summary> /// <summary>
/// Register an Interface with a Type that implements it as a multiton and create a <see cref="IMultitonRegistration{TInterface}"/> /// Register an Interface with a Type that implements it as a multiton and create a <see cref="IMultitonRegistration{TInterface}"/>
/// </summary> /// </summary>

@ -117,6 +117,7 @@ namespace Test.LightweightIocContainer
public void TestRegister() public void TestRegister()
{ {
Assert.DoesNotThrow(() => _iocContainer.Register(RegistrationFactory.Register(typeof(ITest), typeof(Test)))); 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))); Assert.DoesNotThrow(() => _iocContainer.Register(RegistrationFactory.RegisterFactory(typeof(ITestFactory), _iocContainer)));
} }
@ -128,6 +129,13 @@ namespace Test.LightweightIocContainer
Assert.AreEqual(typeof(ITest), exception.Type); Assert.AreEqual(typeof(ITest), exception.Type);
} }
[Test]
public void TestRegisterInterfaceWithoutImplementation()
{
Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Register(RegistrationFactory.Register<ITest>()));
Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Register(RegistrationFactory.Register(typeof(ITest))));
}
[Test] [Test]
public void TestRegisterFactoryWithoutCreate() public void TestRegisterFactoryWithoutCreate()
{ {
@ -157,6 +165,16 @@ namespace Test.LightweightIocContainer
Assert.IsInstanceOf<Test>(resolvedTest); Assert.IsInstanceOf<Test>(resolvedTest);
} }
[Test]
public void TestResolveWithoutInterface()
{
_iocContainer.Register(RegistrationFactory.Register<Test>());
Test resolvedTest = _iocContainer.Resolve<Test>();
Assert.IsInstanceOf<Test>(resolvedTest);
}
[Test] [Test]
public void TestResolveWithParams() public void TestResolveWithParams()
{ {
@ -363,6 +381,9 @@ namespace Test.LightweightIocContainer
_iocContainer.Register(RegistrationFactory.Register<ITest, Test>()); _iocContainer.Register(RegistrationFactory.Register<ITest, Test>());
Assert.True(_iocContainer.IsTypeRegistered<ITest>()); Assert.True(_iocContainer.IsTypeRegistered<ITest>());
_iocContainer.Register(RegistrationFactory.Register<Test>());
Assert.True(_iocContainer.IsTypeRegistered<Test>());
} }
} }
} }
Loading…
Cancel
Save