There used to be time when you would configure StructureMap this way
StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()```
and then this to get your implementation
```vbnet
ObjectFactory.GetInstance(Of ITest)()```
Fine that all worked nice and dandy but it had a drawback. Structuremapconfiguration was static/shared. This meant that this.
```vbnet
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)][1] to the rescue. In the new svn version (not version 2.4.9 apparently) you can do this.
```vbnet
#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][2] and [Comparing .NET DI (IoC) Frameworks, Part 2 by Andrey Shchekin][3]
[1]: http://codebetter.com/blogs/jeremy.miller/
[2]: http://codebetter.com/blogs/jeremy.miller/archive/2008/09/10/using-the-structuremap-container-independently-of-objectfactory.aspx
[3]: http://blog.ashmind.com/index.php/2008/09/08/comparing-net-di-ioc-frameworks-part-2/