According to Simple Injector documentation, a Per Graph lifestyle exists that can be used to limit the scoping of an object to the current graph. But the documentation doesn't list what actually needs done to implement it, and I can't find any documentation online on how to use it. How do I define a Per Graph scope for an object when registering it?
I would have thought:
container.Register<ISomeType, SomeType>(Lifestyle.PerGraph);
would exist, but it does not by default.
The documentation states:
This lifestyle can be simulated by using one of the Scoped lifestyles.
In other words, Simple Injector does not contain a PerGraph lifestyle out of the box (the documentation isn't really explicit about this, so it might need to be improved a bit). The trick is to select the proper scoped lifestyle (depending on your requirements) and (implicitly or explicitly) start a scope and resolve the graph. For instance:
var scopedLifestyle = new AsyncScopedLifestyle();
container.Register<ISomeType, SomeType>(scopedLifestyle);
using (AsyncScopedLifestyle.BeginScope(container))
{
var some = container.GetInstance<SomeRootObjectDependingOnSomeType>();
some.Execute();
}
The main reason why a per graph lifestyle doesn't exist in Simple Injector is because it can be very unreliable. If building of part of the graph is delayed (for instance because of the use of a Lazy<T>, Func<T>, or a call back into the container) this results in a new graph and therefore a new per graph instance. This is easy to miss and it is impossible for the Simple Injector diagnostic services to warn about this. Therefore it is much safer and clearer to explicitly define a scope yourself.