-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
251 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Sample; | ||
|
||
// [RegisterMiddleware] | ||
public class MyRequestMiddleware : IRequestMiddleware<MyMessageRequest, MyMessageResponse> | ||
{ | ||
public async Task<MyMessageResponse> Process(MyMessageRequest request, Func<Task<MyMessageResponse>> next, CancellationToken cancellationToken) | ||
{ | ||
// BEFORE - If connected - pull from API after save to cache ELSE pull from cache | ||
var result = await next(); | ||
// AFTER = cache | ||
return result; | ||
} | ||
} | ||
|
||
public class CatchAllRequestMiddleware : IRequestMiddleware<IRequest<object>, object> | ||
{ | ||
public Task<object> Process(IRequest<object> request, Func<Task<object>> next, CancellationToken cancellationToken) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
namespace Shiny.Mediator; | ||
|
||
public interface IEvent | ||
{ | ||
|
||
} | ||
public interface IEvent { } | ||
|
||
/// <summary> | ||
/// This is a good base type if you want to make use of covariance in your handlers | ||
/// </summary> | ||
public record Event : IEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace Shiny.Mediator; | ||
|
||
public interface IRequest | ||
public interface IRequest : IRequest<Unit> | ||
{ | ||
} | ||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
namespace Shiny.Mediator; | ||
|
||
public interface IRequestHandler<in TRequest> where TRequest : notnull, IRequest | ||
public interface IRequestHandler<TRequest> where TRequest : notnull, IRequest | ||
{ | ||
Task Handle(TRequest request, CancellationToken cancellationToken); | ||
} | ||
|
||
|
||
public interface IRequestHandler<in TRequest, TResult> where TRequest : notnull, IRequest<TResult> | ||
public interface IRequestHandler<TRequest, TResult> where TRequest : notnull, IRequest<TResult> | ||
{ | ||
Task<TResult> Handle(TRequest request, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
// using Shiny.Mediator; | ||
// | ||
// // TODO: how do I register an "ALL" middleware | ||
// | ||
// // TODO: execution duration timer | ||
// | ||
// // TODO: catch all could be IRequest or IRequest<T>? Could use an IRequest<Void>? | ||
// public interface IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
// { | ||
// // intercept with connectivity, if offline go to cache, if online go to remote | ||
// // if went to remote, post execute stores to cache | ||
// Task<TResult> Process(TRequest request, IRequestMiddleware<TRequest, TResult> next, CancellationToken cancellationToken); | ||
// } | ||
using Shiny.Mediator; | ||
|
||
|
||
public interface IRequestMiddleware<in TRequest, TResult> where TRequest : IRequest<TResult> | ||
{ | ||
Task<TResult> Process(TRequest request, Func<Task<TResult>> next, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Shiny.Mediator/Middleware/ExceptionHandlerMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Shiny.Mediator.Middleware; | ||
|
||
public class ExceptionHandlerMiddleware<TRequest, TResult> : IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
{ | ||
public async Task<TResult> Process(TRequest request, Func<Task<TResult>> next, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
return await next(); | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
// TODO: log, trap? | ||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
// using System.Diagnostics; | ||
// | ||
// namespace Shiny.Mediator.Middleware; | ||
// | ||
// public class TimedMiddleware<TRequest, TResult> : IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
// { | ||
// public async Task<TResult> Process(TRequest request, IRequestMiddleware<TRequest, TResult> next, CancellationToken cancellationToken) | ||
// { | ||
// var sw = new Stopwatch(); | ||
// sw.Start(); | ||
// var result = await next(request, cancellationToken); | ||
// sw.Stop(); | ||
// | ||
// // TODO: alert on long? | ||
// return result; | ||
// } | ||
// } | ||
using System.Diagnostics; | ||
|
||
namespace Shiny.Mediator.Middleware; | ||
|
||
public class TimedMiddleware<TRequest, TResult> : IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
{ | ||
public async Task<TResult> Process(TRequest request, Func<Task<TResult>> next, CancellationToken cancellationToken) | ||
{ | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
var result = await next(); | ||
sw.Stop(); | ||
|
||
// TODO: alert on long? | ||
return result; | ||
} | ||
} | ||
|
Oops, something went wrong.