using System.Threading.Tasks;
public interface INotificationHandler<in TNotification>
where TNotification : class
Task Handle(TNotification notification, CancellationToken cancellationToken);
public interface IRequestHandler<in TRequest, TResponse>
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
public int UserId { get; set; }
public int Age { get; set; }
public int UserId { get; set; }
class TestRequestHandler : IRequestHandler<TestRequest, int>
public Task<int> Handle(TestRequest request, CancellationToken cancellationToken)
return Task.FromResult(request.UserId);
class TestNotificationHandler : INotificationHandler<TestNotification>
public Task Handle(TestNotification notification, CancellationToken cancellationToken)
Console.WriteLine("hello");
return Task.CompletedTask;
public static void Main()
new TestNotificationHandler()
.Handle(new TestNotification(), CancellationToken.None);
var result = new TestRequestHandler()
.Handle(new TestRequest() { UserId = 111 }, CancellationToken.None)
Console.WriteLine("result is " + result);