-
-
Notifications
You must be signed in to change notification settings - Fork 184
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
Request: Add a IContainerBuilder.RegisterProvider method #205
Comments
Agree. Microsoft.Extensions.DependencyInjection and others also support this, and I think it is useful. As for the name of the method you suggested, what "*Provider" means is not so obvious to me, so Im thinking of the following signature. builder.Register<IFoo>(_ => new Foo(), Lifetime.Scoped); -- In Unity, I think there is a risk that capturing to lambda expressions could cause problems with objects having unintended lifetimes, but oh well, I won't worry about it. |
I have another request that is related to this issue. It would be nice if you could use a similar delegate style with the builder.Register<Foo>()
.WithParameter("url", resolver =>
{
var service = resolver.Resolve<ServiceA>();
return service.GetUrl();
}); |
Interesting. However, since the lambda expression has the maximum degree of freedom, perhaps it can be replaced by. builder.Register<Foo>(resolver =>
{
var url = resolver.Resolve<ServiceA>().GetUrl();
return new Foo(url, ..., ...);
}); I currently prefer to be able to do the same thing with a small number of flexible features rather than adding more features. |
I would like a way to more easily delegate resolving a type to another function or class. Here's my thoughts for a few new
RegisterProvider
methods. The idea is that when resolving a given typeT
, it would invoke the provider and inject the returned result. Unlike a Factory, a provider would be called at the time of resolution, whereas a factory is injected directly.Here's a simple example of how it would be used
Container:
Consumer:
Maybe there is already a way of doing this sort of thing, but I haven't been able to find a good way to do it.
The text was updated successfully, but these errors were encountered: