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

Commit

Permalink
Implement async UseUow calls #122
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Jan 8, 2018
1 parent 8057c89 commit 14f7d96
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 1 deletion.
1 change: 1 addition & 0 deletions common.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/Stove/Stove.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp71</s:String></wpf:ResourceDictionary>
206 changes: 205 additions & 1 deletion src/Stove/StoveComponentBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;

using Stove.Domain.Uow;
Expand Down Expand Up @@ -65,7 +67,7 @@ public IUnitOfWorkManager UnitOfWorkManager
/// </summary>
public IMessageBus MessageBus { get; set; }

public void UseUow(Action act)
protected void UseUow(Action act)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin())
{
Expand All @@ -75,6 +77,19 @@ public void UseUow(Action act)
}
}

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 }))
Expand All @@ -85,6 +100,19 @@ protected void UseUow(Action act, IsolationLevel isolation)
}
}

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 }))
Expand All @@ -95,6 +123,19 @@ protected void UseUow(Action act, bool isTransactional)
}
}

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
Expand All @@ -108,6 +149,22 @@ protected void UseUow(Action act, TransactionScopeOption scope)
}
}

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;
}

protected void UseUow(Action act, IsolationLevel isolation, TransactionScopeOption scope)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(new UnitOfWorkOptions
Expand All @@ -122,6 +179,23 @@ protected void UseUow(Action act, IsolationLevel isolation, TransactionScopeOpti
}
}

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

uow.CompleteAsync(cancellationToken);
}

return task;
}

protected void UseUow(Action act, Action<UnitOfWorkOptions> optsAction)
{
var options = new UnitOfWorkOptions();
Expand All @@ -136,6 +210,23 @@ protected void UseUow(Action act, Action<UnitOfWorkOptions> optsAction)
}
}

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

optsAction(options);

Task task;
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(options))
{
task = func();

uow.CompleteAsync(cancellationToken);
}

return task;
}

protected void UseUowIfNot(Action act)
{
if (UnitOfWorkManager.Current == null)
Expand All @@ -153,6 +244,26 @@ protected void UseUowIfNot(Action 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)
Expand All @@ -173,6 +284,29 @@ protected void UseUowIfNot(Action act, bool isTransactional)
}
}

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)
Expand All @@ -193,6 +327,29 @@ protected void UseUowIfNot(Action act, IsolationLevel isolation)
}
}

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)
Expand All @@ -214,6 +371,30 @@ protected void UseUowIfNot(Action act, IsolationLevel isolation, TransactionScop
}
}

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)
{
var options = new UnitOfWorkOptions();
Expand All @@ -234,6 +415,29 @@ protected void UseUowIfNot(Action act, Action<UnitOfWorkOptions> optsAction)
}
}

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

Task task;
if (UnitOfWorkManager.Current == null)
{
using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin(options))
{
task = func();

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

return task;
}

protected void OnUowCompleted(Action action)
{
CurrentUnitOfWork.Completed += (sender, args) =>
Expand Down

0 comments on commit 14f7d96

Please sign in to comment.