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

Attribute Normalized Xml Mementos

This style is a more terse configuration format that was added in version 1.0.

Primitive Properties (Strings and basic value types)

Primitive constructor or setter arguments are defined by adding an attribute @propertyName="propertyValue" to the instance node.

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

        <Widget Type="Color" Key="Red" Color="Red" />

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. 

        <Instance Type="Sql" Key="SomeQuery">
	    <bigProp>
                <![CDATA[
                    select * from table
                    where
                        somecolumn = 'something' or
                        some_other_column = 'something else'
                ]]>
            </bigProp>
        </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" Breed="Angus" />

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" Name="Tom">
            <MyGrandChild Name="MyGrandChild" Type="Leftie" BirthYear="1984" />
        </StructureMap.Testing.Widget.Child>

Reference Definition

        <StructureMap.Testing.Widget.Child Type="Default" Key="Marsha" Name="Marsha">
            <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">
            <innerStrategies Name="innerStrategies">
                <!-- Referenced Instance -->
                <Child Key="Red" />
 
                <Child><!-- Default Instance --></Child>
 
                <!-- Inline Definition -->
                <Child Type="Random" seed="0.034"/>
            </innerStrategies>
        </Instance>