martinothamar / Mediator

A high performance implementation of Mediator pattern in .NET using source generators.
MIT License
2.16k stars 71 forks source link

Incorrect switch case order in polymorphic notifications #138

Closed grendizeras closed 7 months ago

grendizeras commented 9 months ago

I have base notification class public record DomainEvent(DateTimeOffset Timestamp) : INotification;

and several child classes: public record RoundCreated(long Id, DateTimeOffset Timestamp) : DomainEvent(Timestamp); public record RoundResulted(long Id, WinState? Win, DateTimeOffset Timestamp) : DomainEvent(Timestamp) public record RoundSucceeded(long Id, DateTimeOffset Timestamp) : DomainEvent(Timestamp) I only have handlers for child event notifications. In this case source generator generate switch cases in incorrect order:

 case global::Shared.Aggregate.DomainEvent n: return Publish(n, cancellationToken);
 case global::Domain.Aggregates.RoundAggregate.Events.RoundSucceeded n: return Publish(n, cancellationToken);
 case global::Domain.Aggregates.RoundAggregate.Events.RoundCanceled n: return Publish(n, cancellationToken);
 case global::Domain.Aggregates.RoundAggregate.Events.RoundCreated n: return Publish(n, cancellationToken);
 case global::Domain.Aggregates.RoundAggregate.Events.RoundResulted n: return Publish(n, cancellationToken);

resulting in Error CS8120
"The switch case is unreachable. It has already been handled by a previous case or it is impossible to match. "

Expected behavior would be to place base class case last.

martinothamar commented 9 months ago

Will look into it! What happens if you make DomainEvent abstract? Otherwise I think I agree on the ordering

martinothamar commented 8 months ago

Will be fixed by #145 (when you want the base types to also be published)

martinothamar commented 7 months ago

Fixed in 3.0.0-preview.25