Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/dadhi/DryIoc
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Sep 20, 2021
2 parents 7752bad + 4019e20 commit fbee44a
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions test/DryIoc.IssuesTests/GHIssue418_ResolveCovariantBaseType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using NUnit.Framework;
using System.Collections.Generic;

namespace DryIoc.IssuesTests
{
[TestFixture]
public class GHIssue418_ResolveCovariantBaseType
{
/// <summary>
/// Currently, resolving the list as <see cref="IReadOnlyList{BaseClass}" /> fails in this test.
/// The list is registered as its implementation and mapped to the interface.
/// </summary>
[Test, Ignore("fixme")]
public void RegisterCovariantImplementationTypeWithMapping()
{
var container = new Container();
// Register the implementation as itself
container.Register(Made.Of(() => new List<SubClass>()));
// Register the interface with base type parameter
container.RegisterMapping<IReadOnlyList<BaseClass>, List<SubClass>>();

// Resolving IReadOnlyList<BaseClass> crashes here
var baseClassList = container.Resolve<IReadOnlyList<BaseClass>>();
Assert.IsInstanceOf<List<SubClass>>(baseClassList);
}

/// <summary>
/// This works. The difference is that the list is registered as <see cref="IReadOnlyList{SubClass}"/> and
/// then it is mapped.
/// </summary>
[Test]
public void RegisterCovariantInterfaceTypeWithMapping()
{
var container = new Container();
container.Register(Made.Of<IReadOnlyList<SubClass>>(() => new List<SubClass>()));
container.RegisterMapping<IReadOnlyList<BaseClass>, IReadOnlyList<SubClass>>();

var baseClassList = container.Resolve<IReadOnlyList<BaseClass>>();
Assert.IsInstanceOf<List<SubClass>>(baseClassList);
}

/// <summary>
/// This also works, because there is no mapping here.
/// </summary>
[Test]
public void RegisterCovariantListTypeDirect()
{
var container = new Container();
container.Register<IReadOnlyList<BaseClass>, List<SubClass>>(Made.Of(() => new List<SubClass>()));

var baseClassList = container.Resolve<IReadOnlyList<BaseClass>>();
Assert.IsInstanceOf<List<SubClass>>(baseClassList);
}

/// <summary>
/// This works, but the registration code is weird, because we assign a closed generic implementation to an open
/// generic service.
/// </summary>
[Test]
public void RegisterClosedGenericImplementationAsOpenGeneric()
{
var container = new Container();
container.Register(typeof(IReadOnlyList<>), typeof(List<SubClass>),
made: Made.Of(FactoryMethod.ConstructorWithResolvableArguments));

var baseClassList = container.Resolve<IReadOnlyList<BaseClass>>();
Assert.IsInstanceOf<List<SubClass>>(baseClassList);
}

/// <summary>
/// This is a workaround by using a delegate instead of a mapping.
/// </summary>
[Test]
public void RegisterCovariantImplementationTypeWithDelegete()
{
var container = new Container();
container.Register(Made.Of(() => new List<SubClass>()));
container.RegisterDelegate<IReadOnlyList<BaseClass>>(c => c.Resolve<List<SubClass>>());

var baseClassList = container.Resolve<IReadOnlyList<BaseClass>>();
Assert.IsInstanceOf<List<SubClass>>(baseClassList);
}

private class BaseClass
{
}

private class SubClass : BaseClass
{
}
}
}

0 comments on commit fbee44a

Please sign in to comment.