aspnet / DependencyInjection

[Archived] Contains common DI abstractions that ASP.NET Core and Entity Framework Core use. Project moved to https://github.com/aspnet/Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BoundTo Lifestyle

Dave3of5 opened this issue · comments

Is there anyway to use a bound lifestyle with aspnet core i.e. In Castle Windsor you can do:

container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.BoundTo<IBar>());

I don't see any option for this at the moment.

No there is no equivalent.

@davidfowl is there a plan for this or am I SOL?

You are SOL.

@davidfowl Ok lol thanks, can I ask then how does Scoped lifetime work in a non web program? If I have two threads is the scoping between those threads ?

@Dave3of5 scoping is extremely simple. You make a scope, and scoped instanced are tied to the lifetime of the scoped container. As in, you need to manually resolve instances from the scoped container to get the scoped instance. That's it. There's no magic per thread, per anything ambient scope.

@davidfowl Sorry still quite don't understand. So if I had two threads running in parallel both resolving from the same container any instance created via scoped would use the same instance. Whereas if I had two containers (one for each) thread they would get different instances ?

@davidfowl Nevermind I understand it now.

I would create a scope per thread / task. Is there anyway to have different registrations per scope something like: http://docs.autofac.org/en/latest/lifetime/working-with-scopes.html#adding-registrations-to-a-lifetime-scope.

Doesn't need to be done exactly like this but for example if I two static methods that would create different lifetime registrations would I just call whatever method as the first line of the scope?

We don't support registering services in a scope (they aren't child container builders). Scoped containers themselves just represent the lifetime of the scoped services.

Doesn't need to be done exactly like this but for example if I two static methods that would create different lifetime registrations would I just call whatever method as the first line of the scope?

Yes, you just create a scope and use that service provider.

Exactly what I need to know you are a star @davidfowl !!