Skip to content

Commit 6aa1974

Browse files
committed
using the MEF to mitigate the #574
1 parent 97ccab8 commit 6aa1974

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

test/DryIoc.IssuesTests/GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key.cs

+45-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44
using NUnit.Framework;
55
using Microsoft.Extensions.DependencyInjection;
66
using DryIoc.Microsoft.DependencyInjection;
7+
using DryIoc.MefAttributedModel;
8+
using System.ComponentModel.Composition;
79

810
namespace DryIoc.IssuesTests
911
{
10-
public class GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key
12+
// [TestFixture]
13+
public class GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key : ITest
1114
{
15+
public int Run()
16+
{
17+
// ResolveEnumerableFromChild();
18+
ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys();
19+
return 2;
20+
}
21+
1222
[Ignore("fixme")]
1323
public void ResolveEnumerableFromChild()
1424
{
@@ -23,7 +33,6 @@ public void ResolveEnumerableFromChild()
2333
var rootContainer = spf.CreateBuilder(new ServiceCollection());
2434
var childContainer = rootContainer.CreateChild(RegistrySharing.Share, "child-stamp", IfAlreadyRegistered.AppendNewImplementation);
2535

26-
//childContainer.Populate(services);
2736
foreach (var service in services)
2837
{
2938
childContainer.RegisterDescriptor(service, IfAlreadyRegistered.AppendNewImplementation, "child-stamp");
@@ -40,14 +49,48 @@ public void ResolveEnumerableFromChild()
4049
Is.EqualTo(4));
4150
}
4251

52+
// [Test]
53+
public void ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys()
54+
{
55+
var container = new Container(Rules.MicrosoftDependencyInjectionRules)
56+
.WithMef(); // <-- this is the key, LOL ;-)
57+
58+
var spf = new DryIocServiceProviderFactory(container);
59+
var rootContainer = spf.CreateBuilder(new ServiceCollection());
60+
var childContainer = rootContainer
61+
.CreateChild(RegistrySharing.Share, "child-stamp", IfAlreadyRegistered.AppendNewImplementation);
62+
63+
// here use RegisterExport instead of the RegisterDescriptor
64+
childContainer.RegisterExports(
65+
typeof(Printer),
66+
typeof(PrinterA),
67+
typeof(PrinterB),
68+
typeof(NeighborPrinter)
69+
);
70+
71+
var msContainer = childContainer.GetServiceProvider();
72+
73+
Assert.That(
74+
childContainer.Resolve<IEnumerable<IPrinter>>().Count(),
75+
Is.EqualTo(msContainer.GetRequiredService<IEnumerable<IPrinter>>().Count()));
76+
77+
Assert.That(
78+
msContainer.GetRequiredService<IEnumerable<IPrinter>>().Count(),
79+
Is.EqualTo(4));
80+
}
81+
4382
private interface IPrinter { }
4483

84+
[Export("child-stamp", typeof(IPrinter))]
4585
private class Printer : IPrinter { }
4686

87+
[Export("child-stamp", typeof(IPrinter))]
4788
private class PrinterA : IPrinter { }
4889

90+
[Export("child-stamp", typeof(IPrinter))]
4991
private class PrinterB : IPrinter { }
5092

93+
[Export("child-stamp", typeof(IPrinter))]
5194
private class NeighborPrinter : IPrinter { }
5295
}
5396
}

test/DryIoc.TestRunner/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static void Main()
1010
{
1111
RunAllTests();
1212

13+
// new GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key().Run();
1314
// new GHIssue576_Extension_methods_not_being_handled_correctly_in_MadeOf_service_returning_expression().Run();
1415
// new GHIssue116_ReOpened_DryIoc_Resolve_with_decorators_goes_wrong_for_parallel_execution().Run();
1516
// new RequiredPropertiesTests().Run();

0 commit comments

Comments
 (0)