Skip to content

Commit

Permalink
Merge pull request #92 from marcwittke/develop
Browse files Browse the repository at this point in the history
version 6.1.0
  • Loading branch information
marcwittke committed Dec 23, 2019
2 parents d4a91ca + 0c94286 commit fb5bc06
Show file tree
Hide file tree
Showing 74 changed files with 993 additions and 2,244 deletions.
7 changes: 0 additions & 7 deletions Backend.Fx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Backend.Fx.InMemoryPersiste
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Backend.Fx.Log4NetLogging", "src\implementations\Backend.Fx.Log4NetLogging\Backend.Fx.Log4NetLogging.csproj", "{C27BA4CE-882B-405F-906E-4DFA6E9F1216}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Backend.Fx.SqlServer", "src\implementations\Backend.Fx.SqlServer\Backend.Fx.SqlServer.csproj", "{BEDC2709-7B9C-4BE3-97F3-C56D593C8C15}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -105,10 +103,6 @@ Global
{C27BA4CE-882B-405F-906E-4DFA6E9F1216}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C27BA4CE-882B-405F-906E-4DFA6E9F1216}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C27BA4CE-882B-405F-906E-4DFA6E9F1216}.Release|Any CPU.Build.0 = Release|Any CPU
{BEDC2709-7B9C-4BE3-97F3-C56D593C8C15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEDC2709-7B9C-4BE3-97F3-C56D593C8C15}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEDC2709-7B9C-4BE3-97F3-C56D593C8C15}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEDC2709-7B9C-4BE3-97F3-C56D593C8C15}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -130,7 +124,6 @@ Global
{45EC5987-1C85-4940-8E5E-3B4F0FA90AF8} = {56ACAE69-F7F0-4FF2-BEE6-4B079481CF9A}
{0B8F13CA-1347-4655-9D41-AED21B1AFAC4} = {739A7296-579F-4D9A-BC73-DCECD260D7A0}
{C27BA4CE-882B-405F-906E-4DFA6E9F1216} = {739A7296-579F-4D9A-BC73-DCECD260D7A0}
{BEDC2709-7B9C-4BE3-97F3-C56D593C8C15} = {739A7296-579F-4D9A-BC73-DCECD260D7A0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {45648557-C751-44AD-9C87-0F12EB673969}
Expand Down
7 changes: 4 additions & 3 deletions src/abstractions/Backend.Fx/Backend.Fx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2018.2.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.CSharp" Version="4.6.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
<PackageReference Include="System.Data.Common" Version="4.1.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.8.16" />
<PackageReference Include="System.Linq.Queryable" Version="4.3.0" />
<PackageReference Include="System.Security.Principal" Version="4.3.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19324-01" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19324-01" PrivateAssets="All" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions src/abstractions/Backend.Fx/Extensions/DelegateDisposable.cs
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();
}
}
}
8 changes: 4 additions & 4 deletions src/abstractions/Backend.Fx/Logging/DebugLoggerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Backend.Fx.Logging
{
using System.Diagnostics;
using System.Diagnostics;

namespace Backend.Fx.Logging
{
[DebuggerStepThrough]
public class DebugLoggerFactory : ILoggerFactory
{
Expand All @@ -12,7 +12,7 @@ public ILogger Create(string s)

public void BeginActivity(int activityIndex)
{
System.Diagnostics.Debug.WriteLine($"Beginning activity {activityIndex}");
Debug.WriteLine($"Beginning activity {activityIndex}");
}

public void Shutdown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public IDisposable BeginScope(IIdentity identity = null, TenantId tenantId = nul

return new MultipleDisposable(scope, scopeDurationLogger);
}

public void Run<TJob>() where TJob : class, IJob
{
var tenantIds = TenantIdService.GetActiveTenantIds();
Expand All @@ -91,23 +91,21 @@ public void Invoke(Action action, IIdentity identity, TenantId tenantId)
{
using (BeginScope(new SystemIdentity(), tenantId))
{
using (var unitOfWork = CompositionRoot.GetInstance<IUnitOfWork>())
var unitOfWork = CompositionRoot.GetInstance<IUnitOfWork>();
try
{
unitOfWork.Begin();
action.Invoke();
unitOfWork.Complete();
}
catch (TargetInvocationException ex)
{
ExceptionLogger.LogException(ex.InnerException ?? ex);
}
catch (Exception ex)
{
try
{
unitOfWork.Begin();
action.Invoke();
unitOfWork.Complete();
}
catch (TargetInvocationException ex)
{
ExceptionLogger.LogException(ex.InnerException ?? ex);
}
catch (Exception ex)
{
Logger.Info(ex);
ExceptionLogger.LogException(ex);
}
Logger.Info(ex);
ExceptionLogger.LogException(ex);
}
}
}
Expand All @@ -116,23 +114,21 @@ public async Task InvokeAsync(Func<Task> awaitableAsyncAction, IIdentity identit
{
using (BeginScope(new SystemIdentity(), tenantId))
{
using (var unitOfWork = CompositionRoot.GetInstance<IUnitOfWork>())
var unitOfWork = CompositionRoot.GetInstance<IUnitOfWork>();
try
{
unitOfWork.Begin();
await awaitableAsyncAction.Invoke();
unitOfWork.Complete();
}
catch (TargetInvocationException ex)
{
ExceptionLogger.LogException(ex.InnerException ?? ex);
}
catch (Exception ex)
{
try
{
unitOfWork.Begin();
await awaitableAsyncAction.Invoke();
unitOfWork.Complete();
}
catch (TargetInvocationException ex)
{
ExceptionLogger.LogException(ex.InnerException ?? ex);
}
catch (Exception ex)
{
Logger.Info(ex);
ExceptionLogger.LogException(ex);
}
Logger.Info(ex);
ExceptionLogger.LogException(ex);
}
}
}
Expand Down
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; }
}
}
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;
}
}
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;
}
}
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();
}
}
}
}
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();
}
}
}

0 comments on commit fb5bc06

Please sign in to comment.