SourceForge.net Logo

Home
StructureMap on SourceForge
Basic Architecture
Concepts
API Documentation
FAQ
Configuration Schema
    Memento Sources
    Node Normalized Xml
    Attribute Normalized Xml
    Attribute Usage
    Instance Lifecyle Scoping
Configuration Management
    StructureMapDoctor
    StructureMapExplorer
    deployment Task
    verification Task
    ValidationMethod Attribute
    Other NAnt Tasks
Troubleshooting
Singleton Injection

Node Normalized Xml Mementos

This is the default style used for instances within the StructureMap.config file.

Primitive Properties (Strings and basic value types)

Primitive constructor or setter arguments are defined by adding a <Property Name="propertyName" Value="propertyValue"> node underneath the instance node.

    [Pluggable("Color", "Only for testing")]
    public class ColorWidget : IWidget
    {
        public ColorWidget(string Color)
        {
            _Color = Color;
        }
    }

        <Widget Type="Color" Key="Red">
            <Property Name="Color" Value="Red" />
        </Widget>

Long Strings

There is an optional mode to define a property value inside a CDATA tag for very long strings like sql statements or Javascript templates. 

        <Property Name="bigProp">
            <![CDATA[
                select * from table
                where
                    somecolumn = 'something' or
                    some_other_column = 'something else'
            ]]>
        </Property>

Enumeration Properties

Enumeration arguments are defined the same way as primitive properties.  Use the string names of the enumeration for the values.

    public enum BreedEnum
    {
        Hereford,
        Angus,
        Longhorn
    }
 
    [Pluggable("Cow")]
    public class Cow
    {
        public BreedEnum Breed;
        public long Weight;
        public string Name;
 
        public Cow(long Weight, BreedEnum Breed, string Name)
        {
            this.Breed = Breed;
            this.Weight = Weight;
            this.Name = Name;
        }
    }

        <Instance Type="Cow" Key="Maggie">
            <Property Name="Breed" Value="Angus" />
        </Instance>

Child Properties

Child properties of non-primitive types are defined as embedded memento nodes.  Child properties can be either defined inline or use a reference to a named instance of the property type.  If a child property is omitted or defined with no value, StructureMap will use the default instance of the property type.

    [PluginFamily, Pluggable("Default", "")]
    public class GrandChild
    {
        public GrandChild(bool RightHanded, int BirthYear)
        {
        }
    }
 
 
    [Pluggable("Leftie", "")]
    public class LeftieGrandChild : GrandChild
    {
        public LeftieGrandChild(int BirthYear) : base(false, BirthYear)
        {
        }
    }
 
 
    [PluginFamily, Pluggable("Default", "")]
    public class Child
    {
        public Child(string Name, GrandChild MyGrandChild)
        {
        }
    }

Inline Definition

        <StructureMap.Testing.Widget.Child Type="Default" Key="Tom">
            <Property Name="Name" Value="Tom" />
            <Property Name="MyGrandChild" Type="Leftie">
                <Property Name="BirthYear" Value="1984" />
            </Property>
        </StructureMap.Testing.Widget.Child>

Reference Definition

        <StructureMap.Testing.Widget.Child Type="Default" Key="Marsha">
            <Property Name="Name" Value="Marsha"/>
            <Property Name="MyGrandChild" Key="Tommy"/>
        </StructureMap.Testing.Widget.Child>    

Child Array Property

    [Pluggable("Compound")]
    public class CompoundStrategy : IStrategy
    {
        public CompoundStrategy(IStrategy[] innerStrategies)
        {
        }
    }

        <Instance Key="ArrayTest" Type="Compound">
            <Property Name="innerStrategies">
                <!-- Referenced Instance -->
                <Property Key="Red"></Property>
 
                <Property><!-- Default Instance --></Property>
 
                <!-- Inline Definition -->
                <Property Type="Random">
                    <Property Name="seed" Value="0.034"/>
                </Property>
            </Property>
        </Instance>