You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 2, 2018. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 2, 2018. It is now read-only.
Add ability to have multiple service types per definition #360
This also works but I have to know the entire dependency graph to construct bar:
varbar=newBar();// Hope Bar has no servicesservices.AddSingleton<IBar>(bar);;services.AddSingleton<IBaz>(bar);vari2=sp.GetService<IBaz>();vari1=sp.GetService<IBar>();vari2=sp.GetService<IBaz>();Assert.Same(i1,i2);// Passes
We'd have to look at how to support this, should it be a service descriptor?
services.AddSingleton<IBar, IBaz, Bar>();
We also have to see if other DI containers support this.
Reacted by Dan Kirkham, nil4, hem@hdk and Yufei Huang
With regards to the API I think it is important to specify all interfaces along with the implementing type (or factory delegate) in one single registration method.
Consider this scenario
services.AddSingleton<IBar>(provider => new Bar());
services.AddSingleton<IBaz>(provider => new Bar());
In this scenario we have no way to look at the implementing type at registration time to determine if both interfaces should resolve to the same instance or not. The intent here could just as well be that we want two singletons, one for IBar and one for IBaz.
In my opinion the key to a service instance is the service type and the actual need for multiple service types pointing to the same instance should be so rare that your "less efficient" solution would work just fine.
I also would like to point out that more and more "expected behavior" is now being baked into this abstraction making it harder for third party containers to integrate.
I also would like to point out that more and more "expected behavior" is now being baked into this abstraction making it harder for third party containers to integrate.
This repository has 2 things, the default container and the abstraction. The 2 aren't coupled. It's conceivable that features can be added to the container without breaking the DI abstraction. It's really up to us to decide how that works. Right now we don't add features that we aren't consuming ourselves so it's a good forcing function.
@davidfowl I know this issue is old, why is your less efficient solution "less efficient"? Is it related to performance, or just because we have to write more code?
If its just about writing more code, couldn't we simply write a few extension methods for this?
@viniciusmelquiades Whenever you call GetService<T> manually, you basically start a new dependency graph. What I mean by that is, imagine you have an object graph that looks like this:
The DI container can build a dependency tree that ends up generating code like this:
new A(new B(new C())) (roughly). When you throw in GetService<T> calls, the container can't optimize because it cannot see the entire dependency graph.
Activity
dadhi commentedon Feb 12, 2016
DryIoc supports this via RegisterMany and more specifically via RegisterMapping.
seesharper commentedon Feb 12, 2016
With regards to the API I think it is important to specify all interfaces along with the implementing type (or factory delegate) in one single registration method.
Consider this scenario
In this scenario we have no way to look at the implementing type at registration time to determine if both interfaces should resolve to the same instance or not. The intent here could just as well be that we want two singletons, one for IBar and one for IBaz.
In my opinion the key to a service instance is the service type and the actual need for multiple service types pointing to the same instance should be so rare that your "less efficient" solution would work just fine.
I also would like to point out that more and more "expected behavior" is now being baked into this abstraction making it harder for third party containers to integrate.
Eilon commentedon Mar 7, 2016
Backlog.
davidfowl commentedon Mar 8, 2016
This repository has 2 things, the default container and the abstraction. The 2 aren't coupled. It's conceivable that features can be added to the container without breaking the DI abstraction. It's really up to us to decide how that works. Right now we don't add features that we aren't consuming ourselves so it's a good forcing function.
herecydev commentedon May 25, 2016
Any update on this now RC2 is shipped? Would be a nice to have for me.
Eilon commentedon May 26, 2016
@herecydev it's not a feature we're planning to implement for v1.
NKnusperer commentedon Aug 23, 2016
Any schedule on this?
viniciussalvati commentedon Oct 19, 2016
@davidfowl I know this issue is old, why is your less efficient solution "less efficient"? Is it related to performance, or just because we have to write more code?
If its just about writing more code, couldn't we simply write a few extension methods for this?
davidfowl commentedon Feb 12, 2017
@viniciusmelquiades Whenever you call
GetService<T>manually, you basically start a new dependency graph. What I mean by that is, imagine you have an object graph that looks like this:The DI container can build a dependency tree that ends up generating code like this:
new A(new B(new C()))(roughly). When you throw inGetService<T>calls, the container can't optimize because it cannot see the entire dependency graph.new A(GetService<B>())new B(GetService<C>())new C()`Each explicit
GetService<T>call basically resets dependency resolution (there's a new dictionary lookup etc).yufeih commentedon Mar 23, 2017
Any update to implement this post v1?
Eilon commentedon Mar 23, 2017
We're having a discussion this afternoon (though not sure if we'll get to this issue today; if not, we'll get to it soon).
Eilon commentedon Mar 27, 2017
Closing because there are no plans to support this. This would be a fairly large breaking change for not-that-large value.