-
Notifications
You must be signed in to change notification settings - Fork 1
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 #92 from marcwittke/develop
version 6.1.0
- Loading branch information
Showing
74 changed files
with
993 additions
and
2,244 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
20 changes: 20 additions & 0 deletions
20
src/abstractions/Backend.Fx/Extensions/DelegateDisposable.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,20 @@ | ||
using System; | ||
using JetBrains.Annotations; | ||
|
||
namespace Backend.Fx.Extensions | ||
{ | ||
public class DelegateDisposable : IDisposable | ||
{ | ||
private readonly Action _onDisposal; | ||
|
||
public DelegateDisposable([NotNull] Action onDisposal) | ||
{ | ||
_onDisposal = onDisposal ?? throw new ArgumentNullException(nameof(onDisposal)); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_onDisposal(); | ||
} | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/abstractions/Backend.Fx/Patterns/IdGeneration/ISequence.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,9 @@ | ||
namespace Backend.Fx.Patterns.IdGeneration | ||
{ | ||
public interface ISequence | ||
{ | ||
void EnsureSequence(); | ||
int GetNextValue(); | ||
int Increment { get; } | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/abstractions/Backend.Fx/Patterns/IdGeneration/SequenceHiLoIdGenerator.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,19 @@ | ||
namespace Backend.Fx.Patterns.IdGeneration | ||
{ | ||
public abstract class SequenceHiLoIdGenerator : HiLoIdGenerator, IEntityIdGenerator | ||
{ | ||
private readonly ISequence _sequence; | ||
|
||
protected SequenceHiLoIdGenerator(ISequence sequence) | ||
{ | ||
_sequence = sequence; | ||
} | ||
|
||
protected override int GetNextBlockStart() | ||
{ | ||
return _sequence.GetNextValue(); | ||
} | ||
|
||
protected override int BlockSize => _sequence.Increment; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/abstractions/Backend.Fx/Patterns/Transactions/ReadonlyDecorator.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,26 @@ | ||
using System.Data; | ||
|
||
namespace Backend.Fx.Patterns.Transactions | ||
{ | ||
public class ReadonlyDecorator : ITransactionContext | ||
{ | ||
private readonly ITransactionContext _transactionContext; | ||
|
||
public ReadonlyDecorator(ITransactionContext transactionContext) | ||
{ | ||
_transactionContext = transactionContext; | ||
} | ||
|
||
public void BeginTransaction() => _transactionContext.BeginTransaction(); | ||
|
||
public void CommitTransaction() | ||
{ | ||
RollbackTransaction(); | ||
} | ||
|
||
public void RollbackTransaction() => _transactionContext.RollbackTransaction(); | ||
|
||
|
||
public IDbTransaction CurrentTransaction => _transactionContext.CurrentTransaction; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/abstractions/Backend.Fx/Patterns/Transactions/TransactionContext.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,112 @@ | ||
using System; | ||
using System.Data; | ||
using Backend.Fx.Logging; | ||
|
||
namespace Backend.Fx.Patterns.Transactions | ||
{ | ||
/// <summary> | ||
/// wraps an underlying database transaction. In combination with a injection container, access to | ||
/// the current transaction can be gained by means of this interface. | ||
/// </summary> | ||
public interface ITransactionContext | ||
{ | ||
IDbTransaction CurrentTransaction { get; } | ||
void BeginTransaction(); | ||
void CommitTransaction(); | ||
void RollbackTransaction(); | ||
} | ||
|
||
public class TransactionContext : ITransactionContext, IDisposable | ||
{ | ||
private static readonly ILogger Logger = LogManager.Create<TransactionContext>(); | ||
private readonly bool _shouldHandleConnectionState; | ||
private IDisposable _transactionLifetimeLogger; | ||
|
||
public TransactionContext(IDbConnection connection) | ||
{ | ||
Connection = connection; | ||
ConnectionState state = Connection.State; | ||
switch (state) | ||
{ | ||
case ConnectionState.Closed: | ||
_shouldHandleConnectionState = true; | ||
break; | ||
case ConnectionState.Open: | ||
_shouldHandleConnectionState = false; | ||
break; | ||
default: | ||
throw new InvalidOperationException($"A connection provided to the TransactionContext must either be closed or open, but must not be {state}"); | ||
} | ||
} | ||
|
||
public IDbConnection Connection { get; } | ||
|
||
public IDbTransaction CurrentTransaction { get; private set; } | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public void BeginTransaction() | ||
{ | ||
if (_shouldHandleConnectionState) | ||
{ | ||
Connection.Open(); | ||
} | ||
|
||
CurrentTransaction = Connection.BeginTransaction(); | ||
_transactionLifetimeLogger = Logger.DebugDuration("Transaction open"); | ||
} | ||
|
||
public void CommitTransaction() | ||
{ | ||
CurrentTransaction.Commit(); | ||
CurrentTransaction.Dispose(); | ||
CurrentTransaction = null; | ||
_transactionLifetimeLogger?.Dispose(); | ||
_transactionLifetimeLogger = null; | ||
if (_shouldHandleConnectionState) | ||
{ | ||
Connection.Close(); | ||
} | ||
} | ||
|
||
public void RollbackTransaction() | ||
{ | ||
CurrentTransaction.Rollback(); | ||
CurrentTransaction.Dispose(); | ||
CurrentTransaction = null; | ||
_transactionLifetimeLogger?.Dispose(); | ||
_transactionLifetimeLogger = null; | ||
if (_shouldHandleConnectionState) | ||
{ | ||
Connection.Close(); | ||
} | ||
} | ||
|
||
private void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
try | ||
{ | ||
CurrentTransaction?.Dispose(); | ||
CurrentTransaction = null; | ||
if (_shouldHandleConnectionState && Connection.State == ConnectionState.Open) | ||
{ | ||
Connection.Close(); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.Error(ex, "Dispose failed"); | ||
} | ||
|
||
_transactionLifetimeLogger?.Dispose(); | ||
CurrentTransaction?.Dispose(); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/abstractions/Backend.Fx/Patterns/UnitOfWork/DbConnectionDecorator.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,36 @@ | ||
using System.Data; | ||
using System.Security.Principal; | ||
using Backend.Fx.Patterns.DependencyInjection; | ||
|
||
namespace Backend.Fx.Patterns.UnitOfWork | ||
{ | ||
/// <summary> | ||
/// Enriches the unit of work to open and close a database connection during lifetime | ||
/// </summary> | ||
public class DbConnectionDecorator : IUnitOfWork | ||
{ | ||
public DbConnectionDecorator(IDbConnection dbConnection, IUnitOfWork unitOfWork) | ||
{ | ||
DbConnection = dbConnection; | ||
UnitOfWork = unitOfWork; | ||
} | ||
|
||
public IUnitOfWork UnitOfWork { get; } | ||
|
||
public IDbConnection DbConnection { get; } | ||
|
||
public ICurrentTHolder<IIdentity> IdentityHolder => UnitOfWork.IdentityHolder; | ||
|
||
public void Begin() | ||
{ | ||
DbConnection.Open(); | ||
UnitOfWork.Begin(); | ||
} | ||
|
||
public void Complete() | ||
{ | ||
UnitOfWork.Complete(); | ||
DbConnection.Close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.