Skip to content

Commit 5939964

Browse files
committed
Update DataAccess library in accordance with http://databooster.codeplex.com/
1 parent a169788 commit 5939964

File tree

5 files changed

+147
-43
lines changed

5 files changed

+147
-43
lines changed

TaskParallelFoundation/DataAccess/DataAccess.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="Oracle\DbAccess.cs" />
5454
<Compile Include="Oracle\DbExtensions.partial.cs" />
5555
<Compile Include="Oracle\DbParameterBuilder.partial.cs" />
56+
<Compile Include="ParallelExecuteWaitHandle.cs" />
5657
<Compile Include="Properties\AssemblyInfo.cs" />
5758
</ItemGroup>
5859
<ItemGroup>

TaskParallelFoundation/DataAccess/DbAccess.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Data;
33
using System.Data.Common;
44
using System.Configuration;
5+
using System.Collections.Generic;
56

67
namespace DbParallel.DataAccess
78
{
@@ -30,12 +31,8 @@ public DbAccess(ConnectionStringSettings connSetting)
3031
}
3132

3233
public DbAccess(string connectionStringKey)
34+
: this(ConfigurationManager.ConnectionStrings[connectionStringKey])
3335
{
34-
ConnectionStringSettings connSetting = ConfigurationManager.ConnectionStrings[connectionStringKey];
35-
36-
_Connection = DbProviderFactories.GetFactory(connSetting.ProviderName).CreateConnection();
37-
_Connection.ConnectionString = connSetting.ConnectionString;
38-
_Connection.Open();
3936
}
4037

4138
private DbCommand CreateCommand(string commandText, int commandTimeout, CommandType commandType, Action<DbParameterBuilder> parametersBuilder)
@@ -149,6 +146,29 @@ public void ExecuteReader<T>(string commandText, Action<DbParameterBuilder> para
149146
ExecuteReader<T>(commandText, 0, CommandType.StoredProcedure, parametersBuilder, null, readEntity);
150147
}
151148

149+
public IEnumerable<T> ExecuteReader<T>(string commandText, int commandTimeout, CommandType commandType,
150+
Action<DbParameterBuilder> parametersBuilder, Action<DbFieldMap<T>> resultMap = null) where T : new()
151+
{
152+
using (DbDataReader reader = CreateReader(commandText, commandTimeout, commandType, parametersBuilder))
153+
{
154+
DbFieldMap<T> map = new DbFieldMap<T>();
155+
156+
if (resultMap == null)
157+
map.AddAllPropertiesOrFields();
158+
else
159+
resultMap(map);
160+
161+
while (reader.Read())
162+
yield return map.ReadNew(reader);
163+
}
164+
}
165+
166+
public IEnumerable<T> ExecuteReader<T>(string commandText, Action<DbParameterBuilder> parametersBuilder,
167+
Action<DbFieldMap<T>> resultMap = null) where T : new()
168+
{
169+
return ExecuteReader<T>(commandText, 0, CommandType.StoredProcedure, parametersBuilder, resultMap);
170+
}
171+
152172
public int ExecuteNonQuery(string commandText, int commandTimeout, CommandType commandType, Action<DbParameterBuilder> parametersBuilder)
153173
{
154174
int nAffectedRows = 0;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace DbParallel.DataAccess
6+
{
7+
public class ParallelExecuteWaitHandle : IDisposable
8+
{
9+
protected ManualResetEventSlim _CompleteEvent;
10+
protected int _ExecutingCount;
11+
12+
public ParallelExecuteWaitHandle(bool initialState = true)
13+
{
14+
_CompleteEvent = new ManualResetEventSlim(initialState);
15+
_ExecutingCount = 0;
16+
}
17+
18+
protected virtual void EnterTask()
19+
{
20+
if (Interlocked.Increment(ref _ExecutingCount) == 1)
21+
_CompleteEvent.Reset();
22+
}
23+
24+
protected virtual void ExitTask()
25+
{
26+
if (Interlocked.Decrement(ref _ExecutingCount) == 0)
27+
_CompleteEvent.Set();
28+
}
29+
30+
public Task StartNewTask(Action action)
31+
{
32+
EnterTask();
33+
34+
return Task.Factory.StartNew(() =>
35+
{
36+
try
37+
{
38+
action();
39+
}
40+
finally
41+
{
42+
ExitTask();
43+
}
44+
});
45+
}
46+
47+
public Task StartNewTask<T>(Action<T> action, T state)
48+
{
49+
EnterTask();
50+
51+
return Task.Factory.StartNew(context =>
52+
{
53+
try
54+
{
55+
action((T)context);
56+
}
57+
finally
58+
{
59+
ExitTask();
60+
}
61+
}, state);
62+
}
63+
64+
public void Wait()
65+
{
66+
if (_ExecutingCount > 0)
67+
_CompleteEvent.Wait();
68+
}
69+
70+
public void Wait(int millisecondsTimeout)
71+
{
72+
if (_ExecutingCount > 0)
73+
_CompleteEvent.Wait(millisecondsTimeout);
74+
}
75+
76+
public void Wait(TimeSpan timeout)
77+
{
78+
if (_ExecutingCount > 0)
79+
_CompleteEvent.Wait(timeout);
80+
}
81+
82+
public void Dispose()
83+
{
84+
if (_CompleteEvent != null)
85+
{
86+
_CompleteEvent.Dispose();
87+
_CompleteEvent = null;
88+
}
89+
}
90+
}
91+
}
92+
93+
////////////////////////////////////////////////////////////////////////////////////////////////////
94+
//
95+
// Copyright 2012 Abel Cheng
96+
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
97+
// See http://www.apache.org/licenses/LICENSE-2.0.
98+
// All other rights reserved.
99+
// You must not remove this notice, or any other, from this software.
100+
//
101+
// Original Author: Abel Cheng <[email protected]>
102+
// Created Date: 2012-05-30
103+
// Primary Host: http://databooster.codeplex.com
104+
// Change Log:
105+
// Author Date Comment
106+
//
107+
//
108+
//
109+
//
110+
// (Keep clean code rather than complicated code plus long comments.)
111+
//
112+
////////////////////////////////////////////////////////////////////////////////////////////////////

TaskParallelFoundation/DataAccess/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.1")]
36-
[assembly: AssemblyFileVersion("1.0.0.1")]
35+
[assembly: AssemblyVersion("1.0.0.2")]
36+
[assembly: AssemblyFileVersion("1.0.0.2")]

TaskParallelFoundation/Dispatcher/PumpSynchronizer.cs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,35 @@
11
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using DbParallel.DataAccess;
45

56
namespace DbParallel.Dispatcher
67
{
7-
internal class PumpSynchronizer : IDisposable
8+
internal class PumpSynchronizer : ParallelExecuteWaitHandle
89
{
910
private volatile bool _KeepPumping;
10-
private ManualResetEvent _EndingEvent;
11-
private int _ExecutingCount;
1211

1312
public PumpSynchronizer()
13+
: base(false)
1414
{
1515
_KeepPumping = false;
16-
_EndingEvent = new ManualResetEvent(false);
17-
_ExecutingCount = 0;
1816
}
1917

2018
public bool KeepPumping
2119
{
2220
get { return _KeepPumping; }
2321
}
2422

25-
private void EnterTask()
23+
protected override void EnterTask()
2624
{
2725
Interlocked.Increment(ref _ExecutingCount);
2826
}
2927

30-
private void ExitTask()
28+
protected override void ExitTask()
3129
{
3230
if (Interlocked.Decrement(ref _ExecutingCount) == 0)
3331
if (_KeepPumping == false)
34-
_EndingEvent.Set();
35-
}
36-
37-
public Task StartNewTask(Action action)
38-
{
39-
EnterTask();
40-
41-
return Task.Factory.StartNew(() =>
42-
{
43-
try
44-
{
45-
action();
46-
}
47-
finally
48-
{
49-
ExitTask();
50-
}
51-
});
32+
_CompleteEvent.Set();
5233
}
5334

5435
public Task StartPump(Action action)
@@ -65,17 +46,7 @@ public void StopPump()
6546
{
6647
_KeepPumping = false;
6748

68-
if (_ExecutingCount > 0)
69-
_EndingEvent.WaitOne();
70-
}
71-
72-
public void Dispose()
73-
{
74-
if (_EndingEvent != null)
75-
{
76-
_EndingEvent.Close();
77-
_EndingEvent = null;
78-
}
49+
Wait();
7950
}
8051
}
8152
}

0 commit comments

Comments
 (0)