Skip to content
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

Closed
Atulin opened this issue Mar 3, 2024 · 3 comments
Closed

Support generic handlers #141

Atulin opened this issue Mar 3, 2024 · 3 comments

Comments

@Atulin
Copy link

Atulin commented Mar 3, 2024

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:

public sealed record Command<T>(...) : IRequest<ActionResult> where T : BaseModel, IBlockableContent;

public class Handler<T>(ApplicationDbContext context) : BaseHandler, IRequestHandler<Command<T>, ActionResult>
    where T : BaseModel, IBlockableContent
{
    public async ValueTask<ActionResult> Handle(Command<T> request, CancellationToken cancellationToken)
    {
        // snip
        var res = await context.Set<T>() // here's where the generic is important
            .Where(i => i.Id == itemId)
            .ExecuteUpdateAsync(i => i.SetProperty(p => p.ContentBlock, cb), cancellationToken);
        // snip
    }
}
[HttpPost("story")]
public async Task<ActionResult> BlockStory(BlockContent.Command<Story> data)
    => await mediator.Send(data);

[HttpPost("chapter")]
public async Task<ActionResult> BlockChapter(BlockContent.Command<Chapter> data)
    => await mediator.Send(data);

[HttpPost("blogpost")]
public async Task<ActionResult> BlockBlogpost(BlockContent.Command<Blogpost> data)
    => await mediator.Send(data);
@cranberry-clockworks
Copy link

cranberry-clockworks commented Apr 5, 2024

In MediatR one could register it by manually registering handler:

services.AddSingleton<IRequestHandler<Command<Story>, ActionResult>, Handler<Story>>();

But it doesn't seem to work in Mediator
I'm still getting the error about handler not being found error.

@martinothamar
Copy link
Owner

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 message.GetType())

@martinothamar
Copy link
Owner

Closing this and keeping track of #76

Atulin added a commit to Genfic/Ogma that referenced this issue Apr 18, 2024
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants