Skip to content

Commit 59bd702

Browse files
committed
fixed: #406 - relaxing the checks and always register generic ServiceType as the generic type definition
1 parent 3b07908 commit 59bd702

9 files changed

+74
-20
lines changed

b.bat

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ echo:
1212
echo:
1313
echo:## Starting: TestRunner... ##
1414
echo:
15+
1516
dotnet run --no-build -c Release --project test/DryIoc.TestRunner/DryIoc.TestRunner.csproj
17+
1618
if %ERRORLEVEL% neq 0 goto :error
1719
echo:## Finished: TestRunner ##
1820

1921
echo:
2022
echo:## Starting: TESTS...
2123
echo:
2224

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

2527
if %ERRORLEVEL% neq 0 goto :error
2628

build.bat

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ echo:## Finished: BUILD and PACKAGING ##
3333
echo:
3434
echo:## Starting: TestRunner... ##
3535
echo:
36+
3637
dotnet run --no-build -c Release --project test/DryIoc.TestRunner/DryIoc.TestRunner.csproj
38+
3739
if %ERRORLEVEL% neq 0 goto :error
3840
echo:## Finished: TestRunner ##
3941

4042
echo:
4143
echo:## Running: TESTS... ##
4244
echo:
43-
dotnet test -c Release -p:GeneratePackageOnBuild=false
45+
46+
dotnet test --no-build -c Release -p:DevMode=false
47+
4448
if %ERRORLEVEL% neq 0 goto :error
4549
echo:## Finished: TESTS ##
4650

build_no_legacy.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ echo:
1111
echo:## Starting: TESTS...
1212
echo:
1313

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

1616
if %ERRORLEVEL% neq 0 goto :error
1717

src/DryIoc/Container.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ public void Register(Factory factory, Type serviceType, object serviceKey, IfAlr
270270

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

11116-
else if (implType.IsGeneric() && serviceType.IsOpenGeneric())
11117-
Throw.It(Error.RegisteringNotAGenericTypedefServiceType,
11118-
serviceType, serviceType.GetGenericTypeDefinition());
11119-
1112011119
else if (!serviceType.IsGeneric())
1112111120
Throw.It(Error.RegisteringOpenGenericImplWithNonGenericService, implType, serviceType);
1112211121

@@ -14992,3 +14991,13 @@ public Task Send<M>(M message, CancellationToken cancellationToken) where M : IM
1499214991
}
1499314992
}
1499414993
#endif
14994+
14995+
namespace DryIoc
14996+
{
14997+
/// <summary>The testing utility</summary>
14998+
public interface ITest
14999+
{
15000+
/// <summary>Runs the tests and should return the number of run tests</summary>
15001+
int Run();
15002+
}
15003+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using NUnit.Framework;
2+
3+
namespace DryIoc.IssuesTests
4+
{
5+
[TestFixture]
6+
public class GHIssue406_Allow_the_registration_of_the_partially_closed_implementation_type : ITest
7+
{
8+
public int Run()
9+
{
10+
Test1();
11+
return 1;
12+
}
13+
14+
[Test]
15+
public void Test1()
16+
{
17+
var container = new Container();
18+
19+
container.Register(
20+
typeof(ISuperWrapper<,>).MakeGenericType(typeof(IWrappedType<>), typeof(IResult)),
21+
typeof(SuperWrapper<>));
22+
23+
var x = container.Resolve<ISuperWrapper<IWrappedType<string>, IResult>>();
24+
25+
Assert.IsInstanceOf<SuperWrapper<string>>(x);
26+
}
27+
28+
interface IResult {}
29+
interface IWrappedType<T> {}
30+
interface ISuperWrapper<T, R> {}
31+
class SuperWrapper<X> : ISuperWrapper<IWrappedType<X>, IResult> {}
32+
}
33+
}

test/DryIoc.IssuesTests/TestTools.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/DryIoc.TestRunner/DryIoc.TestRunner.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<ProjectReference Include="..\DryIoc.UnitTests\DryIoc.UnitTests.csproj" />
1011
<ProjectReference Include="..\DryIoc.IssuesTests\DryIoc.IssuesTests.csproj" />
1112
</ItemGroup>
1213

test/DryIoc.TestRunner/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Diagnostics;
3-
using System.Threading.Tasks;
43
using DryIoc.IssuesTests;
54

65
namespace DryIoc.UnitTests
@@ -11,10 +10,12 @@ public static void Main()
1110
{
1211
RunAllTests();
1312

13+
// new OpenGenericsTests().Run();
1414
// new GHIssue391_Deadlock_during_Resolve().Run();
1515
// new GHIssue399_Func_dependency_on_Singleton_resolved_under_scope_breaks_after_disposing_scope_when_WithFuncAndLazyWithoutRegistration().Run();
1616
// new GHIssue380_ExportFactory_throws_Container_disposed_exception().Run();
1717
// new GHIssue402_Inconsistent_transient_disposable_behavior_when_using_Made().Run();
18+
// new GHIssue406_Allow_the_registration_of_the_partially_closed_implementation_type().Run();
1819
}
1920

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

46-
var tests = new ITest[] {
47+
var tests = new ITest[]
48+
{
49+
new OpenGenericsTests(),
4750
new GHIssue378_InconsistentResolutionFailure(),
4851
new GHIssue380_ExportFactory_throws_Container_disposed_exception(),
4952
new GHIssue391_Deadlock_during_Resolve(),
5053
new GHIssue399_Func_dependency_on_Singleton_resolved_under_scope_breaks_after_disposing_scope_when_WithFuncAndLazyWithoutRegistration(),
5154
new GHIssue402_Inconsistent_transient_disposable_behavior_when_using_Made(),
55+
new GHIssue406_Allow_the_registration_of_the_partially_closed_implementation_type(),
5256
};
5357

5458
// Parallel.ForEach(tests, x => Run(x.Run)); // todo: @perf enable and test when more tests are added

test/DryIoc.UnitTests/OpenGenericsTests.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
namespace DryIoc.UnitTests
77
{
88
[TestFixture]
9-
public class OpenGenericsTests
9+
public class OpenGenericsTests : ITest
1010
{
11+
public int Run()
12+
{
13+
Registering_technically_non_generic_type_definition_service_type_but_compatible_with_implementation_should_work();
14+
return 1;
15+
}
16+
1117
[Test]
1218
public void Resolving_non_registered_generic_should_throw()
1319
{
@@ -304,12 +310,14 @@ public void Registering_generic_but_not_closed_implementation_should_Throw()
304310
}
305311

306312
[Test]
307-
public void Registering_generic_but_not_closed_service_should_Throw()
313+
public void Registering_technically_non_generic_type_definition_service_type_but_compatible_with_implementation_should_work()
308314
{
309315
var container = new Container();
310316

311-
Assert.Throws<ContainerException>(() =>
312-
container.Register(typeof(Closed<>).GetBaseType(), typeof(Closed<>)));
317+
container.Register(typeof(Closed<>).GetBaseType(), typeof(Closed<>));
318+
319+
var x = container.Resolve<Open<string>>();
320+
Assert.IsInstanceOf<Closed<string>>(x);
313321
}
314322

315323
[Test]

0 commit comments

Comments
 (0)