-
Notifications
You must be signed in to change notification settings - Fork 31
Injection Context
Starting in version 5.0 the injection context has been split into two distinct entities StaticInjectionContext and IInjectionContext.
The static injection context represents static information that can be gleaned at configuration time. Current injection information is available as well as information about parents in the object graph.
IInjectionContext represents a per request context that is created only if an object graph depends on it or a lifestyle is used that depends on IInjectionContext.
The context serves one main purpose to act as a storage container on a per request basis. You can store your own data in the injection context using the GetExtraData/SetExtraData methods. The context is also used to store data for lifestyles like SingletonPerObjectGraph and SingletonPerAncestor
The context will also be checked for dependencies when no registered dependency can be found (this is a last ditch effort before throwing an exception).
To make it easier to provide request time data the Locate methods can take an extra data object and turn it's properties into an IInjectionContext.
public class SomeClass
{
public SomeClass(int someInt) { IntValue = someInt; }
public int IntValue { get; }
}
var container = new DependencyInjectionContainer();
var instance = container.Locate<SomeClass>(new { someInt = 10 });
Assert.Equal(10, instance.IntValue);