Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal static string BuildEtag(string timeStamp, string? suffix)
return $"\"{AssemblyWriteTime}-{timeStamp}-{suffix}\"";
}
```
<sup><a href='/src/Delta/DeltaExtensions_Shared.cs#L142-L154' title='Snippet source file'>snippet source</a> | <a href='#snippet-BuildEtag' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Delta/DeltaExtensions_Shared.cs#L164-L176' title='Snippet source file'>snippet source</a> | <a href='#snippet-BuildEtag' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -330,38 +330,42 @@ app.UseDelta(

By default, Delta uses `HttpContext.RequestServices` to discover the SqlConnection and SqlTransaction:

<!-- snippet: DiscoverConnection -->
<a id='snippet-DiscoverConnection'></a>
<!-- snippet: DiscoverConnectionType -->
<a id='snippet-DiscoverConnectionType'></a>
```cs
static (Type sqlConnection, Type transaction) FindConnectionType()
var sqlConnectionType = Type.GetType("Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient");
if (sqlConnectionType != null)
{
var sqlConnection = Type.GetType("Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient");
if (sqlConnection != null)
{
var transaction = sqlConnection.Assembly.GetType("Microsoft.Data.SqlClient.SqlTransaction")!;
return (sqlConnection, transaction);
}

var npgsqlConnection = Type.GetType("Npgsql.NpgsqlConnection, Npgsql");
if (npgsqlConnection != null)
{
var transaction = npgsqlConnection.Assembly.GetType("Npgsql.NpgsqlTransaction")!;
return (npgsqlConnection, transaction);
}
connectionType = sqlConnectionType;
transactionType = sqlConnectionType.Assembly.GetType("Microsoft.Data.SqlClient.SqlTransaction")!;
return;
}

throw new("Could not find connection type. Tried Microsoft.Data.SqlClient.SqlConnection and Npgsql.NpgsqlTransaction");
var npgConnectionType = Type.GetType("Npgsql.NpgsqlConnection, Npgsql");
if (npgConnectionType != null)
{
connectionType = npgConnectionType;
transactionType = npgConnectionType.Assembly.GetType("Npgsql.NpgsqlTransaction")!;
return;
}

throw new("Could not find connection type. Tried Microsoft.Data.SqlClient.SqlConnection and Npgsql.NpgsqlTransaction");
```
<sup><a href='/src/Delta/DeltaExtensions_Shared.cs#L45-L65' title='Snippet source file'>snippet source</a> | <a href='#snippet-DiscoverConnectionType' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

<!-- snippet: DiscoverConnection -->
<a id='snippet-DiscoverConnection'></a>
```cs
static Connection DiscoverConnection(HttpContext httpContext)
{
var (connectionType, transactionType) = FindConnectionType();
var provider = httpContext.RequestServices;
var connection = (DbConnection) provider.GetRequiredService(connectionType);
var transaction = (DbTransaction?) provider.GetService(transactionType);
return new(connection, transaction);
}
```
<sup><a href='/src/Delta/DeltaExtensions_ConnectionDiscovery.cs#L5-L35' title='Snippet source file'>snippet source</a> | <a href='#snippet-DiscoverConnection' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Delta/DeltaExtensions_ConnectionDiscovery.cs#L8-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-DiscoverConnection' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

To use custom connection discovery:
Expand Down
2 changes: 2 additions & 0 deletions readme.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ snippet: ShouldExecute

By default, Delta uses `HttpContext.RequestServices` to discover the SqlConnection and SqlTransaction:

snippet: DiscoverConnectionType

snippet: DiscoverConnection

To use custom connection discovery:
Expand Down
23 changes: 3 additions & 20 deletions src/Delta/DeltaExtensions_ConnectionDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,13 @@ namespace Delta;

public static partial class DeltaExtensions
{
#region DiscoverConnection

static (Type sqlConnection, Type transaction) FindConnectionType()
{
var sqlConnection = Type.GetType("Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient");
if (sqlConnection != null)
{
var transaction = sqlConnection.Assembly.GetType("Microsoft.Data.SqlClient.SqlTransaction")!;
return (sqlConnection, transaction);
}
static Type connectionType;
static Type transactionType;

var npgsqlConnection = Type.GetType("Npgsql.NpgsqlConnection, Npgsql");
if (npgsqlConnection != null)
{
var transaction = npgsqlConnection.Assembly.GetType("Npgsql.NpgsqlTransaction")!;
return (npgsqlConnection, transaction);
}

throw new("Could not find connection type. Tried Microsoft.Data.SqlClient.SqlConnection and Npgsql.NpgsqlTransaction");
}
#region DiscoverConnection

static Connection DiscoverConnection(HttpContext httpContext)
{
var (connectionType, transactionType) = FindConnectionType();
var provider = httpContext.RequestServices;
var connection = (DbConnection) provider.GetRequiredService(connectionType);
var transaction = (DbTransaction?) provider.GetService(transactionType);
Expand Down
22 changes: 22 additions & 0 deletions src/Delta/DeltaExtensions_Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ static DeltaExtensions()
AssemblyWriteTime = File.GetLastWriteTime(webAssemblyLocation).Ticks.ToString();

#endregion

#region DiscoverConnectionType

var sqlConnectionType = Type.GetType("Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient");
if (sqlConnectionType != null)
{
connectionType = sqlConnectionType;
transactionType = sqlConnectionType.Assembly.GetType("Microsoft.Data.SqlClient.SqlTransaction")!;
return;
}

var npgConnectionType = Type.GetType("Npgsql.NpgsqlConnection, Npgsql");
if (npgConnectionType != null)
{
connectionType = npgConnectionType;
transactionType = npgConnectionType.Assembly.GetType("Npgsql.NpgsqlTransaction")!;
return;
}

throw new("Could not find connection type. Tried Microsoft.Data.SqlClient.SqlConnection and Npgsql.NpgsqlTransaction");

#endregion
}

internal static async Task<bool> HandleRequest(HttpContext context, ILogger logger, Func<HttpContext, string?>? suffix, Func<HttpContext, Task<string>> getTimeStamp, Func<HttpContext, bool>? shouldExecute, LogLevel level)
Expand Down