There used to be time when you would configure StructureMap this way

vb.net
1
StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()

and then this to get your implementation

vb.net
1
ObjectFactory.GetInstance(Of ITest)()
ObjectFactory.GetInstance(Of ITest)()

Fine that all worked nice and dandy but it had a drawback. Structuremapconfiguration was static/shared. This meant that this.

vb.net
1
2
3
4
StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
ObjectFactory.GetInstance(Of ITest)
StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
ObjectFactory.GetInstance(Of ITest)
StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
ObjectFactory.GetInstance(Of ITest)
StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
ObjectFactory.GetInstance(Of ITest)

would lead to an error because you could not add classes to structuremap once objectfactory.getinstance was called. But Jeremy and his compadres (sorry musketeers) to the rescue. In the new svn version (not version 2.4.9 apparently) you can do this.

vb.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#Region " Private members "
        Imports TDB2007.Utils.CommonFunctions.IoC.Interfaces
Imports StructureMap
 
Namespace IoC.StructureMapIoC
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Class StructureMapContainer
        Implements IoC.Interfaces.IContainerIoC
 
#Region " Private members "
''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Private _Container As StructureMap.IContainer
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Private _Registry As StructureMap.Configuration.DSL.Registry
#End Region
 
#Region " Constructors "
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            _Registry = New StructureMap.Configuration.DSL.Registry
            _Container = New StructureMap.Container(_Registry)
        End Sub
#End Region
 
#Region " Public methods "
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Resolve(Of T)() As T Implements IContainerIoC.Resolve
            Return _Container.GetInstance(Of T)()
        End Function
 
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="ImplamentationName"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Resolve(Of T)(ByVal ImplamentationName As String) As T Implements IContainerIoC.Resolve
            Return _Container.GetInstance(Of T)(ImplamentationName)
        End Function
 
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="Implemtation"></param>
        ''' <remarks></remarks>
        Public Sub InjectImplementation(Of T)(ByVal Implemtation As T) Implements IContainerIoC.InjectImplementation
            _Container.Inject(Of T)(Implemtation)
        End Sub
 
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="ImplementationName"></param>
        ''' <remarks></remarks>
        Public Sub SetDefaultInstanceName(Of T)(ByVal ImplementationName As String) Implements IContainerIoC.SetDefaultInstanceName
            _Container.SetDefault(GetType(T), ImplementationName)
        End Sub
 
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="INPUTTYPE"></typeparam>
        ''' <typeparam name="CONCRETETYPE"></typeparam>
        ''' <param name="name"></param>
        ''' <param name="IsDefault"></param>
        ''' <remarks></remarks>
        Public Sub Register(Of INPUTTYPE, CONCRETETYPE As {INPUTTYPE})(Optional ByVal name As String = "default", Optional ByVal IsDefault As Boolean = True) Implements Interfaces.IContainerIoC.Register
            If IsDefault Then
                _Registry.ForRequestedType(Of INPUTTYPE).TheDefault.Is.OfConcreteType(Of CONCRETETYPE).WithName(name)
            Else
                _Registry.BuildInstancesOf(Of INPUTTYPE).AddConcreteType(Of CONCRETETYPE)(name)
            End If
            _Container = New StructureMap.Container(_Registry)
        End Sub
 
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="INPUTTYPE"></typeparam>
        ''' <typeparam name="CONCRETETYPE"></typeparam>
        ''' <param name="name"></param>
        ''' <param name="IsDefault"></param>
        ''' <remarks></remarks>
        Public Sub RegisterSingleton(Of INPUTTYPE, CONCRETETYPE As {INPUTTYPE})(Optional ByVal name As String = "default", Optional ByVal IsDefault As Boolean = True) Implements Interfaces.IContainerIoC.RegisterSingleton
            If IsDefault Then
                _Registry.ForRequestedType(Of INPUTTYPE).AsSingletons.TheDefault.Is.OfConcreteType(Of CONCRETETYPE).WithName(name)
            Else
                _Registry.ForRequestedType(Of INPUTTYPE).AsSingletons.AddConcreteType(Of CONCRETETYPE)(name)
            End If
            _Container = New StructureMap.Container(_Registry)
        End Sub
 
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub ResetContainer() Implements IContainerIoC.ResetContainer
            _Registry = New StructureMap.Configuration.DSL.Registry
            _Container = New StructureMap.Container(_Registry)
        End Sub
#End Region
 
    End Class
End Namespace
#Region " Private members "
        Imports TDB2007.Utils.CommonFunctions.IoC.Interfaces
Imports StructureMap

Namespace IoC.StructureMapIoC
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Class StructureMapContainer
        Implements IoC.Interfaces.IContainerIoC

#Region " Private members "
''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Private _Container As StructureMap.IContainer
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Private _Registry As StructureMap.Configuration.DSL.Registry
#End Region

#Region " Constructors "
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            _Registry = New StructureMap.Configuration.DSL.Registry
            _Container = New StructureMap.Container(_Registry)
        End Sub
#End Region

#Region " Public methods "
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Resolve(Of T)() As T Implements IContainerIoC.Resolve
            Return _Container.GetInstance(Of T)()
        End Function

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="ImplamentationName"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Resolve(Of T)(ByVal ImplamentationName As String) As T Implements IContainerIoC.Resolve
            Return _Container.GetInstance(Of T)(ImplamentationName)
        End Function

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="Implemtation"></param>
        ''' <remarks></remarks>
        Public Sub InjectImplementation(Of T)(ByVal Implemtation As T) Implements IContainerIoC.InjectImplementation
            _Container.Inject(Of T)(Implemtation)
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="ImplementationName"></param>
        ''' <remarks></remarks>
        Public Sub SetDefaultInstanceName(Of T)(ByVal ImplementationName As String) Implements IContainerIoC.SetDefaultInstanceName
            _Container.SetDefault(GetType(T), ImplementationName)
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="INPUTTYPE"></typeparam>
        ''' <typeparam name="CONCRETETYPE"></typeparam>
        ''' <param name="name"></param>
        ''' <param name="IsDefault"></param>
        ''' <remarks></remarks>
        Public Sub Register(Of INPUTTYPE, CONCRETETYPE As {INPUTTYPE})(Optional ByVal name As String = "default", Optional ByVal IsDefault As Boolean = True) Implements Interfaces.IContainerIoC.Register
            If IsDefault Then
                _Registry.ForRequestedType(Of INPUTTYPE).TheDefault.Is.OfConcreteType(Of CONCRETETYPE).WithName(name)
            Else
                _Registry.BuildInstancesOf(Of INPUTTYPE).AddConcreteType(Of CONCRETETYPE)(name)
            End If
            _Container = New StructureMap.Container(_Registry)
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <typeparam name="INPUTTYPE"></typeparam>
        ''' <typeparam name="CONCRETETYPE"></typeparam>
        ''' <param name="name"></param>
        ''' <param name="IsDefault"></param>
        ''' <remarks></remarks>
        Public Sub RegisterSingleton(Of INPUTTYPE, CONCRETETYPE As {INPUTTYPE})(Optional ByVal name As String = "default", Optional ByVal IsDefault As Boolean = True) Implements Interfaces.IContainerIoC.RegisterSingleton
            If IsDefault Then
                _Registry.ForRequestedType(Of INPUTTYPE).AsSingletons.TheDefault.Is.OfConcreteType(Of CONCRETETYPE).WithName(name)
            Else
                _Registry.ForRequestedType(Of INPUTTYPE).AsSingletons.AddConcreteType(Of CONCRETETYPE)(name)
            End If
            _Container = New StructureMap.Container(_Registry)
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub ResetContainer() Implements IContainerIoC.ResetContainer
            _Registry = New StructureMap.Configuration.DSL.Registry
            _Container = New StructureMap.Container(_Registry)
        End Sub
#End Region

    End Class
End Namespace

Or something equally brilliant. You can now add things to the container even after calling resolve.

You can find more of this Using the StructureMap Container independently of ObjectFactory by Jeremy and Comparing .NET DI (IoC) Frameworks, Part 2 by Andrey Shchekin