@@ -12,9 +12,19 @@ namespace Simple.Data
1212
1313 class MefHelper : Composer
1414 {
15+ private static readonly Assembly ThisAssembly = typeof ( MefHelper ) . Assembly ;
16+
1517 public override T Compose < T > ( )
1618 {
17- using ( var container = CreateContainer ( ) )
19+ using ( var container = CreateAppDomainContainer ( ) )
20+ {
21+ var exports = container . GetExports < T > ( ) . ToList ( ) ;
22+ if ( exports . Count == 1 )
23+ {
24+ return exports . Single ( ) . Value ;
25+ }
26+ }
27+ using ( var container = CreateFolderContainer ( ) )
1828 {
1929 var exports = container . GetExports < T > ( ) . ToList ( ) ;
2030 if ( exports . Count == 0 ) throw new SimpleDataException ( "No ADO Provider found." ) ;
@@ -27,7 +37,15 @@ public override T Compose<T>(string contractName)
2737 {
2838 try
2939 {
30- using ( var container = CreateContainer ( ) )
40+ using ( var container = CreateAppDomainContainer ( ) )
41+ {
42+ var exports = container . GetExports < T > ( contractName ) . ToList ( ) ;
43+ if ( exports . Count == 1 )
44+ {
45+ return exports . Single ( ) . Value ;
46+ }
47+ }
48+ using ( var container = CreateFolderContainer ( ) )
3149 {
3250 var exports = container . GetExports < T > ( contractName ) . ToList ( ) ;
3351 if ( exports . Count == 0 ) throw new SimpleDataException ( "No ADO Provider found." ) ;
@@ -55,7 +73,7 @@ public static T GetAdjacentComponent<T>(Type knownSiblingType)
5573
5674 static string GetThisAssemblyPath ( )
5775 {
58- var path = Assembly . GetExecutingAssembly ( ) . CodeBase . Replace ( "file:///" , "" ) . Replace ( "file://" , "//" ) ;
76+ var path = ThisAssembly . CodeBase . Replace ( "file:///" , "" ) . Replace ( "file://" , "//" ) ;
5977 path = Path . GetDirectoryName ( path ) ;
6078 if ( path == null ) throw new ArgumentException ( "Unrecognised file." ) ;
6179 if ( ! Path . IsPathRooted ( path ) )
@@ -65,11 +83,11 @@ static string GetThisAssemblyPath()
6583 return path ;
6684 }
6785
68- private static CompositionContainer CreateContainer ( )
86+ private static CompositionContainer CreateFolderContainer ( )
6987 {
7088 var path = GetThisAssemblyPath ( ) ;
7189
72- var assemblyCatalog = new AssemblyCatalog ( Assembly . GetExecutingAssembly ( ) ) ;
90+ var assemblyCatalog = new AssemblyCatalog ( ThisAssembly ) ;
7391 var aggregateCatalog = new AggregateCatalog ( assemblyCatalog ) ;
7492 foreach ( string file in System . IO . Directory . GetFiles ( path , "Simple.Data.*.dll" ) )
7593 {
@@ -78,5 +96,15 @@ private static CompositionContainer CreateContainer()
7896 }
7997 return new CompositionContainer ( aggregateCatalog ) ;
8098 }
99+
100+ private static CompositionContainer CreateAppDomainContainer ( )
101+ {
102+ var aggregateCatalog = new AggregateCatalog ( ) ;
103+ foreach ( var assembly in AppDomain . CurrentDomain . GetAssemblies ( ) . Where ( a => ! a . GlobalAssemblyCache ) )
104+ {
105+ aggregateCatalog . Catalogs . Add ( new AssemblyCatalog ( assembly ) ) ;
106+ }
107+ return new CompositionContainer ( aggregateCatalog ) ;
108+ }
81109 }
82110}
0 commit comments