In general, it is generally accepted that passing the IoC container around your application and using it as a service locator is bad practice.
I prefer to use the container only in the composite root of my application and, as a rule, makes one call to Resolve () - resolving the top-level object in my application and responding to the container to introduce dependencies to classes below the object graph.
Windsor Castle recently added a lifestyle in which you can call container.BeginScope () in the using block. From within this block, βuseβ, allowing a component that has been registered in the cloud lifestyle, will always return the same instance throughout the entire βuseβ of the block.
container.Register(Component.For<A>().LifestyleScoped()); using (container.BeginScope()) { var a1 = container.Resolve<A>(); var a2 = container.Resolve<A>(); Assert.AreSame(a1, a2); }
Question: Given that BeginScope () is an extension method in a container, I donβt see how an exemplary lifestyle can be used in an application if the container is not passed (which I really do not want to do). Does anyone have examples of where / how to use an exemplary lifestyle?
Thanks,
Tom
Tom davis
source share