|
7 | 7 |
|
8 | 8 | namespace ConnectionFactory |
9 | 9 | { |
10 | | - public class CfConnection : IDisposable |
11 | | - { |
12 | | - private readonly DbProviderFactory _dbProvider; |
13 | | - private DbConnection _conn; |
14 | | - private DbTransaction _tran; |
15 | | - private long _tranOpenCount; |
16 | | - |
17 | | - internal enum TransactionType |
18 | | - { |
19 | | - TransactionOpen = 1, |
20 | | - TransactionCommit = 2, |
21 | | - TransactionRollback = 3 |
22 | | - } |
23 | | - |
24 | | - [System.Diagnostics.DebuggerStepThrough] |
25 | | - public CfConnection(string connectionName) |
26 | | - { |
27 | | - if (string.IsNullOrWhiteSpace(connectionName)) |
28 | | - throw new ArgumentException("Obrigatorio informar o nome da conexão", "connectionName"); |
29 | | - |
30 | | - |
| 10 | + public class CfConnection : IDisposable |
| 11 | + { |
| 12 | + private readonly DbProviderFactory _dbProvider; |
| 13 | + private DbConnection _conn; |
| 14 | + private DbTransaction _tran; |
| 15 | + private long _tranOpenCount; |
| 16 | + |
| 17 | + internal enum TransactionType |
| 18 | + { |
| 19 | + TransactionOpen = 1, |
| 20 | + TransactionCommit = 2, |
| 21 | + TransactionRollback = 3 |
| 22 | + } |
| 23 | + |
| 24 | + [System.Diagnostics.DebuggerStepThrough] |
| 25 | + public CfConnection(string connectionName) |
| 26 | + { |
| 27 | + try |
| 28 | + { |
31 | 29 | Configuration = ConfigurationManager.ConnectionStrings[connectionName]; |
32 | 30 |
|
33 | 31 | if (Configuration == null) |
34 | | - throw new CfException(string.Format("Conexão {0} não definida nas configurações", connectionName)); |
35 | | - |
36 | | - try |
37 | 32 | { |
38 | | - _dbProvider = DbProviderFactories.GetFactory(Configuration.ProviderName); |
| 33 | + throw new CfException(String.Format("{0} connection not defined in the settings.", connectionName)); |
39 | 34 | } |
40 | | - catch (Exception ex) |
41 | | - { |
42 | | - throw new CfException(string.Format("Não foi possivel obter o provedor da conexão {0}", connectionName),ex); |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - [System.Diagnostics.DebuggerStepThrough] |
47 | | - public void Dispose() |
48 | | - { |
49 | | - CloseFactoryConnection(); |
50 | | - //_dbProvider = null; |
51 | | - if (_tran != null) _tran.Dispose(); |
52 | | - } |
53 | | - |
54 | | - [System.Diagnostics.DebuggerStepThrough] |
55 | | - internal void EstablishFactoryConnection() |
56 | | - { |
57 | | - try |
58 | | - { |
59 | | - if (null == _conn) |
60 | | - _conn = _dbProvider.CreateConnection(); |
61 | | - |
62 | | - if (_conn == null || !_conn.State.Equals(ConnectionState.Closed)) return; |
63 | 35 |
|
64 | | - _conn.ConnectionString = Configuration.ConnectionString; |
65 | | - |
66 | | - if (Transaction.Current == null) |
67 | | - { |
68 | | - _conn.Open(); |
69 | | - } |
70 | | - |
71 | | - _tranOpenCount = 0; |
72 | | - } |
73 | | - catch (DbException dbe) |
| 36 | + _dbProvider = DbProviderFactories.GetFactory(Configuration.ProviderName); |
| 37 | + } |
| 38 | + catch (CfException) |
| 39 | + { |
| 40 | + throw; |
| 41 | + } |
| 42 | + catch (ConfigurationException cex) |
| 43 | + { |
| 44 | + throw new CfException(String.Format("Error getting the provider to {0} connection. {1}", connectionName, cex.Message), cex); |
| 45 | + } |
| 46 | + catch (Exception ex) |
| 47 | + { |
| 48 | + throw new CfException(String.Format("We could not identify the provider for {0} connection.", connectionName), ex); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + [System.Diagnostics.DebuggerStepThrough] |
| 53 | + public void Dispose() |
| 54 | + { |
| 55 | + CloseFactoryConnection(); |
| 56 | + //_dbProvider = null; |
| 57 | + if (_tran != null) _tran.Dispose(); |
| 58 | + } |
| 59 | + |
| 60 | + [System.Diagnostics.DebuggerStepThrough] |
| 61 | + internal void EstablishFactoryConnection() |
| 62 | + { |
| 63 | + try |
| 64 | + { |
| 65 | + if (null == _conn) |
| 66 | + _conn = _dbProvider.CreateConnection(); |
| 67 | + |
| 68 | + if (_conn == null || !_conn.State.Equals(ConnectionState.Closed)) return; |
| 69 | + |
| 70 | + _conn.ConnectionString = Configuration.ConnectionString; |
| 71 | + |
| 72 | + if (Transaction.Current == null) |
74 | 73 | { |
75 | | - throw new CfException("Não foi possível se conectar ao banco de dados", dbe); |
| 74 | + _conn.Open(); |
76 | 75 | } |
77 | | - catch (Exception ex) |
78 | | - { |
79 | | - throw new CfException("Não foi possível se conectar ao banco de dados", ex); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - [System.Diagnostics.DebuggerStepThrough] |
84 | | - internal void CloseFactoryConnection() |
85 | | - { |
86 | | - if (_conn == null) return; |
87 | 76 |
|
88 | | - if (!_conn.State.Equals(ConnectionState.Closed)) |
89 | | - { |
90 | | - if (_tranOpenCount > 0) |
91 | | - { |
92 | | - TransactionHandler(TransactionType.TransactionRollback); |
93 | | - } |
94 | | - _conn.Close(); |
95 | | - } |
96 | 77 | _tranOpenCount = 0; |
97 | | - //_conn.Dispose(); |
98 | | - //_conn = null; |
99 | | - |
100 | | - } |
101 | | - |
102 | | - [System.Diagnostics.DebuggerStepThrough] |
103 | | - internal void TransactionHandler(TransactionType transactionType, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) |
104 | | - { |
105 | | - EstablishFactoryConnection(); |
106 | | - |
107 | | - switch (transactionType) |
108 | | - { |
109 | | - case TransactionType.TransactionOpen: |
110 | | - _tran = _conn.BeginTransaction(isolationLevel); |
111 | | - _tranOpenCount++; |
112 | | - break; |
113 | | - case TransactionType.TransactionCommit: |
114 | | - _tran.Commit(); |
115 | | - _tranOpenCount--; |
116 | | - break; |
117 | | - case TransactionType.TransactionRollback: |
118 | | - _tran.Rollback(); |
119 | | - _tranOpenCount--; |
120 | | - break; |
121 | | - default: |
122 | | - throw new ArgumentOutOfRangeException("transactionType"); |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - public ConnectionStringSettings Configuration { get; private set; } |
127 | | - |
128 | | - public ConnectionState State |
129 | | - { |
130 | | - get { return _conn == null ? ConnectionState.Closed : _conn.State; } |
131 | | - } |
132 | | - |
133 | | - internal DbCommand CreateDbCommand() |
134 | | - { |
135 | | - var cmd = _conn.CreateCommand(); |
136 | | - if (cmd.Connection.State == ConnectionState.Closed) |
137 | | - { |
138 | | - cmd.Connection.Open(); |
139 | | - } |
140 | | - //var cmd =_dbProvider.CreateCommand(); |
141 | | - //cmd.Connection = _conn; |
| 78 | + } |
| 79 | + catch (DbException dbe) |
| 80 | + { |
| 81 | + throw new CfException("Não foi possível se conectar ao banco de dados", dbe); |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + throw new CfException("Não foi possível se conectar ao banco de dados", ex); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + [System.Diagnostics.DebuggerStepThrough] |
| 90 | + internal void CloseFactoryConnection() |
| 91 | + { |
| 92 | + if (_conn == null) return; |
| 93 | + |
| 94 | + if (!_conn.State.Equals(ConnectionState.Closed)) |
| 95 | + { |
142 | 96 | if (_tranOpenCount > 0) |
143 | 97 | { |
144 | | - cmd.Transaction = _tran; |
| 98 | + TransactionHandler(TransactionType.TransactionRollback); |
145 | 99 | } |
146 | | - return cmd; |
147 | | - } |
148 | | - |
149 | | - [System.Diagnostics.DebuggerStepThrough] |
150 | | - public CfCommand CreateCfCommand() |
151 | | - { |
152 | | - var cfConnection = this; |
153 | | - return new CfCommand(ref cfConnection); |
154 | | - } |
155 | | - |
156 | | - [System.Diagnostics.DebuggerStepThrough] |
157 | | - public DbDataAdapter CreateDataAdapter() |
158 | | - { |
159 | | - var dataAdp = _dbProvider.CreateDataAdapter(); |
160 | | - return dataAdp; |
161 | | - } |
162 | | - |
163 | | - [System.Diagnostics.DebuggerStepThrough] |
164 | | - internal CfTransaction CreateTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) |
165 | | - { |
166 | | - var cfConnection = this; |
167 | | - return new CfTransaction(ref cfConnection, isolationLevel); |
168 | | - } |
169 | | - } |
| 100 | + _conn.Close(); |
| 101 | + } |
| 102 | + _tranOpenCount = 0; |
| 103 | + //_conn.Dispose(); |
| 104 | + //_conn = null; |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + [System.Diagnostics.DebuggerStepThrough] |
| 109 | + internal void TransactionHandler(TransactionType transactionType, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) |
| 110 | + { |
| 111 | + EstablishFactoryConnection(); |
| 112 | + |
| 113 | + switch (transactionType) |
| 114 | + { |
| 115 | + case TransactionType.TransactionOpen: |
| 116 | + _tran = _conn.BeginTransaction(isolationLevel); |
| 117 | + _tranOpenCount++; |
| 118 | + break; |
| 119 | + case TransactionType.TransactionCommit: |
| 120 | + _tran.Commit(); |
| 121 | + _tranOpenCount--; |
| 122 | + break; |
| 123 | + case TransactionType.TransactionRollback: |
| 124 | + _tran.Rollback(); |
| 125 | + _tranOpenCount--; |
| 126 | + break; |
| 127 | + default: |
| 128 | + throw new ArgumentOutOfRangeException("transactionType"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public ConnectionStringSettings Configuration { get; private set; } |
| 133 | + |
| 134 | + public ConnectionState State |
| 135 | + { |
| 136 | + get { return _conn?.State ?? ConnectionState.Closed; } |
| 137 | + } |
| 138 | + |
| 139 | + internal DbCommand CreateDbCommand() |
| 140 | + { |
| 141 | + var cmd = _conn.CreateCommand(); |
| 142 | + if (cmd.Connection.State == ConnectionState.Closed) |
| 143 | + { |
| 144 | + cmd.Connection.Open(); |
| 145 | + } |
| 146 | + //var cmd =_dbProvider.CreateCommand(); |
| 147 | + //cmd.Connection = _conn; |
| 148 | + if (_tranOpenCount > 0) |
| 149 | + { |
| 150 | + cmd.Transaction = _tran; |
| 151 | + } |
| 152 | + return cmd; |
| 153 | + } |
| 154 | + |
| 155 | + [System.Diagnostics.DebuggerStepThrough] |
| 156 | + public CfCommand CreateCfCommand() |
| 157 | + { |
| 158 | + var cfConnection = this; |
| 159 | + return new CfCommand(ref cfConnection); |
| 160 | + } |
| 161 | + |
| 162 | + [System.Diagnostics.DebuggerStepThrough] |
| 163 | + public DbDataAdapter CreateDataAdapter() |
| 164 | + { |
| 165 | + var dataAdp = _dbProvider.CreateDataAdapter(); |
| 166 | + return dataAdp; |
| 167 | + } |
| 168 | + |
| 169 | + [System.Diagnostics.DebuggerStepThrough] |
| 170 | + public CfTransaction CreateTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) |
| 171 | + { |
| 172 | + var cfConnection = this; |
| 173 | + return new CfTransaction(ref cfConnection, isolationLevel); |
| 174 | + } |
| 175 | + } |
170 | 176 | } |
0 commit comments