martinothamar/Mediator

Incorrect switch case order in polymorphic notifications

grendizeras opened this issue · 3 comments

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.

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

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