Skip to content

Commit 2277dfc

Browse files
committed
2015-07-16
- Implementing IDataRecordExtensions... - Implementing DataReader... - Removing obsolete methods - Implementing IDbTransactionScope... - ConnectionDefinition - Implementing DataReader... - How to log CallerInformation - Implementing PostgreSQL provider...
1 parent ed052ae commit 2277dfc

File tree

138 files changed

+2377
-1865
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+2377
-1865
lines changed

DataCommander.Foundation/Collections/IndexableCollection/NonUniqueIndex.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
/// <typeparam name="T"></typeparam>
1414
public class NonUniqueIndex<TKey, T> : ICollectionIndex<T>, IDictionary<TKey, ICollection<T>>
1515
{
16+
#region Private Fields
17+
1618
private string name;
1719
private IDictionary<TKey, ICollection<T>> dictionary;
1820
private Func<T, GetKeyResponse<TKey>> getKey;
1921
private Func<ICollection<T>> createCollection;
2022

23+
#endregion
24+
2125
/// <summary>
2226
///
2327
/// </summary>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace DataCommander.Foundation.Data
2+
{
3+
using System.Collections.Generic;
4+
using System.Data;
5+
6+
/// <summary>
7+
///
8+
/// </summary>
9+
public sealed class CommandDefinition
10+
{
11+
/// <summary>
12+
///
13+
/// </summary>
14+
public string CommandText;
15+
16+
/// <summary>
17+
///
18+
/// </summary>
19+
public List<object> Parameters;
20+
21+
/// <summary>
22+
///
23+
/// </summary>
24+
public CommandType CommandType = CommandType.Text;
25+
26+
/// <summary>
27+
///
28+
/// </summary>
29+
public int CommandTimeout;
30+
}
31+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
namespace DataCommander.Foundation.Data
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Data;
6+
using System.Diagnostics.Contracts;
7+
8+
/// <summary>
9+
///
10+
/// </summary>
11+
public sealed class DataReader : IDisposable
12+
{
13+
#region Private Fields
14+
private readonly IDbCommand command;
15+
private readonly IDataReader dataReader;
16+
private bool nextResultCalled = true;
17+
#endregion
18+
19+
private DataReader(IDbCommand command, IDataReader dataReader)
20+
{
21+
Contract.Requires<ArgumentNullException>(command != null);
22+
Contract.Requires<ArgumentNullException>(dataReader != null);
23+
24+
this.command = command;
25+
this.dataReader = dataReader;
26+
}
27+
28+
internal static DataReader Create(
29+
IDbTransactionScope transactionScope,
30+
CommandDefinition commandDefinition,
31+
CommandBehavior commandBehavior)
32+
{
33+
Contract.Requires<ArgumentNullException>(transactionScope != null);
34+
Contract.Requires<ArgumentNullException>(commandDefinition != null);
35+
36+
IDbCommand command = null;
37+
IDataReader dataReader = null;
38+
39+
try
40+
{
41+
command = transactionScope.CreateCommand(commandDefinition);
42+
dataReader = command.ExecuteReader(commandBehavior);
43+
return new DataReader(command, dataReader);
44+
}
45+
catch
46+
{
47+
if (dataReader != null)
48+
{
49+
dataReader.Dispose();
50+
}
51+
52+
if (command != null)
53+
{
54+
command.Dispose();
55+
}
56+
57+
throw;
58+
}
59+
}
60+
61+
#region Public Methods
62+
63+
/// <summary>
64+
///
65+
/// </summary>
66+
/// <typeparam name="T"></typeparam>
67+
/// <param name="read"></param>
68+
/// <returns></returns>
69+
public IEnumerable<T> Read<T>(Func<IDataRecord, T> read)
70+
{
71+
Contract.Requires<ArgumentNullException>(read != null);
72+
this.PrivateNextResult();
73+
74+
while (this.dataReader.Read())
75+
{
76+
yield return read(this.dataReader);
77+
}
78+
}
79+
80+
/// <summary>
81+
///
82+
/// </summary>
83+
/// <param name="read"></param>
84+
public void Read(Action<IDataRecord> read)
85+
{
86+
Contract.Requires<ArgumentNullException>(read != null);
87+
this.PrivateNextResult();
88+
89+
while (this.dataReader.Read())
90+
{
91+
read(this.dataReader);
92+
}
93+
}
94+
95+
/// <summary>
96+
///
97+
/// </summary>
98+
/// <param name="read"></param>
99+
public void Read(Func<IDataRecord, bool> read)
100+
{
101+
Contract.Requires<ArgumentNullException>(read != null);
102+
this.PrivateNextResult();
103+
104+
while (this.dataReader.Read())
105+
{
106+
bool succeeded = read(this.dataReader);
107+
if (!succeeded)
108+
{
109+
break;
110+
}
111+
}
112+
}
113+
114+
/// <summary>
115+
///
116+
/// </summary>
117+
/// <returns></returns>
118+
public bool NextResult()
119+
{
120+
Contract.Assert(this.dataReader != null);
121+
Contract.Assert(!this.nextResultCalled);
122+
bool nextResult = this.dataReader.NextResult();
123+
this.nextResultCalled = true;
124+
return nextResult;
125+
}
126+
127+
#endregion
128+
129+
void IDisposable.Dispose()
130+
{
131+
this.dataReader.Dispose();
132+
this.command.Dispose();
133+
}
134+
135+
private void PrivateNextResult()
136+
{
137+
if (this.nextResultCalled)
138+
{
139+
this.nextResultCalled = false;
140+
}
141+
else
142+
{
143+
bool nextResult = this.dataReader.NextResult();
144+
this.nextResultCalled = true;
145+
146+
if (!nextResult)
147+
{
148+
throw new InvalidOperationException();
149+
}
150+
}
151+
}
152+
}
153+
}

DataCommander.Foundation/Data/DataReaderContext.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

DataCommander.Foundation/Data/DbProviderFactoryExtensions.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
namespace DataCommander.Foundation.Data
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Data;
56
using System.Data.Common;
67
using System.Diagnostics.Contracts;
78
using System.Globalization;
9+
using System.Linq;
810

911
/// <summary>
1012
///
@@ -37,5 +39,53 @@ public static DataTable ExecuteDataTable(
3739
adapter.Fill(table);
3840
return table;
3941
}
42+
43+
/// <summary>
44+
///
45+
/// </summary>
46+
/// <param name="dbProviderFactory"></param>
47+
/// <param name="connectionString"></param>
48+
/// <param name="commandDefinition"></param>
49+
/// <param name="commandBehavior"></param>
50+
/// <param name="read"></param>
51+
public static IEnumerable<T> ExecuteReader<T>(
52+
this DbProviderFactory dbProviderFactory,
53+
string connectionString,
54+
CommandDefinition commandDefinition,
55+
CommandBehavior commandBehavior,
56+
Func<IDataRecord, T> read)
57+
{
58+
Contract.Requires<ArgumentNullException>(dbProviderFactory != null);
59+
Contract.Requires<ArgumentNullException>(commandDefinition != null);
60+
Contract.Requires<ArgumentNullException>(read != null);
61+
62+
using (var connection = dbProviderFactory.CreateConnection())
63+
{
64+
connection.ConnectionString = connectionString;
65+
connection.Open();
66+
var transactionScope = new DbTransactionScope(connection, null);
67+
68+
using (var dataReader = transactionScope.ExecuteReader(commandDefinition, commandBehavior))
69+
{
70+
return dataReader.Read(read);
71+
}
72+
}
73+
}
74+
75+
/// <summary>
76+
///
77+
/// </summary>
78+
/// <param name="dbProviderFactory"></param>
79+
/// <param name="connnectionString"></param>
80+
/// <param name="commandText"></param>
81+
/// <param name="read"></param>
82+
public static IEnumerable<T> ExecuteReader<T>(
83+
this DbProviderFactory dbProviderFactory,
84+
string connnectionString,
85+
string commandText,
86+
Func<IDataRecord, T> read)
87+
{
88+
return dbProviderFactory.ExecuteReader(connnectionString, new CommandDefinition {CommandText = commandText}, CommandBehavior.Default, read);
89+
}
4090
}
4191
}
Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace DataCommander.Foundation.Data
1+
namespace DataCommander.Foundation.Data
22
{
33
using System;
44
using System.Data;
@@ -7,57 +7,37 @@
77
/// <summary>
88
///
99
/// </summary>
10-
public sealed class DbConnectionContext : IDbConnectionContext
10+
public sealed class DbTransactionScope : IDbTransactionScope
1111
{
12-
#region Private Fields
13-
1412
private readonly IDbConnection connection;
1513
private readonly IDbTransaction transaction;
16-
private readonly int? commandTimeout;
17-
18-
#endregion
1914

2015
/// <summary>
2116
///
2217
/// </summary>
2318
/// <param name="connection"></param>
2419
/// <param name="transaction"></param>
25-
/// <param name="commandTimeout"></param>
26-
public DbConnectionContext(IDbConnection connection, IDbTransaction transaction, int? commandTimeout)
20+
public DbTransactionScope(IDbConnection connection, IDbTransaction transaction)
2721
{
2822
Contract.Requires<ArgumentNullException>(connection != null);
29-
3023
this.connection = connection;
3124
this.transaction = transaction;
32-
this.commandTimeout = commandTimeout;
3325
}
3426

35-
#region IDbConnectionContext Members
36-
37-
IDbConnection IDbConnectionContext.Connection
27+
IDbConnection IDbTransactionScope.Connection
3828
{
3929
get
4030
{
4131
return this.connection;
4232
}
4333
}
4434

45-
IDbTransaction IDbConnectionContext.Transaction
35+
IDbTransaction IDbTransactionScope.Transaction
4636
{
4737
get
4838
{
4939
return this.transaction;
5040
}
5141
}
52-
53-
int? IDbConnectionContext.CommandTimeout
54-
{
55-
get
56-
{
57-
return this.commandTimeout;
58-
}
59-
}
60-
61-
#endregion
6242
}
6343
}

0 commit comments

Comments
 (0)