Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#230 Add support for async on before, on after and on exception operations #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ $RECYCLE.BIN/

# Mac desktop service store files
.DS_Store
**/.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected internal sealed override T WrapSync<T>(Func<object[], T> target, objec

try
{
var result = base.WrapSync(target, args, eventArgs);
T result = base.WrapSync(target, args, eventArgs);
OnAfter(eventArgs);

return result;
Expand All @@ -23,34 +23,49 @@ protected internal sealed override T WrapSync<T>(Func<object[], T> target, objec
}
}

protected virtual void OnBefore(AspectEventArgs eventArgs)
{
}

protected virtual void OnAfter(AspectEventArgs eventArgs)
{
}

protected virtual T OnException<T>(AspectEventArgs eventArgs, Exception exception)
{
throw exception;
}

protected internal sealed override async Task<T> WrapAsync<T>(Func<object[], Task<T>> target, object[] args, AspectEventArgs eventArgs)
{
OnBefore(eventArgs);
await OnBeforeAsync(eventArgs);

try
{
var result = await target(args);
OnAfter(eventArgs);
T result = await base.WrapAsync(target, args, eventArgs);
await OnAfterAsync(eventArgs);

return result;
}
catch (Exception exception)
{
return OnException<T>(eventArgs, exception);
return await OnExceptionAsync<T>(eventArgs, exception);
}
}

protected virtual void OnBefore(AspectEventArgs eventArgs)
protected virtual Task OnBeforeAsync(AspectEventArgs eventArgs)
{
return Task.CompletedTask;
}

protected virtual void OnAfter(AspectEventArgs eventArgs)
protected virtual Task OnAfterAsync(AspectEventArgs eventArgs)
{
return Task.CompletedTask;
}

protected virtual T OnException<T>(AspectEventArgs eventArgs, Exception exception)
protected virtual Task<T> OnExceptionAsync<T>(AspectEventArgs eventArgs, Exception exception)
{
throw exception;
}
}
}
}
Loading