Replies: 1 comment
-
|
I think you should be able to do this.. I tried to make this example as close as I could to yours. //some type of type constraint.. I used interface.
public interface ITestResultEvent
{
bool HandlerCalled { get; set; }
}
//event class implements the constraint above and also INotification for MediatR
public class TestResultTestParameterAdded : ITestResultEvent, INotification
{
public bool HandlerCalled { get; set; }
}
//second event class like above to test that multiple events can have the same notification handler
public class TestResultTestParameterRemoved : ITestResultEvent, INotification
{
public bool HandlerCalled { get; set; }
}
//generic notification handler handles all event notifications that implement ITestResultEvent from above
public class TestResultEventNotificationHandler<TNotification> : INotificationHandler<TNotification>
where TNotification : class, ITestResultEvent, INotification
{
public Task Handle(TNotification notification, CancellationToken cancellationToken)
{
notification.HandlerCalled = true;
return Task.CompletedTask;
}
}
Then I made a test to test if each notification was calling the same handler. [Fact]
public async Task Test()
{
var mediator = _provider.GetService<IMediator>();
var addedNotification = new TestResultTestParameterAdded();
var removedNotification = new TestResultTestParameterRemoved();
await mediator!.Publish(addedNotification);
await mediator!.Publish(removedNotification);
addedNotification.HandlerCalled.ShouldBeTrue(); //true
removedNotification.HandlerCalled.ShouldBeTrue(); //true
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
So currently I have this scenario, where each handler will have 1:1 the same logic, so if i have 10 notifications / events that will mean I will also have 10 handler methods...
Smth like this
Is it possible to just have one Handler, and maybe 1 event that has it's own unqiue handler ??
TestResultRenamed and all the other events listed inherit from TestResultEvent, which, in turn, inherits from DomainEvent, and DomainEvent implements INotification from MediatR.
I have tried the follwing but the handle never gets called.
Thanks in advance !
Beta Was this translation helpful? Give feedback.
All reactions