Skip to content

Commit 1e978f5

Browse files
author
Vadim Belov
committed
Refactor: move MediatR types to EasyExtensions.Mediator
All MediatR interfaces, pipeline, and wrapper classes are moved to the new EasyExtensions.Mediator namespace and sub-namespaces. The NotificationHandlerExecutor record is replaced with a class. ServiceRegistrar is refactored for clarity and improved generic handler registration logic. Code formatting and documentation are updated for consistency. No functional changes are introduced; this is a structural and organizational refactor for maintainability.
1 parent 5a891f6 commit 1e978f5

24 files changed

Lines changed: 1054 additions & 1024 deletions
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
namespace MediatR;
2-
3-
/// <summary>
4-
/// Defines a mediator to encapsulate request/response and publishing interaction patterns
5-
/// </summary>
6-
public interface IMediator : ISender, IPublisher
1+
namespace EasyExtensions.Mediator
72
{
3+
/// <summary>
4+
/// Defines a mediator to encapsulate request/response and publishing interaction patterns
5+
/// </summary>
6+
public interface IMediator : ISender, IPublisher
7+
{
8+
}
89
}
Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33

4-
namespace MediatR;
5-
6-
/// <summary>
7-
/// Defines a handler for a notification
8-
/// </summary>
9-
/// <typeparam name="TNotification">The type of notification being handled</typeparam>
10-
public interface INotificationHandler<in TNotification>
11-
where TNotification : INotification
4+
namespace EasyExtensions.Mediator
125
{
136
/// <summary>
14-
/// Handles a notification
7+
/// Defines a handler for a notification
158
/// </summary>
16-
/// <param name="notification">The notification</param>
17-
/// <param name="cancellationToken">Cancellation token</param>
18-
Task Handle(TNotification notification, CancellationToken cancellationToken);
19-
}
20-
21-
/// <summary>
22-
/// Wrapper class for a synchronous notification handler
23-
/// </summary>
24-
/// <typeparam name="TNotification">The notification type</typeparam>
25-
public abstract class NotificationHandler<TNotification> : INotificationHandler<TNotification>
9+
/// <typeparam name="TNotification">The type of notification being handled</typeparam>
10+
public interface INotificationHandler<in TNotification>
2611
where TNotification : INotification
27-
{
28-
Task INotificationHandler<TNotification>.Handle(TNotification notification, CancellationToken cancellationToken)
2912
{
30-
Handle(notification);
31-
32-
return Task.CompletedTask;
13+
/// <summary>
14+
/// Handles a notification
15+
/// </summary>
16+
/// <param name="notification">The notification</param>
17+
/// <param name="cancellationToken">Cancellation token</param>
18+
Task Handle(TNotification notification, CancellationToken cancellationToken);
3319
}
3420

3521
/// <summary>
36-
/// Override in a derived class for the handler logic
22+
/// Wrapper class for a synchronous notification handler
3723
/// </summary>
38-
/// <param name="notification">Notification</param>
39-
protected abstract void Handle(TNotification notification);
24+
/// <typeparam name="TNotification">The notification type</typeparam>
25+
public abstract class NotificationHandler<TNotification> : INotificationHandler<TNotification>
26+
where TNotification : INotification
27+
{
28+
Task INotificationHandler<TNotification>.Handle(TNotification notification, CancellationToken cancellationToken)
29+
{
30+
Handle(notification);
31+
32+
return Task.CompletedTask;
33+
}
34+
35+
/// <summary>
36+
/// Override in a derived class for the handler logic
37+
/// </summary>
38+
/// <param name="notification">Notification</param>
39+
protected abstract void Handle(TNotification notification);
40+
}
4041
}

Sources/EasyExtensions.Mediator/INotificationPublisher.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
using System.Threading.Tasks;
33
using System.Threading;
44

5-
namespace MediatR;
6-
7-
public interface INotificationPublisher
5+
namespace EasyExtensions.Mediator
86
{
9-
Task Publish(IEnumerable<NotificationHandlerExecutor> handlerExecutors, INotification notification,
10-
CancellationToken cancellationToken);
7+
public interface INotificationPublisher
8+
{
9+
Task Publish(IEnumerable<NotificationHandlerExecutor> handlerExecutors, INotification notification,
10+
CancellationToken cancellationToken);
11+
}
1112
}
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
2-
namespace MediatR;
3-
41
using System.Threading;
52
using System.Threading.Tasks;
63

7-
/// <summary>
8-
/// Represents an async continuation for the next task to execute in the pipeline
9-
/// </summary>
10-
/// <typeparam name="TResponse">Response type</typeparam>
11-
/// <returns>Awaitable task returning a <typeparamref name="TResponse"/></returns>
12-
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>(CancellationToken t = default);
13-
14-
/// <summary>
15-
/// Pipeline behavior to surround the inner handler.
16-
/// Implementations add additional behavior and await the next delegate.
17-
/// </summary>
18-
/// <typeparam name="TRequest">Request type</typeparam>
19-
/// <typeparam name="TResponse">Response type</typeparam>
20-
public interface IPipelineBehavior<in TRequest, TResponse> where TRequest : notnull
4+
namespace EasyExtensions.Mediator
215
{
226
/// <summary>
23-
/// Pipeline handler. Perform any additional behavior and await the <paramref name="next"/> delegate as necessary
7+
/// Represents an async continuation for the next task to execute in the pipeline
8+
/// </summary>
9+
/// <typeparam name="TResponse">Response type</typeparam>
10+
/// <returns>Awaitable task returning a <typeparamref name="TResponse"/></returns>
11+
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>(CancellationToken t = default);
12+
13+
/// <summary>
14+
/// Pipeline behavior to surround the inner handler.
15+
/// Implementations add additional behavior and await the next delegate.
2416
/// </summary>
25-
/// <param name="request">Incoming request</param>
26-
/// <param name="next">Awaitable delegate for the next action in the pipeline. Eventually this delegate represents the handler.</param>
27-
/// <param name="cancellationToken">Cancellation token</param>
28-
/// <returns>Awaitable task returning the <typeparamref name="TResponse"/></returns>
29-
Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken);
17+
/// <typeparam name="TRequest">Request type</typeparam>
18+
/// <typeparam name="TResponse">Response type</typeparam>
19+
public interface IPipelineBehavior<in TRequest, TResponse> where TRequest : notnull
20+
{
21+
/// <summary>
22+
/// Pipeline handler. Perform any additional behavior and await the <paramref name="next"/> delegate as necessary
23+
/// </summary>
24+
/// <param name="request">Incoming request</param>
25+
/// <param name="next">Awaitable delegate for the next action in the pipeline. Eventually this delegate represents the handler.</param>
26+
/// <param name="cancellationToken">Cancellation token</param>
27+
/// <returns>Awaitable task returning the <typeparamref name="TResponse"/></returns>
28+
Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken);
29+
}
3030
}
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33

4-
namespace MediatR;
5-
6-
/// <summary>
7-
/// Publish a notification or event through the mediator pipeline to be handled by multiple handlers.
8-
/// </summary>
9-
public interface IPublisher
4+
namespace EasyExtensions.Mediator
105
{
116
/// <summary>
12-
/// Asynchronously send a notification to multiple handlers
7+
/// Publish a notification or event through the mediator pipeline to be handled by multiple handlers.
138
/// </summary>
14-
/// <param name="notification">Notification object</param>
15-
/// <param name="cancellationToken">Optional cancellation token</param>
16-
/// <returns>A task that represents the publish operation.</returns>
17-
Task Publish(object notification, CancellationToken cancellationToken = default);
9+
public interface IPublisher
10+
{
11+
/// <summary>
12+
/// Asynchronously send a notification to multiple handlers
13+
/// </summary>
14+
/// <param name="notification">Notification object</param>
15+
/// <param name="cancellationToken">Optional cancellation token</param>
16+
/// <returns>A task that represents the publish operation.</returns>
17+
Task Publish(object notification, CancellationToken cancellationToken = default);
1818

19-
/// <summary>
20-
/// Asynchronously send a notification to multiple handlers
21-
/// </summary>
22-
/// <param name="notification">Notification object</param>
23-
/// <param name="cancellationToken">Optional cancellation token</param>
24-
/// <returns>A task that represents the publish operation.</returns>
25-
Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
26-
where TNotification : INotification;
19+
/// <summary>
20+
/// Asynchronously send a notification to multiple handlers
21+
/// </summary>
22+
/// <param name="notification">Notification object</param>
23+
/// <param name="cancellationToken">Optional cancellation token</param>
24+
/// <returns>A task that represents the publish operation.</returns>
25+
Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
26+
where TNotification : INotification;
27+
}
2728
}
Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33

4-
namespace MediatR;
5-
6-
/// <summary>
7-
/// Defines a handler for a request
8-
/// </summary>
9-
/// <typeparam name="TRequest">The type of request being handled</typeparam>
10-
/// <typeparam name="TResponse">The type of response from the handler</typeparam>
11-
public interface IRequestHandler<in TRequest, TResponse>
12-
where TRequest : IRequest<TResponse>
4+
namespace EasyExtensions.Mediator
135
{
146
/// <summary>
15-
/// Handles a request
7+
/// Defines a handler for a request
168
/// </summary>
17-
/// <param name="request">The request</param>
18-
/// <param name="cancellationToken">Cancellation token</param>
19-
/// <returns>Response from the request</returns>
20-
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
21-
}
9+
/// <typeparam name="TRequest">The type of request being handled</typeparam>
10+
/// <typeparam name="TResponse">The type of response from the handler</typeparam>
11+
public interface IRequestHandler<in TRequest, TResponse>
12+
where TRequest : IRequest<TResponse>
13+
{
14+
/// <summary>
15+
/// Handles a request
16+
/// </summary>
17+
/// <param name="request">The request</param>
18+
/// <param name="cancellationToken">Cancellation token</param>
19+
/// <returns>Response from the request</returns>
20+
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
21+
}
2222

23-
/// <summary>
24-
/// Defines a handler for a request with a void response.
25-
/// </summary>
26-
/// <typeparam name="TRequest">The type of request being handled</typeparam>
27-
public interface IRequestHandler<in TRequest>
28-
where TRequest : IRequest
29-
{
3023
/// <summary>
31-
/// Handles a request
24+
/// Defines a handler for a request with a void response.
3225
/// </summary>
33-
/// <param name="request">The request</param>
34-
/// <param name="cancellationToken">Cancellation token</param>
35-
/// <returns>Response from the request</returns>
36-
Task Handle(TRequest request, CancellationToken cancellationToken);
37-
}
26+
/// <typeparam name="TRequest">The type of request being handled</typeparam>
27+
public interface IRequestHandler<in TRequest>
28+
where TRequest : IRequest
29+
{
30+
/// <summary>
31+
/// Handles a request
32+
/// </summary>
33+
/// <param name="request">The request</param>
34+
/// <param name="cancellationToken">Cancellation token</param>
35+
/// <returns>Response from the request</returns>
36+
Task Handle(TRequest request, CancellationToken cancellationToken);
37+
}
38+
}

Sources/EasyExtensions.Mediator/ISender.cs

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,54 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44

5-
namespace MediatR;
6-
7-
/// <summary>
8-
/// Send a request through the mediator pipeline to be handled by a single handler.
9-
/// </summary>
10-
public interface ISender
5+
namespace EasyExtensions.Mediator
116
{
127
/// <summary>
13-
/// Asynchronously send a request to a single handler
8+
/// Send a request through the mediator pipeline to be handled by a single handler.
149
/// </summary>
15-
/// <typeparam name="TResponse">Response type</typeparam>
16-
/// <param name="request">Request object</param>
17-
/// <param name="cancellationToken">Optional cancellation token</param>
18-
/// <returns>A task that represents the send operation. The task result contains the handler response</returns>
19-
Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default);
10+
public interface ISender
11+
{
12+
/// <summary>
13+
/// Asynchronously send a request to a single handler
14+
/// </summary>
15+
/// <typeparam name="TResponse">Response type</typeparam>
16+
/// <param name="request">Request object</param>
17+
/// <param name="cancellationToken">Optional cancellation token</param>
18+
/// <returns>A task that represents the send operation. The task result contains the handler response</returns>
19+
Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default);
2020

21-
/// <summary>
22-
/// Asynchronously send a request to a single handler with no response
23-
/// </summary>
24-
/// <param name="request">Request object</param>
25-
/// <param name="cancellationToken">Optional cancellation token</param>
26-
/// <returns>A task that represents the send operation.</returns>
27-
Task Send<TRequest>(TRequest request, CancellationToken cancellationToken = default)
28-
where TRequest : IRequest;
21+
/// <summary>
22+
/// Asynchronously send a request to a single handler with no response
23+
/// </summary>
24+
/// <param name="request">Request object</param>
25+
/// <param name="cancellationToken">Optional cancellation token</param>
26+
/// <returns>A task that represents the send operation.</returns>
27+
Task Send<TRequest>(TRequest request, CancellationToken cancellationToken = default)
28+
where TRequest : IRequest;
2929

30-
/// <summary>
31-
/// Asynchronously send an object request to a single handler via dynamic dispatch
32-
/// </summary>
33-
/// <param name="request">Request object</param>
34-
/// <param name="cancellationToken">Optional cancellation token</param>
35-
/// <returns>A task that represents the send operation. The task result contains the type erased handler response</returns>
36-
Task<object?> Send(object request, CancellationToken cancellationToken = default);
30+
/// <summary>
31+
/// Asynchronously send an object request to a single handler via dynamic dispatch
32+
/// </summary>
33+
/// <param name="request">Request object</param>
34+
/// <param name="cancellationToken">Optional cancellation token</param>
35+
/// <returns>A task that represents the send operation. The task result contains the type erased handler response</returns>
36+
Task<object?> Send(object request, CancellationToken cancellationToken = default);
3737

38-
/// <summary>
39-
/// Create a stream via a single stream handler
40-
/// </summary>
41-
/// <typeparam name="TResponse"></typeparam>
42-
/// <param name="request"></param>
43-
/// <param name="cancellationToken"></param>
44-
/// <returns></returns>
45-
IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TResponse> request, CancellationToken cancellationToken = default);
38+
/// <summary>
39+
/// Create a stream via a single stream handler
40+
/// </summary>
41+
/// <typeparam name="TResponse"></typeparam>
42+
/// <param name="request"></param>
43+
/// <param name="cancellationToken"></param>
44+
/// <returns></returns>
45+
IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TResponse> request, CancellationToken cancellationToken = default);
4646

47-
/// <summary>
48-
/// Create a stream via an object request to a stream handler
49-
/// </summary>
50-
/// <param name="request"></param>
51-
/// <param name="cancellationToken"></param>
52-
/// <returns></returns>
53-
IAsyncEnumerable<object?> CreateStream(object request, CancellationToken cancellationToken = default);
47+
/// <summary>
48+
/// Create a stream via an object request to a stream handler
49+
/// </summary>
50+
/// <param name="request"></param>
51+
/// <param name="cancellationToken"></param>
52+
/// <returns></returns>
53+
IAsyncEnumerable<object?> CreateStream(object request, CancellationToken cancellationToken = default);
54+
}
5455
}

0 commit comments

Comments
 (0)