-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add TransactionAwareContextFactory in separate project
- Loading branch information
1 parent
34ab6e4
commit 1c57f53
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/softaware.Cqs.EntityFramework/TransactionAwareContextFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Data.Common; | ||
using System.Data.SqlClient; | ||
using System.Transactions; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace softaware.Cqs.EntityFramework | ||
{ | ||
public static class TransactionAwareContextFactory | ||
{ | ||
private static ConcurrentDictionary<Transaction, DbConnection> connections = new ConcurrentDictionary<Transaction, DbConnection>(); | ||
|
||
public static TContext CreateContext<TContext>( | ||
string connectionString, | ||
Func<DbContextOptions<TContext>, TContext> dbContextFuncWithOptions, | ||
Func<DbConnection, TContext> dbContextFuncWithDbConnection) | ||
where TContext : DbContext | ||
{ | ||
var currentTransaction = Transaction.Current; | ||
if (currentTransaction == null) | ||
{ | ||
var options = new DbContextOptionsBuilder<TContext>() | ||
.UseSqlServer(connectionString) | ||
.Options; | ||
|
||
return dbContextFuncWithOptions(options); | ||
} | ||
else | ||
{ | ||
var connectionForTransaction = connections.GetOrAdd(currentTransaction, valueFactory: t => | ||
{ | ||
var connection = new SqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
t.TransactionCompleted += (s, e) => | ||
{ | ||
connection.Close(); | ||
connections.TryRemove(t, out var _); | ||
}; | ||
|
||
return connection; | ||
}); | ||
|
||
return dbContextFuncWithDbConnection(connectionForTransaction); | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/softaware.Cqs.EntityFramework/softaware.Cqs.EntityFramework.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<Authors>softaware gmbh</Authors> | ||
<Company>softaware gmbh</Company> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<PackageLicenseUrl>https://github.com/softawaregmbh/library-cqs/blob/master/LICENSE</PackageLicenseUrl> | ||
<PackageProjectUrl>https://github.com/softawaregmbh/library-cqs</PackageProjectUrl> | ||
<PackageIconUrl>https://secure.gravatar.com/avatar/d441f5a5514d2afce7da518a89f028d0?s=512&amp;r=g&amp;d=retro</PackageIconUrl> | ||
<RepositoryUrl>https://github.com/softawaregmbh/library-cqs</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<PackageTags>softaware, command-query-separation</PackageTags> | ||
<Description>Adds a generic EntityFramework DbContext factory which is aware of open transactions.</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.3" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters