Skip to content

Commit

Permalink
Merge branch 'releases/5.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
cd21h committed Jan 24, 2020
2 parents ffa9b67 + 1001550 commit 9e7016f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Samples/SharpArch.WebApi/App/Stubs/TransactionManagerStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace SharpArch.WebApi.Stubs
{
public class TransactionManagerStub : ITransactionManager, IDisposable
public class TransactionManagerStub : ITransactionManager, IDisposable, ISupportsTransactionStatus
{
public const string TransactionIsolationLevel = "x-transaction-isolation-level";
public const string TransactionState = "x-transaction-result";
Expand Down Expand Up @@ -74,5 +74,8 @@ public void Dispose()
{
_transaction?.Dispose();
}

/// <inheritdoc />
public bool IsActive => true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SharpArch.Domain.PersistenceSupport
{
using JetBrains.Annotations;


/// <summary>
/// Returns transaction status.
/// </summary>
[PublicAPI]
public interface ISupportsTransactionStatus
{
/// <summary>
/// Checks whether transaction is active or not.
/// </summary>
bool IsActive { get; }
}
}
6 changes: 5 additions & 1 deletion Src/SharpArch.NHibernate/TransactionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Domain.PersistenceSupport;
using global::NHibernate;
using JetBrains.Annotations;

Expand All @@ -12,7 +13,7 @@
/// Transaction manager for NHibernate.
/// </summary>
[PublicAPI]
public class TransactionManager : INHibernateTransactionManager
public class TransactionManager : INHibernateTransactionManager, ISupportsTransactionStatus
{
/// <inheritdoc />
[NotNull]
Expand Down Expand Up @@ -44,5 +45,8 @@ public TransactionManager([NotNull] ISession session)

/// <inheritdoc />
public void FlushChanges() => Session.Flush();

/// <inheritdoc />
public bool IsActive => Session.Transaction.IsActive;
}
}
5 changes: 4 additions & 1 deletion Src/SharpArch.RavenDb/TransactionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace SharpArch.RavenDb
/// </remarks>
/// <seealso cref="SharpArch.Domain.PersistenceSupport.ITransactionManager" />
[PublicAPI]
public class TransactionManager : ITransactionManager
public class TransactionManager : ITransactionManager, ISupportsTransactionStatus
{
[NotNull] readonly IDocumentSession _session;

Expand Down Expand Up @@ -98,5 +98,8 @@ void ClearTransaction()
_transaction?.Dispose();
_transaction = null;
}

/// <inheritdoc />
public bool IsActive => _transaction != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

<ItemGroup>
<ProjectReference Include="..\SharpArch.Domain\SharpArch.Domain.csproj" />
<ProjectReference Include="..\SharpArch.Infrastructure\SharpArch.Infrastructure.csproj" />
</ItemGroup>

</Project>
20 changes: 19 additions & 1 deletion Src/SharpArch.Web.AspNetCore/Transaction/UnitOfWorkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

namespace SharpArch.AspNetCore.Transaction
{
using Infrastructure.Logging;
using Microsoft.AspNetCore.Http;


/// <summary>
/// Wraps controller actions marked with <see cref="TransactionAttribute" /> into transaction.
/// </summary>
Expand All @@ -15,10 +19,12 @@ namespace SharpArch.AspNetCore.Transaction
[PublicAPI]
public class UnitOfWorkHandler : ApplyTransactionFilterBase
{
static readonly ILog Log = LogProvider.For<UnitOfWorkHandler>();

/// <summary>
/// HttpContext key for Transaction Manger.
/// </summary>
private const string TransactionManagerKey = "SharpArch.AspNetCore.UnitOfWork.TransactionManager";
const string TransactionManagerKey = "SharpArch.AspNetCore.UnitOfWork.TransactionManager";

/// <inheritdoc />
public override void OnActionExecuting(ActionExecutingContext context)
Expand All @@ -33,6 +39,9 @@ public override void OnActionExecuting(ActionExecutingContext context)
}

/// <inheritdoc />
/// <exception cref="T:System.InvalidOperationException">ITransactionManger was not found in HttpContext.
/// In can happen if third-party overwritten <see cref="HttpContext.Items"/>.
/// </exception>
public override void OnActionExecuted(ActionExecutedContext context)
{
var transactionAttribute = GetTransactionAttribute(context);
Expand All @@ -43,6 +52,15 @@ public override void OnActionExecuted(ActionExecutedContext context)
throw new InvalidOperationException(nameof(ITransactionManager) +
" was not found in HttpContext. Please contact SharpArch dev team.");

if (transactionManager is ISupportsTransactionStatus tranStatus)
{
if (!tranStatus.IsActive)
{
Log.Debug("Transaction is already closed");
return;
}
}

if (context.Exception != null || transactionAttribute.RollbackOnModelValidationError && context.ModelState.IsValid == false)
transactionManager.RollbackTransaction();
else
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ install:
- ps: ./mssql-setup.ps1

build_script:
- ps: ./build.ps1
- ps: ./build.ps1 -Verbosity Verbose

test: off

Expand Down

0 comments on commit 9e7016f

Please sign in to comment.