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

[Suggestion] Option to control handler lifetime #60

Open
janoveh opened this issue Nov 20, 2022 · 2 comments
Open

[Suggestion] Option to control handler lifetime #60

janoveh opened this issue Nov 20, 2022 · 2 comments

Comments

@janoveh
Copy link

janoveh commented Nov 20, 2022

What about providing the following interfaces and use those to register handlers accordingly:
ISingletonRequestHandler<in TRequest, Tresponse>, IScopedRequestHandler<...>, ITransientRequestHandler<...>
All of them inherit from IRequestHandler<...>. Default lifetime applies if IRequestHandler<...> is used.
Alternatively, there could maybe be an attribute to override the default registration.
In a larger asp.net core web api type of backend you will typically have a mix of lifetimes for your handlers.
It would actually also be a good idea for notification handlers, in my opinion.

@martinothamar
Copy link
Owner

Interesting idea! This needs some prototyping (would need some refactoring), but it would definitely be a nice feature. I agree that it would have to be configurable across all handlers.

Probably will be some challenges/design questions when it comes to combination of handlers and pipelinebehaviors with different lifetimes, but as long as it doesn't become too messy/unclear I think this could work

@janoveh
Copy link
Author

janoveh commented Nov 21, 2022

Yes, I believe there will probably (?) be some challenges around handling DI scopes and injection of the IMediator service into handlers, especially in singletons, I guess, but I think they can be overcome by carefully creating scopes as needed. But this would of course be the responsibility of the handler developer and not the Mediator source generators.
I must say I really like the work you have done on the source generator.

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

2 participants