Fork me on GitHub

Custom Instance Types Edit on GitHub


Most users will never need to implement a custom Instance type, but it does come up here and there. The easiest way is probably to subclass LambdaInstance as shown below:


public class FuncInstance<T> : LambdaInstance<Func<T>>
{
    // Pass a Func<IContext, T> into the base constructor
    // Use a static method for cleaner syntax to avoid the 
    // nasty Lambda syntax formatting as needed
    public FuncInstance() : base(s => s.GetInstance<IContainer>().GetInstance<T>)
    {
    }

    public override string Description
    {
        get { return "Constructor for Func<{0}>".ToFormat(typeof (T).Name); }
    }
}

The other common customization is as a generic builder template

If neither of these approaches will easily work for your custom Instance, you may want to contact the StructureMap team via the gitter link above and we can help walk you through a custom subclass of Instance -- or more likely, talk you out of it somehow.

Missing Name Instance Template

New to StructureMap 4.0 is the Instance.ToNamedClone(name) method that is used behind the scenes by the Handling Missing Named Instances feature.


public virtual Instance ToNamedClone(string name)

Custom Instance types can override this method to provide a brand new Instance for a name. This functionality may be useful for multi-tenancy situations.