Skip to content

Commit 29feacb

Browse files
committed
Add ConnectionHandler API
1 parent c0dbb48 commit 29feacb

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
6+
using System.Data.Common;
7+
using Xtensive.Core;
8+
9+
namespace Xtensive.Orm
10+
{
11+
/// <summary>
12+
/// Extended <see cref="ConnectionEventData"/> with error happend during connection opening, restoration or initialization.
13+
/// </summary>
14+
public class ConnectionErrorEventData : ConnectionEventData
15+
{
16+
/// <summary>
17+
/// The exception appeared.
18+
/// </summary>
19+
public Exception Exception { get; }
20+
21+
public ConnectionErrorEventData(Exception exception, DbConnection connection, bool reconnect = false)
22+
: base(connection, reconnect)
23+
{
24+
ArgumentValidator.EnsureArgumentNotNull(exception, nameof(exception));
25+
Exception = exception;
26+
}
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System.Data.Common;
6+
using Xtensive.Core;
7+
8+
namespace Xtensive.Orm
9+
{
10+
/// <summary>
11+
/// Contains general data for <see cref="IConnectionHandler"/> methods.
12+
/// </summary>
13+
public class ConnectionEventData
14+
{
15+
/// <summary>
16+
/// The connection for which event triggered.
17+
/// </summary>
18+
public DbConnection Connection { get; }
19+
20+
/// <summary>
21+
/// Indicates whether event happened during an attempt to restore connection.
22+
/// </summary>
23+
public bool Reconnect { get; }
24+
25+
public ConnectionEventData(DbConnection connection, bool reconnect = false)
26+
{
27+
ArgumentValidator.EnsureArgumentNotNull(connection, nameof(connection));
28+
Connection = connection;
29+
Reconnect = reconnect;
30+
}
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System.Data.Common;
6+
using Xtensive.Core;
7+
8+
namespace Xtensive.Orm
9+
{
10+
/// <summary>
11+
/// Extended <see cref="ConnectionEventData"/> with connection initialization script
12+
/// </summary>
13+
public class ConnectionInitEventData : ConnectionEventData
14+
{
15+
/// <summary>
16+
/// Gets the script which will be used for connection initializatin
17+
/// </summary>
18+
public string InitializationScript { get; }
19+
20+
public ConnectionInitEventData(string initializationScript, DbConnection connection, bool reconnect = false)
21+
: base(connection, reconnect)
22+
{
23+
ArgumentValidator.EnsureArgumentNotNullOrEmpty(initializationScript, nameof(initializationScript));
24+
InitializationScript = initializationScript;
25+
}
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Xtensive.Orm
9+
{
10+
/// <summary>
11+
/// Base type for connection handlers to be inherited from.
12+
/// </summary>
13+
public abstract class ConnectionHandler : IConnectionHandler
14+
{
15+
/// <inheritdoc/>
16+
public virtual void ConnectionOpening(ConnectionEventData eventData)
17+
{
18+
}
19+
20+
/// <inheritdoc/>
21+
public virtual void ConnectionInitialization(ConnectionInitEventData eventData)
22+
{
23+
}
24+
25+
/// <inheritdoc/>
26+
public virtual void ConnectionOpened(ConnectionEventData eventData)
27+
{
28+
}
29+
30+
/// <inheritdoc/>
31+
public virtual void ConnectionOpeningFailed(ConnectionErrorEventData eventData)
32+
{
33+
}
34+
}
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Xtensive.Orm
9+
{
10+
/// <summary>
11+
/// Offers event-like methods to access native database connection on different stages.
12+
/// </summary>
13+
public interface IConnectionHandler
14+
{
15+
/// <summary>
16+
/// Executes before connection opening.
17+
/// </summary>
18+
/// <param name="eventData">Information connected with this event.</param>
19+
void ConnectionOpening(ConnectionEventData eventData);
20+
21+
/// <summary>
22+
/// Executes when connection is already opened but initialization script
23+
/// hasn't been executed yet.
24+
/// </summary>
25+
/// <param name="eventData">Information connected with this event.</param>
26+
void ConnectionInitialization(ConnectionInitEventData eventData);
27+
28+
/// <summary>
29+
/// Executes when connection is successfully opened and initialized.
30+
/// </summary>
31+
/// <param name="eventData">Information connected with this event.</param>
32+
void ConnectionOpened(ConnectionEventData eventData);
33+
34+
/// <summary>
35+
/// Executes if an error appeared on either connection opening or connection initialization.
36+
/// </summary>
37+
/// <param name="eventData">Information connected with this event.</param>
38+
void ConnectionOpeningFailed(ConnectionErrorEventData eventData);
39+
}
40+
}

0 commit comments

Comments
 (0)