@@ -7,27 +7,35 @@ public class DatabaseContextCache : IDatabaseContextCache
77 {
88 public IDatabaseContext Current { get ; private set ; }
99
10- public void Use ( string name )
10+ public ActiveDatabaseContext Use ( string name )
1111 {
12- Current = DatabaseContexts . Find ( candidate => candidate . Name . Equals ( name , StringComparison . OrdinalIgnoreCase ) ) ;
12+ var current = Current ;
13+
14+ Current = DatabaseContexts . Find ( candidate => candidate . Name . Equals ( name , StringComparison . OrdinalIgnoreCase ) ) ;
1315
1416 if ( Current == null )
1517 {
1618 throw new Exception ( string . Format ( Resources . DatabaseContextNameNotFoundException , name ) ) ;
1719 }
20+
21+ return new ActiveDatabaseContext ( this , current ) ;
1822 }
1923
20- public void Use ( IDatabaseContext context )
24+ public ActiveDatabaseContext Use ( IDatabaseContext context )
2125 {
2226 Guard . AgainstNull ( context , nameof ( context ) ) ;
2327
28+ var current = Current ;
29+
2430 Current = DatabaseContexts . Find ( candidate => candidate . Key . Equals ( context . Key ) ) ;
2531
2632 if ( Current == null )
2733 {
2834 throw new Exception ( string . Format ( Resources . DatabaseContextKeyNotFoundException , context . Key ) ) ;
2935 }
30- }
36+
37+ return new ActiveDatabaseContext ( this , current ) ;
38+ }
3139
3240 public bool Contains ( string connectionString )
3341 {
@@ -42,9 +50,10 @@ public void Add(IDatabaseContext context)
4250 }
4351
4452 DatabaseContexts . Add ( context ) ;
53+ Use ( context ) ;
4554 }
4655
47- private IDatabaseContext Find ( IDatabaseContext context )
56+ private IDatabaseContext Find ( IDatabaseContext context )
4857 {
4958 return DatabaseContexts . Find ( candidate => candidate . Key . Equals ( context . Key ) ) ;
5059 }
@@ -78,7 +87,7 @@ public IDatabaseContext Get(string connectionString)
7887 return result ;
7988 }
8089
81- public DatabaseContextCollection DatabaseContexts { get ; private set ; }
90+ public DatabaseContextCollection DatabaseContexts { get ; }
8291
8392 public DatabaseContextCache ( )
8493 {
0 commit comments