Skip to content

Instantly share code, notes, and snippets.

@treytomes
Created June 2, 2016 14:56
Show Gist options
  • Save treytomes/94ee7450bb9ed1afb267f54305b537da to your computer and use it in GitHub Desktop.
Save treytomes/94ee7450bb9ed1afb267f54305b537da to your computer and use it in GitHub Desktop.
This VB.NET class will provide the glue between the StructureMap IOC library and Microsoft's CommonServiceLocator.
Imports Microsoft.Practices.ServiceLocation
Imports StructureMap
Public Class StructureMapServiceLocator
Inherits ServiceLocatorImplBase
Private _Container As IContainer
Public Sub New(pContainer As IContainer)
_Container = pContainer
End Sub
Public Sub New(pRegistry As Registry)
Me.New(New Container(pRegistry))
End Sub
''' <summary>
''' This method will do the actual work of resolving the requested service instance.
''' </summary>
''' <param name="pServiceType">Type of instance requested.</param>
''' <param name="pKey">Name of registered service you want. May be null.</param>
''' <returns>
''' The requested service instance.
''' </returns>
Protected Overrides Function DoGetInstance(pServiceType As Type, pKey As String) As Object
If String.IsNullOrWhiteSpace(pKey) Then
Return _Container.GetInstance(pServiceType)
Else
Return _Container.GetInstance(pServiceType, pKey)
End If
End Function
''' <summary>
''' This method will do the actual work of resolving all the requested service instances.
''' </summary>
''' <param name="pServiceType">Type of service requested.</param>
''' <returns>
''' Sequence of service instance objects.
''' </returns>
Protected Overrides Iterator Function DoGetAllInstances(pServiceType As Type) As IEnumerable(Of Object)
For Each oInstance As Object In _Container.GetAllInstances(pServiceType)
Yield oInstance
Next
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment