-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Tim-Maes/feature/transactions
Add support for DbTransactions
- Loading branch information
Showing
16 changed files
with
207 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
2 changes: 1 addition & 1 deletion
2
src/GraphR.Application/Authors/Handlers/GetAuthorsQueryHandler.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
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
2 changes: 1 addition & 1 deletion
2
src/GraphR.Application/Books/Handlers/Mutation/CreateBookMutationHandler.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
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
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
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
2 changes: 1 addition & 1 deletion
2
...hR.Domain/Interfaces/IAuthorRepository.cs → ...erfaces/Repositories/IAuthorRepository.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
2 changes: 1 addition & 1 deletion
2
...aphR.Domain/Interfaces/IBookRepository.cs → ...nterfaces/Repositories/IBookRepository.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
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,10 @@ | ||
namespace GraphR.Domain.Interfaces.Transaction; | ||
|
||
public interface ITransaction : IDisposable | ||
{ | ||
void Begin(); | ||
|
||
void Commit(); | ||
|
||
void Rollback(); | ||
} |
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
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,73 @@ | ||
using System.Data; | ||
using Bindicate.Attributes; | ||
using GraphR.Domain.Interfaces.Transaction; | ||
using GrapR.Infrastructure.Database; | ||
|
||
namespace GraphR.Infrastructure.Database; | ||
|
||
[AddTransient(typeof(ITransaction))] | ||
public class DbTransaction : ITransaction | ||
{ | ||
private readonly IDbConnectionProvider _connectionProvider; | ||
private IDbConnection _connection; | ||
private IDbTransaction _transaction; | ||
private bool _disposed; | ||
|
||
public DbTransaction(IDbConnectionProvider connectionProvider) | ||
{ | ||
_connectionProvider = connectionProvider; | ||
} | ||
|
||
public void Begin() | ||
{ | ||
if (_connection is null) _connection = _connectionProvider.CreateConnection(); | ||
|
||
if (_connection.State is not ConnectionState.Open) _connection.Open(); | ||
|
||
_transaction = _connection.BeginTransaction(); | ||
} | ||
|
||
public void Commit() | ||
{ | ||
try | ||
{ | ||
_transaction?.Commit(); | ||
} | ||
catch | ||
{ | ||
Rollback(); | ||
throw; | ||
} | ||
finally | ||
{ | ||
DisposeTransaction(); | ||
} | ||
} | ||
|
||
public void Rollback() | ||
{ | ||
_transaction?.Rollback(); | ||
DisposeTransaction(); | ||
} | ||
|
||
private void DisposeTransaction() | ||
{ | ||
_transaction?.Dispose(); | ||
_transaction = null; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
_disposed = true; | ||
DisposeTransaction(); | ||
|
||
if (_connection is null) | ||
{ | ||
_connection.Dispose(); | ||
_connection = null; | ||
} | ||
} | ||
} |
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
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