Can be done differently the implementation of the DependencyScope?
See original GitHub issueHi everyone,
I am writing a new web api application using Windsor. I’ve always used Microsoft Unity and Autofac. Now I want to try Windsor.
But something is not clear using Windsor. I have seen how everyone, all people, write the dependency resolver
and the dependency scope
.
Honestly, I do not like that way… It seems muddler to me… but perhaps because I have not understood something…
I try to explain what I mean talking about my experience with Unity and Autofac. The question I want to answer is: How do I start a new scope in “every” DI framework?
Microsoft Unity
In the dependencyResolver
, inside the BeginScope
method, I creat a childContainer and I call recursively the resolver:
var child = container.CreateChildContainer();
return new UnityResolver(child);
In this way, every service resolved in the child container will have the scope of the child container. The scopes of the child containers are the classic scopes: Singleton, PerWebRequest, …
Autofac
In the dependencyResolver
, inside the BeginScope
method, I create a dependencyScope
object that take as parameter a LifeTimeScope
. Then, I resolve everyting in the lifeTimeScope:
new AutoFacDependencyScope(_container.BeginLifetimeScope());
Here, how resolve the service(s) in the AutoFacDependencyScope
:
_lifetimeScope.TryResolve(serviceType, out instance);
As you can see, the services are resolved using the lifeTimeScope, not the container.
Windsor
Surely you know better than me how to implement the dependency resolver
and the dependency scope
. Here I write how I have done.
In the dependencyResolver
, inside the BeginScope
method, I create a dependencyScope
object that take as parameter the SAME instance of the container
I am using. Then, inside the dependencyScope
i do a begin scope
and I resolve the services in the container I passed from outside.
It looks like nothing changed… I am resolving everything inside the main container just with the difference that before I have called a begin scope… perhaps that is enough to create everything with a scope… but it is not explicit… I am just supponsing…
Is it correct this way?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top GitHub Comments
I have an open PR where I am trying to get this exact idea merged. You can see the implementation we are going for here. The PR is also #351.
Also wrote some docs for this here: https://github.com/castleproject/Windsor/blob/master/docs/aspnetwebapi-facility.md