Skip to content
This repository was archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Added StoveCommandContextAccessor to StoveComponentBase
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Jan 29, 2018
1 parent 2ff1c38 commit 82d3f5b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 301 deletions.
4 changes: 4 additions & 0 deletions src/Stove/Commands/IStoveCommandContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public interface IStoveCommandContextAccessor
/// <returns></returns>
IDisposable Use(Action<CommandContext> contextCallback);

/// <summary>
/// Changes the current <see cref="CommandContext" />
/// </summary>
/// <param name="contextCallback"></param>
void Manipulate(Action<CommandContext> contextCallback);
}
}
312 changes: 18 additions & 294 deletions src/Stove/StoveComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;

using Stove.Commands;
using Stove.Domain.Uow;
using Stove.Log;
using Stove.MQ;
Expand Down Expand Up @@ -67,140 +67,32 @@ public IUnitOfWorkManager UnitOfWorkManager
/// </summary>
public IMessageBus MessageBus { get; set; }

protected void UseUow(Action act)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin())
{
act();

uow.Complete();
}
}

protected Task UseUow(Func<Task> func, CancellationToken cancellationToken = default)
{
Task task;
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin())
{
task = func();

uow.CompleteAsync(cancellationToken);
}

return task;
}

protected void UseUow(Action act, IsolationLevel isolation)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions { IsolationLevel = isolation }))
{
act();

uow.Complete();
}
}

protected Task UseUow(Func<Task> func, IsolationLevel isolation, CancellationToken cancellationToken = default)
{
Task task;
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions { IsolationLevel = isolation }))
{
task = func();

uow.CompleteAsync(cancellationToken);
}

return task;
}

protected void UseUow(Action act, bool isTransactional)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions { IsTransactional = isTransactional }))
{
act();

uow.Complete();
}
}

protected Task UseUow(Func<Task> func, bool isTransactional, CancellationToken cancellationToken = default)
{
Task task;
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions { IsTransactional = isTransactional }))
{
task = func();

uow.CompleteAsync(cancellationToken);
}

return task;
}

protected void UseUow(Action act, TransactionScopeOption scope)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
Scope = scope
}))
{
act();

uow.Complete();
}
}

protected Task UseUow(Func<Task> func, TransactionScopeOption scope, CancellationToken cancellationToken = default)
{
Task task;
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
Scope = scope
}))
{
task = func();

uow.CompleteAsync(cancellationToken);
}

return task;
}
/// <summary>
/// The <see cref="CommandContext" /> accessor
/// </summary>
public IStoveCommandContextAccessor CommandContextAccessor { get; set; }

protected void UseUow(Action act, IsolationLevel isolation, TransactionScopeOption scope)
protected void CorrelatingBy(Action act, string correlationId)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
IsolationLevel = isolation,
Scope = scope
}))
using (CommandContextAccessor.Use(correlationId))
{
act();

uow.Complete();
}
}

protected Task UseUow(Func<Task> func, IsolationLevel isolation, TransactionScopeOption scope, CancellationToken cancellationToken = default)
protected Task CorrelatingBy(Func<Task> func, string correlationId)
{
Task task;
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
using (CommandContextAccessor.Use(correlationId))
{
IsolationLevel = isolation,
Scope = scope
}))
{
task = func();

uow.CompleteAsync(cancellationToken);
return func();
}

return task;
}

protected void UseUow(Action act, Action<UnitOfWorkOptions> optsAction)
protected void UseUow(Action act, Action<UnitOfWorkOptions> optsCallback = null)
{
var options = new UnitOfWorkOptions();

optsAction(options);
optsCallback?.Invoke(options);

using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(options))
{
Expand All @@ -210,11 +102,11 @@ protected void UseUow(Action act, Action<UnitOfWorkOptions> optsAction)
}
}

protected Task UseUow(Func<Task> func, Action<UnitOfWorkOptions> optsAction, CancellationToken cancellationToken = default)
protected Task UseUow(Func<Task> func, Action<UnitOfWorkOptions> optsAction = null, CancellationToken cancellationToken = default)
{
var options = new UnitOfWorkOptions();

optsAction(options);
optsAction?.Invoke(options);

Task task;
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(options))
Expand All @@ -227,178 +119,10 @@ protected Task UseUow(Func<Task> func, Action<UnitOfWorkOptions> optsAction, Can
return task;
}

protected void UseUowIfNot(Action act)
{
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin())
{
act();

uow.Complete();
}
}
else
{
act();
}
}

protected Task UseUowIfNot(Func<Task> func, CancellationToken cancellationToken = default)
{
Task task;
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin())
{
task = func();

uow.CompleteAsync(cancellationToken);
}
}
else
{
task = func();
}

return task;
}

protected void UseUowIfNot(Action act, bool isTransactional)
{
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
IsTransactional = isTransactional
}))
{
act();

uow.Complete();
}
}
else
{
act();
}
}

protected Task UseUowIfNot(Func<Task> func, bool isTransactional, CancellationToken cancellationToken = default)
{
Task task;
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
IsTransactional = isTransactional
}))
{
task = func();

uow.CompleteAsync(cancellationToken);
}
}
else
{
task = func();
}

return task;
}

protected void UseUowIfNot(Action act, IsolationLevel isolation)
{
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
IsolationLevel = isolation
}))
{
act();

uow.Complete();
}
}
else
{
act();
}
}

protected Task UseUowIfNot(Func<Task> func, IsolationLevel isolation, CancellationToken cancellationToken = default)
{
Task task;
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
IsolationLevel = isolation
}))
{
task = func();

uow.CompleteAsync(cancellationToken);
}
}
else
{
task = func();
}

return task;
}

protected void UseUowIfNot(Action act, IsolationLevel isolation, TransactionScopeOption scope)
{
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
IsolationLevel = isolation,
Scope = scope
}))
{
act();

uow.Complete();
}
}
else
{
act();
}
}

protected Task UseUowIfNot(Func<Task> func, IsolationLevel isolation, TransactionScopeOption scope, CancellationToken cancellationToken = default)
{
Task task;
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
{
IsolationLevel = isolation,
Scope = scope
}))
{
task = func();

uow.CompleteAsync(cancellationToken);
}
}
else
{
task = func();
}

return task;
}

protected void UseUowIfNot(Action act, Action<UnitOfWorkOptions> optsAction)
protected void UseUowIfNot(Action act, Action<UnitOfWorkOptions> optsAction = null)
{
var options = new UnitOfWorkOptions();
optsAction(options);
optsAction?.Invoke(options);

if (UnitOfWorkManager.Current == null)
{
Expand All @@ -415,10 +139,10 @@ protected void UseUowIfNot(Action act, Action<UnitOfWorkOptions> optsAction)
}
}

protected Task UseUowIfNot(Func<Task> func, Action<UnitOfWorkOptions> optsAction, CancellationToken cancellationToken = default)
protected Task UseUowIfNot(Func<Task> func, Action<UnitOfWorkOptions> optsAction = null, CancellationToken cancellationToken = default)
{
var options = new UnitOfWorkOptions();
optsAction(options);
optsAction?.Invoke(options);

Task task;
if (UnitOfWorkManager.Current == null)
Expand Down
3 changes: 3 additions & 0 deletions test/Stove.TestBase/ApplicationTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Autofac.Extras.IocManager;

using Stove.Bootstrapping;
using Stove.Commands;
using Stove.Reflection.Extensions;
using Stove.Runtime.Session;

Expand All @@ -23,6 +24,8 @@ protected ApplicationTestBase(bool autoUnitOfWorkInterceptionEnabled = false)

protected TestStoveSession StoveSession => The<TestStoveSession>();

protected IStoveCommandContextAccessor CommandContextAccessor => The<IStoveCommandContextAccessor>();

protected override void PreBuild()
{
}
Expand Down
Loading

0 comments on commit 82d3f5b

Please sign in to comment.