-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support generic handlers #141
Comments
In services.AddSingleton<IRequestHandler<Command<Story>, ActionResult>, Handler<Story>>(); But it doesn't seem to work in |
True, doesn't support generic messages yet. Will require a refactoring of some things so not sure that will possible. I double checked, and it is documented in the "differences from MediatR" section in the readme: https://github.com/martinothamar/Mediator?tab=readme-ov-file#6-differences-from-mediatr There is no support for manually registering message handlers, might be possible to change though, by checking the service collection before registering, though there may be more issues down the road.. The fundamental issue is that the sourcegen'd implementation is based around knowing concrete types, underlying handling functions need to namethem (it's not simply |
Closing this and keeping track of #76 |
- Fixes #61. Password redirect from `.well-known` works as it should - Works around martinothamar/Mediator#141 and blockable content API not should work, albeit in a different shape - Fixed the Gulp pipeline not bundling JS code properly - Generated new TS clients, discovered Genfic/Tool#3 - Minor changes removing dead code and switching to primary constructors
Hello, are there any updates on this topic yet? 😄 I was attempting to create generic entity notifications, such as Is this feature still being considered? |
I found a temporary solution to my intent like this: public interface IDomainEventPublisher
{
public ValueTask PublishEntitiesCreatedEventAsync(IEnumerable<Entity> entities, CancellationToken cancellationToken = default);
} public class MediatorDomainEventPublisher : IDomainEventPublisher
{
private readonly IMediator _mediator;
public MediatorDomainEventPublisher(IMediator mediator)
{
_mediator = mediator;
}
public async ValueTask PublishEntitiesCreatedEventAsync(IEnumerable<Entity> entities, CancellationToken cancellationToken = default)
{
Type? entityType = entities.GetType().GetGenericArguments()[0];
ValueTask publishTask = entityType switch
{
Type t when t == typeof(ProductionTask) => _mediator.Publish(new ProductionTasksCreatedEvent(entities.Cast<ProductionTask>()), cancellationToken),
_ => ValueTask.CompletedTask
};
await publishTask;
}
} services.AddScoped<IDomainEventPublisher, MediatorDomainEventPublisher>(); I call publish methods inside my RepositoryBase: public abstract class EfRepositoryBase<TEntity> : IRepository<TEntity> where TEntity : Entity
{
protected EfDbContext Context { get; }
protected IDomainEventPublisher DomainEventPublisher { get; }
public EfRepositoryBase(EfDbContext context, IDomainEventPublisher domainEventPublisher)
{
Context = context;
DomainEventPublisher = domainEventPublisher;
}
public async ValueTask CreateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
{
await Context.AddRangeAsync(entities, cancellationToken).ConfigureAwait(false);
await DomainEventPublisher.PublishEntitiesCreatedEventAsync(entities, cancellationToken).ConfigureAwait(false);
}
} I need to modify this publisher's switch statements for each new Event I create though. |
I switched from MediatR to Mediator recently, for sourcegen instead of reflections. Alas, it doesn't seem to support generic handlers, and I have a specific use case for them:
The text was updated successfully, but these errors were encountered: