Browse Source

Exception fixes.

pull/249/head
Sebastian Stehle 9 years ago
parent
commit
f9626bd512
  1. 13
      src/Squidex.Infrastructure/Commands/DomainObjectGrain.cs
  2. 16
      src/Squidex.Infrastructure/Tasks/PartitionedActionBlock.cs
  3. 38
      src/Squidex.Infrastructure/Tasks/TaskExtensions.cs

13
src/Squidex.Infrastructure/Commands/DomainObjectGrain.cs

@ -14,6 +14,7 @@ using Orleans.Runtime;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.Orleans;
using Squidex.Infrastructure.States;
using Squidex.Infrastructure.Tasks;
namespace Squidex.Infrastructure.Commands
{
@ -108,17 +109,17 @@ namespace Squidex.Infrastructure.Commands
protected Task<object> CreateReturnAsync<TCommand>(TCommand command, Func<TCommand, object> handler) where TCommand : class, IAggregateCommand
{
return InvokeAsync(command, x => Task.FromResult(handler(x)), false);
return InvokeAsync(command, handler?.ToAsync(), false);
}
protected Task<object> CreateAsync<TCommand>(TCommand command, Func<TCommand, Task> handler) where TCommand : class, IAggregateCommand
{
return InvokeAsync(command, x => handler(x).ContinueWith<object>(t => null), false);
return InvokeAsync(command, handler.ToDefault<TCommand, object>(), false);
}
protected Task<object> CreateAsync<TCommand>(TCommand command, Action<TCommand> handler) where TCommand : class, IAggregateCommand
{
return InvokeAsync(command, x => { handler(x); return Task.FromResult<object>(null); }, false);
return InvokeAsync(command, handler?.ToDefault<TCommand, object>()?.ToAsync(), false);
}
protected Task<object> UpdateReturnAsync<TCommand>(TCommand command, Func<TCommand, Task<object>> handler) where TCommand : class, IAggregateCommand
@ -128,17 +129,17 @@ namespace Squidex.Infrastructure.Commands
protected Task<object> UpdateReturnAsync<TCommand>(TCommand command, Func<TCommand, object> handler) where TCommand : class, IAggregateCommand
{
return InvokeAsync(command, x => Task.FromResult(handler(x)), true);
return InvokeAsync(command, handler?.ToAsync(), true);
}
protected Task<object> UpdateAsync<TCommand>(TCommand command, Func<TCommand, Task> handler) where TCommand : class, IAggregateCommand
{
return InvokeAsync(command, x => handler(x).ContinueWith<object>(t => null), true);
return InvokeAsync(command, handler?.ToDefault<TCommand, object>(), true);
}
protected Task<object> UpdateAsync<TCommand>(TCommand command, Action<TCommand> handler) where TCommand : class, IAggregateCommand
{
return InvokeAsync(command, x => { handler(x); return Task.FromResult<object>(null); }, true);
return InvokeAsync(command, handler?.ToDefault<TCommand, object>()?.ToAsync(), true);
}
private async Task<object> InvokeAsync<TCommand>(TCommand command, Func<TCommand, Task<object>> handler, bool isUpdate) where TCommand : class, IAggregateCommand

16
src/Squidex.Infrastructure/Tasks/PartitionedActionBlock.cs

@ -24,7 +24,7 @@ namespace Squidex.Infrastructure.Tasks
}
public PartitionedActionBlock(Action<TInput> action, Func<TInput, int> partitioner)
: this (ToAsync(action), partitioner, new ExecutionDataflowBlockOptions())
: this (action?.ToAsync(), partitioner, new ExecutionDataflowBlockOptions())
{
}
@ -34,7 +34,7 @@ namespace Squidex.Infrastructure.Tasks
}
public PartitionedActionBlock(Action<TInput> action, Func<TInput, int> partitioner, ExecutionDataflowBlockOptions dataflowBlockOptions)
: this(ToAsync(action), partitioner, dataflowBlockOptions)
: this(action?.ToAsync(), partitioner, dataflowBlockOptions)
{
}
@ -94,17 +94,5 @@ namespace Squidex.Infrastructure.Tasks
{
distributor.Fault(exception);
}
private static Func<TInput, Task> ToAsync(Action<TInput> action)
{
Guard.NotNull(action, nameof(action));
return x =>
{
action(x);
return TaskHelper.Done;
};
}
}
}

38
src/Squidex.Infrastructure/Tasks/TaskExtensions.cs

@ -16,7 +16,43 @@ namespace Squidex.Infrastructure.Tasks
{
}
public static Func<T, Task> ToAsync<T>(this Action<T> action)
public static Func<TInput, TOutput> ToDefault<TInput, TOutput>(this Action<TInput> action)
{
Guard.NotNull(action, nameof(action));
return x =>
{
action(x);
return default(TOutput);
};
}
public static Func<TInput, Task<TOutput>> ToDefault<TInput, TOutput>(this Func<TInput, Task> action)
{
Guard.NotNull(action, nameof(action));
return async x =>
{
await action(x);
return default(TOutput);
};
}
public static Func<TInput, Task<TOutput>> ToAsync<TInput, TOutput>(this Func<TInput, TOutput> action)
{
Guard.NotNull(action, nameof(action));
return x =>
{
var result = action(x);
return Task.FromResult(result);
};
}
public static Func<TInput, Task> ToAsync<TInput>(this Action<TInput> action)
{
return x =>
{

Loading…
Cancel
Save