|
9 | 9 |
|
10 | 10 | namespace DryIoc.IssuesTests
|
11 | 11 | {
|
12 |
| - // [TestFixture] |
| 12 | + [TestFixture] |
13 | 13 | public class GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key : ITest
|
14 | 14 | {
|
15 | 15 | public int Run()
|
16 | 16 | {
|
17 | 17 | // ResolveEnumerableFromChild();
|
18 |
| - ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys(); |
19 |
| - return 2; |
| 18 | + // ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys(); |
| 19 | + ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys_2(); |
| 20 | + return 1; |
20 | 21 | }
|
21 | 22 |
|
22 |
| - [Ignore("fixme")] |
| 23 | + //[Ignore("fixme")] |
23 | 24 | public void ResolveEnumerableFromChild()
|
24 | 25 | {
|
25 | 26 | var services = new ServiceCollection();
|
@@ -79,18 +80,48 @@ public void ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServic
|
79 | 80 | Is.EqualTo(4));
|
80 | 81 | }
|
81 | 82 |
|
| 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 | + |
82 | 113 | private interface IPrinter { }
|
83 | 114 |
|
84 |
| - [Export("child-stamp", typeof(IPrinter))] |
| 115 | + [Export(StampName, typeof(IPrinter))] |
85 | 116 | private class Printer : IPrinter { }
|
86 | 117 |
|
87 |
| - [Export("child-stamp", typeof(IPrinter))] |
| 118 | + [Export(StampName, typeof(IPrinter))] |
88 | 119 | private class PrinterA : IPrinter { }
|
89 | 120 |
|
90 |
| - [Export("child-stamp", typeof(IPrinter))] |
| 121 | + [Export(StampName, typeof(IPrinter))] |
91 | 122 | private class PrinterB : IPrinter { }
|
92 | 123 |
|
93 |
| - [Export("child-stamp", typeof(IPrinter))] |
| 124 | + [Export(typeof(IPrinter))] // No name |
94 | 125 | private class NeighborPrinter : IPrinter { }
|
95 | 126 | }
|
96 | 127 | }
|
0 commit comments