Skip to content

Commit

Permalink
fixed: #406 - relaxing the checks and always register generic Service…
Browse files Browse the repository at this point in the history
…Type as the generic type definition
  • Loading branch information
dadhi committed Jun 4, 2021
1 parent 3b07908 commit 59bd702
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 20 deletions.
4 changes: 3 additions & 1 deletion b.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ echo:
echo:
echo:## Starting: TestRunner... ##
echo:

dotnet run --no-build -c Release --project test/DryIoc.TestRunner/DryIoc.TestRunner.csproj

if %ERRORLEVEL% neq 0 goto :error
echo:## Finished: TestRunner ##

echo:
echo:## Starting: TESTS...
echo:

dotnet test -c:Release -p:GeneratePackageOnBuild=false;DevMode=false;NoLegacy=true
dotnet test --no-build -c:Release -p:DevMode=false;NoLegacy=true

if %ERRORLEVEL% neq 0 goto :error

Expand Down
6 changes: 5 additions & 1 deletion build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ echo:## Finished: BUILD and PACKAGING ##
echo:
echo:## Starting: TestRunner... ##
echo:

dotnet run --no-build -c Release --project test/DryIoc.TestRunner/DryIoc.TestRunner.csproj

if %ERRORLEVEL% neq 0 goto :error
echo:## Finished: TestRunner ##

echo:
echo:## Running: TESTS... ##
echo:
dotnet test -c Release -p:GeneratePackageOnBuild=false

dotnet test --no-build -c Release -p:DevMode=false

if %ERRORLEVEL% neq 0 goto :error
echo:## Finished: TESTS ##

Expand Down
2 changes: 1 addition & 1 deletion build_no_legacy.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ echo:
echo:## Starting: TESTS...
echo:

dotnet test -c:Release -p:GeneratePackageOnBuild=false;DevMode=false;NoLegacy=true
dotnet test --no-build -c:Release -p:DevMode=false;NoLegacy=true

if %ERRORLEVEL% neq 0 goto :error

Expand Down
17 changes: 13 additions & 4 deletions src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ public void Register(Factory factory, Type serviceType, object serviceKey, IfAlr

// Improves performance a bit by first attempting to swap the registry while it is still unchanged.
var r = _registry.Value;
var st = serviceType.GetTypeInfo();
if (st.IsGenericType && !st.IsGenericTypeDefinition && st.ContainsGenericParameters)
serviceType = serviceType.GetGenericTypeDefinition();
if (!_registry.TrySwapIfStillCurrent(r, r.Register(factory, serviceType, ifAlreadyRegistered.Value, serviceKey)))
RegistrySwap(factory, serviceType, serviceKey, ifAlreadyRegistered);
}
Expand Down Expand Up @@ -11113,10 +11116,6 @@ internal override bool ValidateAndNormalizeRegistration(Type serviceType, object
if (serviceType.IsGenericDefinition())
ThrowIfImplementationAndServiceTypeParamsDontMatch(implType, serviceType);

else if (implType.IsGeneric() && serviceType.IsOpenGeneric())
Throw.It(Error.RegisteringNotAGenericTypedefServiceType,
serviceType, serviceType.GetGenericTypeDefinition());

else if (!serviceType.IsGeneric())
Throw.It(Error.RegisteringOpenGenericImplWithNonGenericService, implType, serviceType);

Expand Down Expand Up @@ -14992,3 +14991,13 @@ public Task Send<M>(M message, CancellationToken cancellationToken) where M : IM
}
}
#endif

namespace DryIoc
{
/// <summary>The testing utility</summary>
public interface ITest
{
/// <summary>Runs the tests and should return the number of run tests</summary>
int Run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using NUnit.Framework;

namespace DryIoc.IssuesTests
{
[TestFixture]
public class GHIssue406_Allow_the_registration_of_the_partially_closed_implementation_type : ITest
{
public int Run()
{
Test1();
return 1;
}

[Test]
public void Test1()
{
var container = new Container();

container.Register(
typeof(ISuperWrapper<,>).MakeGenericType(typeof(IWrappedType<>), typeof(IResult)),
typeof(SuperWrapper<>));

var x = container.Resolve<ISuperWrapper<IWrappedType<string>, IResult>>();

Assert.IsInstanceOf<SuperWrapper<string>>(x);
}

interface IResult {}
interface IWrappedType<T> {}
interface ISuperWrapper<T, R> {}
class SuperWrapper<X> : ISuperWrapper<IWrappedType<X>, IResult> {}
}
}
7 changes: 0 additions & 7 deletions test/DryIoc.IssuesTests/TestTools.cs

This file was deleted.

1 change: 1 addition & 0 deletions test/DryIoc.TestRunner/DryIoc.TestRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DryIoc.UnitTests\DryIoc.UnitTests.csproj" />
<ProjectReference Include="..\DryIoc.IssuesTests\DryIoc.IssuesTests.csproj" />
</ItemGroup>

Expand Down
8 changes: 6 additions & 2 deletions test/DryIoc.TestRunner/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using DryIoc.IssuesTests;

namespace DryIoc.UnitTests
Expand All @@ -11,10 +10,12 @@ public static void Main()
{
RunAllTests();

// new OpenGenericsTests().Run();
// new GHIssue391_Deadlock_during_Resolve().Run();
// new GHIssue399_Func_dependency_on_Singleton_resolved_under_scope_breaks_after_disposing_scope_when_WithFuncAndLazyWithoutRegistration().Run();
// new GHIssue380_ExportFactory_throws_Container_disposed_exception().Run();
// new GHIssue402_Inconsistent_transient_disposable_behavior_when_using_Made().Run();
// new GHIssue406_Allow_the_registration_of_the_partially_closed_implementation_type().Run();
}

public static void RunAllTests()
Expand Down Expand Up @@ -43,12 +44,15 @@ void Run(Func<int> run, string name = null)
Console.WriteLine("NETCOREAPP2.1: Running UnitTests and IssueTests...");
Console.WriteLine();

var tests = new ITest[] {
var tests = new ITest[]
{
new OpenGenericsTests(),
new GHIssue378_InconsistentResolutionFailure(),
new GHIssue380_ExportFactory_throws_Container_disposed_exception(),
new GHIssue391_Deadlock_during_Resolve(),
new GHIssue399_Func_dependency_on_Singleton_resolved_under_scope_breaks_after_disposing_scope_when_WithFuncAndLazyWithoutRegistration(),
new GHIssue402_Inconsistent_transient_disposable_behavior_when_using_Made(),
new GHIssue406_Allow_the_registration_of_the_partially_closed_implementation_type(),
};

// Parallel.ForEach(tests, x => Run(x.Run)); // todo: @perf enable and test when more tests are added
Expand Down
16 changes: 12 additions & 4 deletions test/DryIoc.UnitTests/OpenGenericsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
namespace DryIoc.UnitTests
{
[TestFixture]
public class OpenGenericsTests
public class OpenGenericsTests : ITest
{
public int Run()
{
Registering_technically_non_generic_type_definition_service_type_but_compatible_with_implementation_should_work();
return 1;
}

[Test]
public void Resolving_non_registered_generic_should_throw()
{
Expand Down Expand Up @@ -304,12 +310,14 @@ public void Registering_generic_but_not_closed_implementation_should_Throw()
}

[Test]
public void Registering_generic_but_not_closed_service_should_Throw()
public void Registering_technically_non_generic_type_definition_service_type_but_compatible_with_implementation_should_work()
{
var container = new Container();

Assert.Throws<ContainerException>(() =>
container.Register(typeof(Closed<>).GetBaseType(), typeof(Closed<>)));
container.Register(typeof(Closed<>).GetBaseType(), typeof(Closed<>));

var x = container.Resolve<Open<string>>();
Assert.IsInstanceOf<Closed<string>>(x);
}

[Test]
Expand Down

0 comments on commit 59bd702

Please sign in to comment.