55
66namespace Shuttle . Core . Data
77{
8- public class DatabaseContextFactory : IDatabaseContextFactory , IConfiguredDatabaseContextFactory
9- {
10- private readonly IDbCommandFactory _dbCommandFactory ;
11- private readonly IDatabaseContextCache _databaseContextCache ;
12- private readonly IDbConnectionFactory _dbConnectionFactory ;
13-
14- private string _connectionStringName = null ;
15- private string _providerName = null ;
16- private string _connectionString = null ;
17- private IDbConnection _dbConnection = null ;
18-
19- public DatabaseContextFactory ( IDbConnectionFactory dbConnectionFactory , IDbCommandFactory dbCommandFactory , IDatabaseContextCache databaseContextCache )
20- {
21- Guard . AgainstNull ( dbConnectionFactory , "dbConnectionFactory" ) ;
22- Guard . AgainstNull ( dbCommandFactory , "dbCommandFactory" ) ;
23- Guard . AgainstNull ( databaseContextCache , "databaseContextCache" ) ;
24-
25- _dbConnectionFactory = dbConnectionFactory ;
26- _dbCommandFactory = dbCommandFactory ;
27- _databaseContextCache = databaseContextCache ;
28-
29- DatabaseContext . Assign ( databaseContextCache ) ;
30- }
31-
32- public static IDatabaseContextFactory Default ( )
33- {
34- return new DatabaseContextFactory ( new DbConnectionFactory ( ) , new DbCommandFactory ( ) ,
35- new ThreadStaticDatabaseContextCache ( ) ) ;
36- }
37-
38- public IDatabaseContext Create ( string connectionStringName )
39- {
40- var settings = ConfigurationManager . ConnectionStrings [ connectionStringName ] ;
41-
42- if ( settings == null )
43- {
44- throw new InvalidOperationException ( string . Format ( DataResources . ConnectionStringMissing , connectionStringName ) ) ;
45- }
46-
47- return Create ( settings . ProviderName , settings . ConnectionString ) ;
48- }
49-
50- public IDatabaseContext Create ( string providerName , string connectionString )
51- {
52- return _databaseContextCache . Contains ( connectionString )
53- ? _databaseContextCache . Get ( connectionString ) . Suppressed ( )
54- : new DatabaseContext ( _dbConnectionFactory . CreateConnection ( providerName , connectionString ) , _dbCommandFactory ) ;
55- }
56-
57- public IDatabaseContext Create ( IDbConnection dbConnection )
58- {
59- Guard . AgainstNull ( dbConnection , "dbConnection" ) ;
60-
61- return _databaseContextCache . Contains ( dbConnection . ConnectionString )
62- ? _databaseContextCache . Get ( dbConnection . ConnectionString ) . Suppressed ( )
63- : new DatabaseContext ( dbConnection , _dbCommandFactory ) ;
64- }
65-
66- public IDatabaseContext Create ( )
67- {
68- if ( ! string . IsNullOrEmpty ( _connectionStringName ) )
69- {
70- return Create ( _connectionStringName ) ;
71- }
72-
73- if ( ! string . IsNullOrEmpty ( _providerName ) && ! string . IsNullOrEmpty ( _connectionString ) )
74- {
75- return Create ( _providerName , _connectionString ) ;
76- }
77-
78- if ( _dbConnection != null )
79- {
80- return Create ( _dbConnection ) ;
81- }
82-
83- throw new InvalidOperationException ( string . Format ( DataResources . DatabaseContextFactoryNotConfiguredException , GetType ( ) . FullName ) ) ;
84- }
85-
86- public void ConfigureWith ( string connectionStringName )
87- {
88- ClearConfiguredValues ( ) ;
89-
90- _connectionStringName = connectionStringName ;
91- }
92-
93- private void ClearConfiguredValues ( )
94- {
95- _connectionStringName = null ;
96- _providerName = null ;
97- _connectionString = null ;
98- _dbConnection = null ;
99- }
100-
101- public void ConfigureWith ( string providerName , string connectionString )
102- {
103- ClearConfiguredValues ( ) ;
104-
105- _providerName = providerName ;
106- _connectionString = connectionString ;
107- }
108-
109- public void ConfigureWith ( IDbConnection dbConnection )
110- {
111- ClearConfiguredValues ( ) ;
112-
113- _dbConnection = dbConnection ;
114- }
115- }
8+ public class DatabaseContextFactory : IConfiguredDatabaseContextFactory
9+ {
10+ private string _connectionString ;
11+ private string _connectionStringName ;
12+ private IDbConnection _dbConnection ;
13+ private string _providerName ;
14+
15+ public DatabaseContextFactory ( IDbConnectionFactory dbConnectionFactory , IDbCommandFactory dbCommandFactory ,
16+ IDatabaseContextCache databaseContextCache )
17+ {
18+ Guard . AgainstNull ( dbConnectionFactory , "dbConnectionFactory" ) ;
19+ Guard . AgainstNull ( dbCommandFactory , "dbCommandFactory" ) ;
20+ Guard . AgainstNull ( databaseContextCache , "databaseContextCache" ) ;
21+
22+ DbConnectionFactory = dbConnectionFactory ;
23+ DbCommandFactory = dbCommandFactory ;
24+ DatabaseContextCache = databaseContextCache ;
25+
26+ DatabaseContext . Assign ( databaseContextCache ) ;
27+ }
28+
29+ public IDatabaseContext Create ( string connectionStringName )
30+ {
31+ var settings = ConfigurationManager . ConnectionStrings [ connectionStringName ] ;
32+
33+ if ( settings == null )
34+ {
35+ throw new InvalidOperationException ( string . Format ( DataResources . ConnectionStringMissing ,
36+ connectionStringName ) ) ;
37+ }
38+
39+ return Create ( settings . ProviderName , settings . ConnectionString ) ;
40+ }
41+
42+ public IDatabaseContext Create ( string providerName , string connectionString )
43+ {
44+ return DatabaseContextCache . Contains ( connectionString )
45+ ? DatabaseContextCache . Get ( connectionString ) . Suppressed ( )
46+ : new DatabaseContext ( DbConnectionFactory . CreateConnection ( providerName , connectionString ) ,
47+ DbCommandFactory ) ;
48+ }
49+
50+ public IDatabaseContext Create ( IDbConnection dbConnection )
51+ {
52+ Guard . AgainstNull ( dbConnection , "dbConnection" ) ;
53+
54+ return DatabaseContextCache . Contains ( dbConnection . ConnectionString )
55+ ? DatabaseContextCache . Get ( dbConnection . ConnectionString ) . Suppressed ( )
56+ : new DatabaseContext ( dbConnection , DbCommandFactory ) ;
57+ }
58+
59+ public IDbConnectionFactory DbConnectionFactory { get ; private set ; }
60+ public IDbCommandFactory DbCommandFactory { get ; private set ; }
61+ public IDatabaseContextCache DatabaseContextCache { get ; private set ; }
62+
63+ public IDatabaseContext Create ( )
64+ {
65+ if ( ! string . IsNullOrEmpty ( _connectionStringName ) )
66+ {
67+ return Create ( _connectionStringName ) ;
68+ }
69+
70+ if ( ! string . IsNullOrEmpty ( _providerName ) && ! string . IsNullOrEmpty ( _connectionString ) )
71+ {
72+ return Create ( _providerName , _connectionString ) ;
73+ }
74+
75+ if ( _dbConnection != null )
76+ {
77+ return Create ( _dbConnection ) ;
78+ }
79+
80+ throw new InvalidOperationException ( string . Format (
81+ DataResources . DatabaseContextFactoryNotConfiguredException , GetType ( ) . FullName ) ) ;
82+ }
83+
84+ public void ConfigureWith ( string connectionStringName )
85+ {
86+ ClearConfiguredValues ( ) ;
87+
88+ _connectionStringName = connectionStringName ;
89+ }
90+
91+ public void ConfigureWith ( string providerName , string connectionString )
92+ {
93+ ClearConfiguredValues ( ) ;
94+
95+ _providerName = providerName ;
96+ _connectionString = connectionString ;
97+ }
98+
99+ public void ConfigureWith ( IDbConnection dbConnection )
100+ {
101+ ClearConfiguredValues ( ) ;
102+
103+ _dbConnection = dbConnection ;
104+ }
105+
106+ public static IDatabaseContextFactory Default ( )
107+ {
108+ return new DatabaseContextFactory ( new DbConnectionFactory ( ) , new DbCommandFactory ( ) ,
109+ new ThreadStaticDatabaseContextCache ( ) ) ;
110+ }
111+
112+ private void ClearConfiguredValues ( )
113+ {
114+ _connectionStringName = null ;
115+ _providerName = null ;
116+ _connectionString = null ;
117+ _dbConnection = null ;
118+ }
119+ }
116120}
0 commit comments