Skip to content

Commit a69b396

Browse files
committed
verify multi-kley behavior for #574
1 parent 6aa1974 commit a69b396

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

test/DryIoc.IssuesTests/GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key.cs

+39-8
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99

1010
namespace DryIoc.IssuesTests
1111
{
12-
// [TestFixture]
12+
[TestFixture]
1313
public class GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key : ITest
1414
{
1515
public int Run()
1616
{
1717
// ResolveEnumerableFromChild();
18-
ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys();
19-
return 2;
18+
// ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys();
19+
ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys_2();
20+
return 1;
2021
}
2122

22-
[Ignore("fixme")]
23+
//[Ignore("fixme")]
2324
public void ResolveEnumerableFromChild()
2425
{
2526
var services = new ServiceCollection();
@@ -79,18 +80,48 @@ public void ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServic
7980
Is.EqualTo(4));
8081
}
8182

83+
[Test]
84+
public void ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys_2()
85+
{
86+
var container = new Container(Rules.MicrosoftDependencyInjectionRules)
87+
.WithMef(); // <-- this is the key, LOL ;-)
88+
89+
// here use RegisterExport instead of the RegisterDescriptor
90+
container.RegisterExports(
91+
typeof(Printer),
92+
typeof(PrinterA),
93+
typeof(PrinterB),
94+
typeof(NeighborPrinter)
95+
);
96+
97+
// all printers with and without the name, this is the default behavior of the Collection wrapper
98+
var ps = container.Resolve<IPrinter[]>();
99+
CollectionAssert.AreEquivalent(
100+
new[] { typeof(Printer), typeof(PrinterA), typeof(PrinterB),
101+
typeof(NeighborPrinter) },
102+
ps.Select(p => p.GetType()));
103+
104+
// only printers with the specific StampName
105+
var psStamped = container.Resolve<IPrinter[]>(serviceKey: StampName);
106+
CollectionAssert.AreEquivalent(
107+
new[] { typeof(Printer), typeof(PrinterA), typeof(PrinterB) },
108+
psStamped.Select(p => p.GetType()));
109+
}
110+
111+
private const string StampName = "child-stamp";
112+
82113
private interface IPrinter { }
83114

84-
[Export("child-stamp", typeof(IPrinter))]
115+
[Export(StampName, typeof(IPrinter))]
85116
private class Printer : IPrinter { }
86117

87-
[Export("child-stamp", typeof(IPrinter))]
118+
[Export(StampName, typeof(IPrinter))]
88119
private class PrinterA : IPrinter { }
89120

90-
[Export("child-stamp", typeof(IPrinter))]
121+
[Export(StampName, typeof(IPrinter))]
91122
private class PrinterB : IPrinter { }
92123

93-
[Export("child-stamp", typeof(IPrinter))]
124+
[Export(typeof(IPrinter))] // No name
94125
private class NeighborPrinter : IPrinter { }
95126
}
96127
}

0 commit comments

Comments
 (0)