Skip to content

Commit 24b35f7

Browse files
committed
Added static ConnectionCreated event to AdoAdapter
1 parent bd4d0f4 commit 24b35f7

3 files changed

Lines changed: 126 additions & 1 deletion

File tree

Simple.Data.Ado.Test/ConnectionModifierTest.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Simple.Data.Ado.Test
55
{
6+
using System;
7+
68
[TestFixture]
79
public class ConnectionModifierTest
810
{
@@ -24,6 +26,29 @@ public void ClearsConnection()
2426
Assert.IsNotInstanceOf<FooConnection>(adapter.CreateConnection());
2527
}
2628

29+
[Test]
30+
public void ConnectionCreatedEventFires()
31+
{
32+
bool fired = false;
33+
EventHandler<ConnectionCreatedEventArgs> handler = (o, e) => { fired = true; };
34+
AdoAdapter.ConnectionCreated += handler;
35+
var adapter = new AdoAdapter(new StubConnectionProvider());
36+
var connection = adapter.CreateConnection();
37+
AdoAdapter.ConnectionCreated -= handler;
38+
Assert.True(fired);
39+
}
40+
41+
[Test]
42+
public void ConnectionCreatedCanOverrideConnection()
43+
{
44+
EventHandler<ConnectionCreatedEventArgs> handler = (o, e) => e.OverrideConnection(new BarConnection(e.Connection));
45+
AdoAdapter.ConnectionCreated += handler;
46+
var adapter = new AdoAdapter(new StubConnectionProvider());
47+
var connection = adapter.CreateConnection();
48+
Assert.IsInstanceOf<BarConnection>(connection);
49+
AdoAdapter.ConnectionCreated -= handler;
50+
}
51+
2752
private class FooConnection : IDbConnection
2853
{
2954
private readonly IDbConnection _wrapped;
@@ -73,5 +98,55 @@ public void Open()
7398
public string Database { get; private set; }
7499
public ConnectionState State { get; private set; }
75100
}
101+
102+
private class BarConnection : IDbConnection
103+
{
104+
private readonly IDbConnection _wrapped;
105+
106+
public BarConnection(IDbConnection wrapped)
107+
{
108+
_wrapped = wrapped;
109+
}
110+
111+
public void Dispose()
112+
{
113+
throw new System.NotImplementedException();
114+
}
115+
116+
public IDbTransaction BeginTransaction()
117+
{
118+
throw new System.NotImplementedException();
119+
}
120+
121+
public IDbTransaction BeginTransaction(IsolationLevel il)
122+
{
123+
throw new System.NotImplementedException();
124+
}
125+
126+
public void Close()
127+
{
128+
throw new System.NotImplementedException();
129+
}
130+
131+
public void ChangeDatabase(string databaseName)
132+
{
133+
throw new System.NotImplementedException();
134+
}
135+
136+
public IDbCommand CreateCommand()
137+
{
138+
throw new System.NotImplementedException();
139+
}
140+
141+
public void Open()
142+
{
143+
throw new System.NotImplementedException();
144+
}
145+
146+
public string ConnectionString { get; set; }
147+
public int ConnectionTimeout { get; private set; }
148+
public string Database { get; private set; }
149+
public ConnectionState State { get; private set; }
150+
}
76151
}
77152
}

Simple.Data.Ado/AdoAdapter.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,14 @@ public void StopUsingSharedConnection()
307307

308308
public IDbConnection CreateConnection()
309309
{
310-
return _sharedConnection ?? _connectionModifier(_connectionProvider.CreateConnection());
310+
if (_sharedConnection != null) return _sharedConnection;
311+
var connection = _connectionModifier(_connectionProvider.CreateConnection());
312+
var args = ConnectionCreated.Raise(this, () => new ConnectionCreatedEventArgs(connection));
313+
if (args != null && args.OverriddenConnection != null)
314+
{
315+
return args.OverriddenConnection;
316+
}
317+
return connection;
311318
}
312319

313320
public DatabaseSchema GetSchema()
@@ -341,5 +348,29 @@ protected override void OnReset()
341348
DatabaseSchema.ClearCache();
342349
_schema = DatabaseSchema.Get(_connectionProvider, _providerHelper);
343350
}
351+
352+
public static event EventHandler<ConnectionCreatedEventArgs> ConnectionCreated;
353+
}
354+
355+
public class ConnectionCreatedEventArgs : EventArgs
356+
{
357+
private readonly IDbConnection _connection;
358+
359+
public ConnectionCreatedEventArgs(IDbConnection connection)
360+
{
361+
_connection = connection;
362+
}
363+
364+
public IDbConnection Connection
365+
{
366+
get { return _connection; }
367+
}
368+
369+
internal IDbConnection OverriddenConnection { get; private set; }
370+
371+
public void OverrideConnection(IDbConnection overridingConnection)
372+
{
373+
OverriddenConnection = overridingConnection;
374+
}
344375
}
345376
}

Simple.Data.Ado/EventHandlerEx.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Simple.Data.Ado
2+
{
3+
using System;
4+
5+
internal static class EventHandlerEx
6+
{
7+
public static T Raise<T>(this EventHandler<T> handler, object sender, Func<T> args)
8+
where T : EventArgs
9+
{
10+
if (handler != null)
11+
{
12+
var e = args();
13+
handler(sender, e);
14+
return e;
15+
}
16+
return null;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)