Fork me on GitHub

Working with IConfiguredInstance

The most common way for StructureMap to build or resolve a requested object is to build a concrete type directly by calling a public constructor function and optionally filling values in public setter properties. For this type of object construction, StructureMap exposes the IConfiguredInstance interface as a means of querying and modifying how a concrete type will be created or resolved. While the ServiceRegistry DSL fluent interface provides the main way of explicitly configuring concrete type creation, the IConfiguredInstance interface is meant to support conventional registration, configuration attributes, and construction policies.

public interface IConfiguredInstance
    ConstructorInfo Constructor { get; set; }
    Type ServiceType { get; }
    Type ImplementationType { get; }
    ServiceLifetime Lifetime { get; set; }
    string Name { get; set; }

    DependencyExpression<T> Ctor<T>(string constructorArg = null);
    IReadOnlyList<Instance> InlineDependencies { get; }

    void AddInline(Instance instance);

Changing the Instance Lifecycle

You can override the lifecycle of a single IConfiguredInstance by calling the LifecycleIs() methods and either supplying a type of ILifecycle or an ILifecycle object. As a quick helper, there are also extension methods for common lifecycles:

IConfiguredInstance instance
    = new ConfiguredInstance(typeof(WidgetHolder));

// Use the SingletonThing lifecycle

// or supply an ILifecycle type

// or supply an ILifecycle object
instance.SetLifecycleTo(new Lifecycles_Samples.MyCustomLifecycle());

// or override to the default "transient" lifecycle

Reflecting over Constructor Parameters

To find the constructor function parameters of an IConfiguredInstance, just use this syntax (it's just .Net Reflection):

public class GuyWithArguments
    public GuyWithArguments(IWidget widget, Rule rule)

public void reflecting_over_constructor_args()
    IConfiguredInstance instance = new SmartInstance<GuyWithArguments>()
        // I'm just forcing it to assign the constructor function
        .SelectConstructor(() => new GuyWithArguments(null, null));

    instance.Constructor.GetParameters().Select(x => x.Name)
        .ShouldHaveTheSameElementsAs("widget", "rule");

The constructor function selection process takes place as the very first step in creating a build plan and will be available in any kind of construction policy or configuration attribute on parameters or properties.

Working with Inline Dependencies

The IConfiguredInstance.InlineDependencies property is a collection of Instance objects that model inline dependencies. A single Instance refers to a parameter in a constructor function:

When Lamar determines a build plan for a concrete type, it reflects over all the parameters in the chosen constructor function and then the settable properties looking for any explicitly configured dependencies by searching in order for:

  1. An exact match by dependency type and name
  2. A partial match by dependency type only
  3. A partial match by name only

For primitive arguments like strings or numbers, the logic is to search first by name, then by type. All searching is done in the order that the inline Instance objects are registered, so do watch the order in which you add arguments. There is a method to insert new arguments at the front of the list if you need to do any kind of overrides of previous behavior.

See Inline Dependencies for more information about working with inline dependencies.