-
Notifications
You must be signed in to change notification settings - Fork 33
Injection Context 4.0
When requesting an export from the container an IInjectionContext is used to hold contextual information about the injection through out the lifetime of the injection (this includes Func<T> and other delegates). By default a new instance of InjectionContext is created each time Locate is called. To allow for flexibility you can construct your own InjectionContext instances or by calling CreateContext on the container or scope.
During an injection IDisposable objects will be added to the disposal scope in the injection context. By default this is usually the IInjectionScope that is being resolved from, the exception to the rule is in MVC applications where the disposal scope is tied to the request lifetime rather than the injection scope.
This is the current requesting scope for the injection, usually it is the scope that the Locate call originated in but it can be swapped with different scope depending on Lifestyle containers.
The Export method allows you to register exports by type that can be used for that injection. Exports that have a transient lifestyle (Transient, PerRequest, PerScope, etc) will try and locate an export first from the Context then from RequestingScope -> RequestingScope.ParentScope -> etc. This allows you to provide overrides for each injection rather than having to register a func with the container.
ISomeClass someClass = new SomeImpl();
var context = container.CreateContext();
context.Export((scope,context) => someImpl));
var anotherService = container.Locate<IAnotherService>(context);
During the course of an injection extra data can be stored in the context and used later. This information is available in a number of different places Func delegates, Conditions, as well as Lifestyles. In fact this is how the PerScope Lifestyle is implemented (a unique id is create for each PerScope lifestyle and then the instance is stored in the context for use later in the injection)
When you resolve a Func<T> or any other delegate factory the IInjectionContext that was used during the injection will be cloned and reused inside the Factory. To put it another way all PerScope & ExtraData in the injection context will be cloned each time the Func<T> is called, however because a new context is created for each call there is no sharing between the calls.