У меня есть едва сложная иерархия классов уведомлений с MediatR:
Код: Выделить всё
public abstract class EntityNotification : INotification { }
public class EntityNotificationTyped : EntityNotification { }
public class A { }
public class B { }
Обратите внимание, что «EntityNotificationType» сам по себе не является универсальным типом.
Вот обработчики:
Код: Выделить всё
public class GenericNotificationHandler
: INotificationHandler
{
public async Task Handle(EntityNotification notification, CancellationToken cancellationToken)
{
Console.WriteLine("GenericNotificationHandler received:" + notification.GetType().FullName);
}
}
public class EntityNotificationForAHandler
: INotificationHandler
{
public async Task Handle(EntityNotificationTyped notification, CancellationToken cancellationToken)
{
Console.WriteLine("EntityNotificationForAHandler received:" + notification.GetType().FullName);
}
}
public class EntityNotificationForBHandler
: INotificationHandler
{
public async Task Handle(EntityNotificationTyped notification, CancellationToken cancellationToken)
{
Console.WriteLine("EntityNotificationForBHandler received:" + notification.GetType().FullName);
}
}
Код: Выделить всё
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddMediatR(config =>
{
config.RegisterServicesFromAssemblyContaining
();
});
var app = builder.Build();
var publisher = app.Services.GetRequiredService();
Console.WriteLine("Publishing A");
await publisher.Publish(new EntityNotificationTyped());
Console.WriteLine(Environment.NewLine + "Publishing B");
await publisher.Publish(new EntityNotificationTyped());
Console.WriteLine("End");
Код: Выделить всё
Publishing A
GenericNotificationHandler received:EntityNotificationTyped`1[[A, MediatRPublish, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
EntityNotificationForAHandler received:EntityNotificationTyped`1[[A, MediatRPublish, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Publishing B
GenericNotificationHandler received:EntityNotificationTyped`1[[B, MediatRPublish, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
EntityNotificationForBHandler received:EntityNotificationTyped`1[[B, MediatRPublish, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
End
Код: Выделить всё
Publishing A
GenericNotificationHandler received:EntityNotificationTyped`1[[A, MediatRPublish, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
EntityNotificationForAHandler received:EntityNotificationTyped`1[[A, MediatRPublish, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Publishing B
End
Я заметил, что создание общего обработчика, похоже, помогает. Думаю, это как-то связано с ковариантностью/контравариантностью, но я не понимаю, почему иногда это работает, иногда нет. Вот возможный «обобщенный обработчик»:
Код: Выделить всё
public class GenericNotificationHandler(ILogger logger)
: INotificationHandler
where TEntityNotification : EntityNotification
{
public async Task Handle(TEntityNotification notification, CancellationToken cancellationToken)...
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/791 ... -depending