Skip to content

Commit

Permalink
udpate readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusz96 committed Oct 26, 2024
1 parent 1a80fe9 commit 334eab8
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ You can even define a fallback that will be executed after your entire chain:
var exceptionHandlersChain = new ResponsibilityChain<Exception, bool>(new ActivatorMiddlewareResolver())
.Chain<OutOfMemoryExceptionHandler>() // The order of middleware being chained matters
.Chain<ArgumentExceptionHandler>()
.Finally<FinallyDoSomething>();
.Finally<DoSomething>();

public class FinallyDoSomething : IFinally<Exception, bool>
public class DoSomething : IFinally<Exception, bool>
{
public bool Finally(Exception parameter)
{
Expand All @@ -105,7 +105,7 @@ var result = exceptionHandlersChain.Execute(new InvalidOperationException()); //
```
The result will be true because of the type used in the `Finally` method.

You can also choose to throw an exception in the `Finally` method instead of returning a value:
You can also choose to throw an exception in the finally instead of returning a value:
```C#
var exceptionHandlersChain = new ResponsibilityChain<Exception, bool>(new ActivatorMiddlewareResolver())
.Chain<OutOfMemoryExceptionHandler>()
Expand Down Expand Up @@ -234,27 +234,22 @@ var pipeline = new AsyncPipeline<Bitmap>(new ActivatorMiddlewareResolver())
.Add<AddTransparencyAsyncMiddleware>() // You can mix both kinds of asynchronous middleware
.AddCancellable<AddWatermarkCancellableAsyncMiddleware>();

Bitmap image = (Bitmap) Image.FromFile("party-photo.png");
CancellationToken cancellationToken = CancellationToken.None;

Bitmap image = (Bitmap) Image.FromFile("party-photo.png");
await pipeline.Execute(image, cancellationToken);

public class RoudCornersCancellableAsyncMiddleware : ICancellableAsyncMiddleware<Bitmap>
{
public async Task Run(Bitmap parameter, Func<Bitmap, Task> next, CancellationToken cancellationToken)
{
await RoundCournersAsync(parameter, cancellationToken);
await next(parameter);
}

private async Task RoudCournersAsync(Bitmap bitmap, CancellationToken cancellationToken)
{
// Handle somehow
await Task.CompletedTask;
await next(parameter);
}
}
```
And to pass the cancellation token to your asynchronous chain of responsibility middleware, you can implement the `ICancellableAsyncMiddleware<TParameter, TReturn>` interface
and pass the cancellation token argument to the `IAsynchChainOfResponsibility<TParamete, TReturnr>.Execute` method.
and pass the cancellation token argument to the `IAsynchChainOfResponsibility<TParamete, TReturn>.Execute` method.

## Middleware resolver
You may be wondering what is all this `ActivatorMiddlewareResolver` class being passed to every instance of pipeline and chain of responsibility.
Expand Down Expand Up @@ -325,7 +320,7 @@ public class RoudCornersAsyncMiddleware : IAsyncMiddleware<Bitmap>
}
```

Or instantiate pipeline/chain of responsibility directly:
Or instantiate it directly:
```C#
services.AddMiddlewareFromAssembly(typeof(RoudCornersAsyncMiddleware).Assembly);

Expand Down Expand Up @@ -379,9 +374,9 @@ With:
var exceptionHandlersChain = new ResponsibilityChain<Exception, bool>(new ActivatorMiddlewareResolver())
.Chain<OutOfMemoryExceptionHandler>()
.Chain<ArgumentExceptionHandler>()
.Finally<FinallyDoSomething>();
.Finally<DoSomething>();

public class FinallyDoSomething : IFinally<Exception, bool>
public class DoSomething : IFinally<Exception, bool>
{
public bool Finally(Exception parameter)
{
Expand Down

0 comments on commit 334eab8

Please sign in to comment.