InstanceProducer Class

Simple Injector
Produces instances for a given registration. Instances of this type are generally created by the container when calling one of the Register overloads. Instances can be retrieved by calling GetCurrentRegistrations() or GetRegistration(Type, bool).
Inheritance Hierarchy


Namespace:  SimpleInjector
Assembly:  SimpleInjector (in SimpleInjector.dll) Version: 4.0.0

public class InstanceProducer

The InstanceProducer type exposes the following members.


Public methodInstanceProducer
Initializes a new instance of the InstanceProducer class.

Public propertyLifestyle
Gets the Lifestyle for this registration. The returned lifestyle can differ from the lifestyle that is used during the registration. This can happen for instance when the registration is changed by an ExpressionBuilt registration or gets decorated.
Public propertyRegistration
Gets the Registration instance for this instance.
Public propertyServiceType
Gets the service type for which this producer produces instances.

Public methodBuildExpression
Builds an expression that expresses the intent to get an instance by the current producer. A call to this method locks the container. No new registrations can't be made after a call to this method.
Public methodStatic memberFromExpression
Creates a new InstanceProducer based on the given serviceType and expression where the expression will be used as-is; no interception (using ExpressionBuilt) such as decorators will be applied.
Public methodGetInstance
Produces an instance.
Public methodGetRelationships
Gets the collection of relationships for this instance that the container knows about. This includes relationships between the registered type and its dependencies and relationships between applied decorators and their dependencies. Note that types that are not newed up by the container and properties that are injected inside a custom delegate that is registered using the RegisterInitializer method are unknown to the container and are not returned from this method. Also note that this method will return an empty collection when called before the registered type is requested from the container (or before Verify is called).
Public methodVisualizeObjectGraph
Builds a string representation of the object graph with the current instance as root of the graph.

The Register method overloads create InstanceProducer instances internally, but InstanceProducers can be created manually to implement special scenarios. An InstanceProducer wraps Registration instance. The Registration builds an Expression that describes the intend to create the instance according to a certain lifestyle. The InstanceProducer on the other hand transforms this Expression to a delegate and allows the actual instance to be created. A Registration itself can't create any instance. The InsanceProducer allows intercepting created instances by hooking onto the Container.ExpressionBuilt event. The RegisterDecorator methods for instance work by hooking onto the ExpressionBuilt event and allow wrapping the returned instance with a decorator.

The following example shows the creation of two different InstanceProducer instances that wrap the same Registration instance. Since the Registration is created using the Singleton lifestyle, both producers will return the same instance. The InstanceProducer for the
however, will wrap that instance in a (transient)
var container = new Container();

// ServiceImpl implements both Interface1 and Interface2.
var registration = Lifestyle.Singleton.CreateRegistration<ServiceImpl, ServiceImpl>(container);

var producer1 = new InstanceProducer(typeof(Interface1), registration);
var producer2 = new InstanceProducer(typeof(Interface2), registration);

container.RegisterDecorator(typeof(Interface1), typeof(Interface1Decorator));

var instance1 = (Interface1)producer1.GetInstance();
var instance2 = (Interface2)producer2.GetInstance();

Assert.IsInstanceOfType(instance1, typeof(Interface1Decorator));
Assert.IsInstanceOfType(instance2, typeof(ServiceImpl));

Assert.AreSame(((Interface1Decorator)instance1).DecoratedInstance, instance2);
See Also