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

[Feature Request]: Message Handlers with constructor args that need setting via factory method #571

Open
kevin-mcmanus opened this issue Jun 6, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@kevin-mcmanus
Copy link

Is your request related to a problem you have?

I have a generic message handler e.g. public class MyMessageHandler<T> : IMessageHandler<T>.

MyMessageHandler<T> has a constructor with some parameters. e.g. public MyMessageHandler(MyOptions options)

If we register MyOptions in the DI container, all instances of a MyMessageHandler<T> for differing types T will get the same MyOptions injected.

It would be good to enable MyMessageHandler<Tx> to be injected with a different set of options to MyMessageHandler<Ty>.

Describe the solution you'd like

Open to suggestions. Perhaps something like:

.AddMiddlewares(middlewares => middlewares
    .AddTypedHandlers(handler =>
    {
        handler.AddHandler<MyMessageHandler<Tx>>(dependencyResolver=> new MyMessageHandler<Tx>(dependencyResolver.Resolve<MyOptionsX>()));
        handler.AddHandler<MyMessageHandler<Ty>>(dependencyResolver => new MyMessageHandler<Ty>(new MyOptionsY()));
    })
)

Are you able to help bring it to life and contribute with a Pull Request?

No

Additional context

No response

@kevin-mcmanus kevin-mcmanus added the enhancement New feature or request label Jun 6, 2024
@joelfoliveira
Copy link
Contributor

joelfoliveira commented Jun 12, 2024

Hi,

Currently, there is no way to do this. Nevertheless, if your use case is dependent on this you can always delegate the object instance resolution during the middleware invocation (although not ideal). Something like this:

public class MyMessageHandler<T> : IMessageHandler<T> where T : class
{
    private readonly IDependencyResolver _resolver;

    public MyMessageHandler(IDependencyResolver resolver)
    {
        _resolver = resolver;
    }

    public Task Handle(IMessageContext context, T message)
    {
        var options = message is Tx ?  _resolver.Resolve<MyOptions>() : new MyOptions();

        return Task.CompletedTask;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants