Skip to content

Commit 8fd925b

Browse files
committed
ToExpando
1 parent cf51577 commit 8fd925b

File tree

10 files changed

+979
-821
lines changed

10 files changed

+979
-821
lines changed

src/CfCommand.cs

Lines changed: 523 additions & 408 deletions
Large diffs are not rendered by default.

src/CfConnection.cs

Lines changed: 153 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -7,164 +7,170 @@
77

88
namespace ConnectionFactory
99
{
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+
{
3129
Configuration = ConfigurationManager.ConnectionStrings[connectionName];
3230

3331
if (Configuration == null)
34-
throw new CfException(string.Format("Conexão {0} não definida nas configurações", connectionName));
35-
36-
try
3732
{
38-
_dbProvider = DbProviderFactories.GetFactory(Configuration.ProviderName);
33+
throw new CfException(String.Format("{0} connection not defined in the settings.", connectionName));
3934
}
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;
6335

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)
7473
{
75-
throw new CfException("Não foi possível se conectar ao banco de dados", dbe);
74+
_conn.Open();
7675
}
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;
8776

88-
if (!_conn.State.Equals(ConnectionState.Closed))
89-
{
90-
if (_tranOpenCount > 0)
91-
{
92-
TransactionHandler(TransactionType.TransactionRollback);
93-
}
94-
_conn.Close();
95-
}
9677
_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+
{
14296
if (_tranOpenCount > 0)
14397
{
144-
cmd.Transaction = _tran;
98+
TransactionHandler(TransactionType.TransactionRollback);
14599
}
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+
}
170176
}

src/CfConvert.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
namespace ConnectionFactory
1+
using System.Runtime.InteropServices;
2+
3+
namespace ConnectionFactory
24
{
35
using System;
46
using System.Data;
5-
using System.Runtime.InteropServices;
67
using System.Collections.Generic;
78
using System.ComponentModel;
89
using System.Globalization;
@@ -118,7 +119,7 @@ public static string UTF8ToString(IntPtr nativestring, int nativestringlen)
118119

119120
byte[] byteArray = new byte[nativestringlen];
120121

121-
Marshal.Copy(nativestring, byteArray, 0, nativestringlen);
122+
Marshal.Copy(nativestring, byteArray, 0, nativestringlen);
122123

123124
return _utf8.GetString(byteArray, 0, nativestringlen);
124125
}

src/CfExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Dynamic;
5+
6+
namespace ConnectionFactory
7+
{
8+
public static class CfExtensions
9+
{
10+
public static dynamic ToExpando(this IDataReader reader)
11+
{
12+
var dictionary = new ExpandoObject() as IDictionary<string, object>;
13+
for (int i = 0; i < reader.FieldCount; i++)
14+
dictionary.Add(reader.GetName(i), reader[i] == DBNull.Value ? null : reader[i]);
15+
16+
return dictionary as ExpandoObject;
17+
}
18+
19+
}
20+
}

0 commit comments

Comments
 (0)