Permalink
Browse files

Added static ConnectionCreated event to AdoAdapter

  • Loading branch information...
markrendle committed Dec 10, 2012
1 parent bd4d0f4 commit 24b35f7662854d517071e90d20187591c2eb57ac
Showing with 126 additions and 1 deletion.
  1. +75 −0 Simple.Data.Ado.Test/ConnectionModifierTest.cs
  2. +32 −1 Simple.Data.Ado/AdoAdapter.cs
  3. +19 −0 Simple.Data.Ado/EventHandlerEx.cs
@@ -3,6 +3,8 @@
namespace Simple.Data.Ado.Test
{
+ using System;
+
[TestFixture]
public class ConnectionModifierTest
{
@@ -24,6 +26,29 @@ public void ClearsConnection()
Assert.IsNotInstanceOf<FooConnection>(adapter.CreateConnection());
}
+ [Test]
+ public void ConnectionCreatedEventFires()
+ {
+ bool fired = false;
+ EventHandler<ConnectionCreatedEventArgs> handler = (o, e) => { fired = true; };
+ AdoAdapter.ConnectionCreated += handler;
+ var adapter = new AdoAdapter(new StubConnectionProvider());
+ var connection = adapter.CreateConnection();
+ AdoAdapter.ConnectionCreated -= handler;
+ Assert.True(fired);
+ }
+
+ [Test]
+ public void ConnectionCreatedCanOverrideConnection()
+ {
+ EventHandler<ConnectionCreatedEventArgs> handler = (o, e) => e.OverrideConnection(new BarConnection(e.Connection));
+ AdoAdapter.ConnectionCreated += handler;
+ var adapter = new AdoAdapter(new StubConnectionProvider());
+ var connection = adapter.CreateConnection();
+ Assert.IsInstanceOf<BarConnection>(connection);
+ AdoAdapter.ConnectionCreated -= handler;
+ }
+
private class FooConnection : IDbConnection
{
private readonly IDbConnection _wrapped;
@@ -73,5 +98,55 @@ public void Open()
public string Database { get; private set; }
public ConnectionState State { get; private set; }
}
+
+ private class BarConnection : IDbConnection
+ {
+ private readonly IDbConnection _wrapped;
+
+ public BarConnection(IDbConnection wrapped)
+ {
+ _wrapped = wrapped;
+ }
+
+ public void Dispose()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public IDbTransaction BeginTransaction()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public IDbTransaction BeginTransaction(IsolationLevel il)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public void Close()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public void ChangeDatabase(string databaseName)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public IDbCommand CreateCommand()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public void Open()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ConnectionString { get; set; }
+ public int ConnectionTimeout { get; private set; }
+ public string Database { get; private set; }
+ public ConnectionState State { get; private set; }
+ }
}
}
@@ -307,7 +307,14 @@ public void StopUsingSharedConnection()
public IDbConnection CreateConnection()
{
- return _sharedConnection ?? _connectionModifier(_connectionProvider.CreateConnection());
+ if (_sharedConnection != null) return _sharedConnection;
+ var connection = _connectionModifier(_connectionProvider.CreateConnection());
+ var args = ConnectionCreated.Raise(this, () => new ConnectionCreatedEventArgs(connection));
+ if (args != null && args.OverriddenConnection != null)
+ {
+ return args.OverriddenConnection;
+ }
+ return connection;
}
public DatabaseSchema GetSchema()
@@ -341,5 +348,29 @@ protected override void OnReset()
DatabaseSchema.ClearCache();
_schema = DatabaseSchema.Get(_connectionProvider, _providerHelper);
}
+
+ public static event EventHandler<ConnectionCreatedEventArgs> ConnectionCreated;
+ }
+
+ public class ConnectionCreatedEventArgs : EventArgs
+ {
+ private readonly IDbConnection _connection;
+
+ public ConnectionCreatedEventArgs(IDbConnection connection)
+ {
+ _connection = connection;
+ }
+
+ public IDbConnection Connection
+ {
+ get { return _connection; }
+ }
+
+ internal IDbConnection OverriddenConnection { get; private set; }
+
+ public void OverrideConnection(IDbConnection overridingConnection)
+ {
+ OverriddenConnection = overridingConnection;
+ }
}
}
@@ -0,0 +1,19 @@
+namespace Simple.Data.Ado
+{
+ using System;
+
+ internal static class EventHandlerEx
+ {
+ public static T Raise<T>(this EventHandler<T> handler, object sender, Func<T> args)
+ where T : EventArgs
+ {
+ if (handler != null)
+ {
+ var e = args();
+ handler(sender, e);
+ return e;
+ }
+ return null;
+ }
+ }
+}

0 comments on commit 24b35f7

Please sign in to comment.