#49: allow resolve of implementation that is registered with an interface

pull/53/head
Simon G 4 years ago
parent 066aa949da
commit 9e957dac5a
  1. 10
      LightweightIocContainer/Interfaces/Registrations/IRegistrationBase.cs
  2. 12
      LightweightIocContainer/Interfaces/Registrations/ITypedRegistrationBase.cs
  3. 10
      LightweightIocContainer/IocContainer.cs
  4. 16
      LightweightIocContainer/LightweightIocContainer.xml
  5. 10
      Test.LightweightIocContainer/IocContainerTest.cs

@ -6,11 +6,19 @@ using LightweightIocContainer.Interfaces.Registrations.FluentProviders;
namespace LightweightIocContainer.Interfaces.Registrations namespace LightweightIocContainer.Interfaces.Registrations
{ {
/// <summary>
/// A base <see cref="IRegistrationBase"/> without generic interface
/// </summary>
public interface IRegistrationBase : IRegistration, ILifestyleProvider
{
}
/// <summary> /// <summary>
/// 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, ILifestyleProvider, IWithParameters<TInterface> public interface IRegistrationBase<TInterface> : IRegistrationBase, IWithParameters<TInterface>
{ {
} }

@ -8,9 +8,9 @@ using LightweightIocContainer.Interfaces.Registrations.FluentProviders;
namespace LightweightIocContainer.Interfaces.Registrations namespace LightweightIocContainer.Interfaces.Registrations
{ {
/// <summary> /// <summary>
/// A base <see cref="ITypedRegistrationBase{TInterface}"/> without implementation /// A base <see cref="ITypedRegistrationBase"/> without generic interface and implementation
/// </summary> /// </summary>
public interface ITypedRegistrationBase<TInterface> : IRegistrationBase<TInterface> public interface ITypedRegistrationBase : IRegistrationBase
{ {
/// <summary> /// <summary>
/// The <see cref="Type"/> that implements the <see cref="IRegistration.InterfaceType"/> that is registered with this <see cref="IRegistrationBase{TInterface}"/> /// The <see cref="Type"/> that implements the <see cref="IRegistration.InterfaceType"/> that is registered with this <see cref="IRegistrationBase{TInterface}"/>
@ -18,6 +18,14 @@ namespace LightweightIocContainer.Interfaces.Registrations
Type ImplementationType { get; } Type ImplementationType { get; }
} }
/// <summary>
/// A base <see cref="ITypedRegistrationBase{TInterface}"/> without generic implementation
/// </summary>
public interface ITypedRegistrationBase<TInterface> : IRegistrationBase<TInterface>, ITypedRegistrationBase
{
}
/// <summary> /// <summary>
/// A <see cref="IRegistrationBase{TInterface}"/> that implements a <see cref="Type"/> /// A <see cref="IRegistrationBase{TInterface}"/> that implements a <see cref="Type"/>
/// </summary> /// </summary>

@ -301,7 +301,7 @@ namespace LightweightIocContainer
T resolvedInstance; T resolvedInstance;
if (registration is IRegistrationBase<T> defaultRegistration) if (registration is IRegistrationBase defaultRegistration)
{ {
if (defaultRegistration.Lifestyle == Lifestyle.Singleton) if (defaultRegistration.Lifestyle == Lifestyle.Singleton)
resolvedInstance = GetOrCreateSingletonInstance<T>(defaultRegistration, arguments, resolveStack); resolvedInstance = GetOrCreateSingletonInstance<T>(defaultRegistration, arguments, resolveStack);
@ -413,11 +413,11 @@ namespace LightweightIocContainer
/// <returns>A newly created instance of the given <see cref="Type"/></returns> /// <returns>A newly created instance of the given <see cref="Type"/></returns>
private T CreateInstance<T>(IRegistration registration, object[] arguments, List<Type> resolveStack) private T CreateInstance<T>(IRegistration registration, object[] arguments, List<Type> resolveStack)
{ {
if (registration is IWithParameters<T> registrationWithParameters && registrationWithParameters.Parameters != null) if (registration is IWithParameters<T> { Parameters: { } } registrationWithParameters)
arguments = UpdateArgumentsWithRegistrationParameters(registrationWithParameters, arguments); arguments = UpdateArgumentsWithRegistrationParameters(registrationWithParameters, arguments);
T instance; T instance;
if (registration is ITypedRegistrationBase<T> defaultRegistration) if (registration is ITypedRegistrationBase defaultRegistration)
{ {
arguments = ResolveConstructorArguments(defaultRegistration.ImplementationType, arguments, resolveStack); arguments = ResolveConstructorArguments(defaultRegistration.ImplementationType, arguments, resolveStack);
instance = (T) Activator.CreateInstance(defaultRegistration.ImplementationType, arguments); instance = (T) Activator.CreateInstance(defaultRegistration.ImplementationType, arguments);
@ -588,6 +588,10 @@ namespace LightweightIocContainer
if (registration != null) if (registration != null)
return registration; return registration;
registration = _registrations.OfType<ITypedRegistrationBase>().FirstOrDefault(r => r.ImplementationType == typeof(T));
if (registration != null)
return registration;
//check for open generic registration //check for open generic registration
if (!typeof(T).GenericTypeArguments.Any()) if (!typeof(T).GenericTypeArguments.Any())
return null; return null;

@ -690,6 +690,11 @@
The <see cref="T:System.Type"/> of the Interface that is registered with this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/> The <see cref="T:System.Type"/> of the Interface that is registered with this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistration"/>
</summary> </summary>
</member> </member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase">
<summary>
A base <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase"/> without generic interface
</summary>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"> <member name="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1">
<summary> <summary>
The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> that is used to register an Interface The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> that is used to register an Interface
@ -725,16 +730,21 @@
The class that contains the implemented abstract factory of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.ITypedFactoryRegistration`1"/> The class that contains the implemented abstract factory of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.ITypedFactoryRegistration`1"/>
</summary> </summary>
</member> </member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase`1"> <member name="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase">
<summary> <summary>
A base <see cref="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase`1"/> without implementation A base <see cref="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase"/> without generic interface and implementation
</summary> </summary>
</member> </member>
<member name="P:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase`1.ImplementationType"> <member name="P:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase.ImplementationType">
<summary> <summary>
The <see cref="T:System.Type"/> that implements the <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistration.InterfaceType"/> that is registered with this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> The <see cref="T:System.Type"/> that implements the <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistration.InterfaceType"/> that is registered with this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/>
</summary> </summary>
</member> </member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase`1">
<summary>
A base <see cref="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase`1"/> without generic implementation
</summary>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase`2"> <member name="T:LightweightIocContainer.Interfaces.Registrations.ITypedRegistrationBase`2">
<summary> <summary>
A <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> that implements a <see cref="T:System.Type"/> A <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> that implements a <see cref="T:System.Type"/>

@ -212,6 +212,16 @@ namespace Test.LightweightIocContainer
Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Resolve<ITest>()); Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Resolve<ITest>());
} }
[Test]
public void TestResolveImplementationRegisteredWithInterface()
{
_iocContainer.Register<ITest, Test>();
Test resolvedTest = _iocContainer.Resolve<Test>();
Assert.IsInstanceOf<Test>(resolvedTest);
}
[Test] [Test]
public void TestResolveWithParams() public void TestResolveWithParams()
{ {

Loading…
Cancel
Save