4
4
using NUnit . Framework ;
5
5
using Microsoft . Extensions . DependencyInjection ;
6
6
using DryIoc . Microsoft . DependencyInjection ;
7
+ using DryIoc . MefAttributedModel ;
8
+ using System . ComponentModel . Composition ;
7
9
8
10
namespace DryIoc . IssuesTests
9
11
{
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
11
14
{
15
+ public int Run ( )
16
+ {
17
+ // ResolveEnumerableFromChild();
18
+ ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys ( ) ;
19
+ return 2 ;
20
+ }
21
+
12
22
[ Ignore ( "fixme" ) ]
13
23
public void ResolveEnumerableFromChild ( )
14
24
{
@@ -23,7 +33,6 @@ public void ResolveEnumerableFromChild()
23
33
var rootContainer = spf . CreateBuilder ( new ServiceCollection ( ) ) ;
24
34
var childContainer = rootContainer . CreateChild ( RegistrySharing . Share , "child-stamp" , IfAlreadyRegistered . AppendNewImplementation ) ;
25
35
26
- //childContainer.Populate(services);
27
36
foreach ( var service in services )
28
37
{
29
38
childContainer . RegisterDescriptor ( service , IfAlreadyRegistered . AppendNewImplementation , "child-stamp" ) ;
@@ -40,14 +49,48 @@ public void ResolveEnumerableFromChild()
40
49
Is . EqualTo ( 4 ) ) ;
41
50
}
42
51
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
+
43
82
private interface IPrinter { }
44
83
84
+ [ Export ( "child-stamp" , typeof ( IPrinter ) ) ]
45
85
private class Printer : IPrinter { }
46
86
87
+ [ Export ( "child-stamp" , typeof ( IPrinter ) ) ]
47
88
private class PrinterA : IPrinter { }
48
89
90
+ [ Export ( "child-stamp" , typeof ( IPrinter ) ) ]
49
91
private class PrinterB : IPrinter { }
50
92
93
+ [ Export ( "child-stamp" , typeof ( IPrinter ) ) ]
51
94
private class NeighborPrinter : IPrinter { }
52
95
}
53
96
}
0 commit comments