Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          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

XML Feeds

Tags: structuremap

All the LessThanDot Journals

Dependency Injection Tool StructureMap 2.5 Has Been Released

by SQLDenis


Permalink 27 Oct 2008 07:08 , Categories: Microsoft Technologies Tags: .net, c#, dependency injection, structuremap

StructureMap is a Dependency Injection tool written in C# for .NET development. StructureMap is also a generic “Plugin” mechanism for flexible and extensible .NET applications.

The new functionality in StructureMap 2.5:

  • Completely revamped Assembly scanning options
  • Cleaner, more predictable way to initialize a Container.  StructureMapConfiguration is now deprecated, please use ObjectFactory.Initialize().
  • Optional setter injection
  • All new abilities to query the configuration of a Container
  • The ability to use StructureMap with ZERO Xml or attributes by default
  • The ability to add services at runtime. You can now programmatically add an entire Assembly at runtime for modular applications that might not want all services to be loaded at startup.
  • An auto mocking container based on Rhino Mocks 3.5. I was a doubter on the validity of AMC, but I'm sold now that I've used it
  • Contextual object construction
  • More sophisticated auto wiring rules
  • Supporting NameValueCollection and IDictionary types
  • Far more extensibility
  • Interception and post processing hooks for you AOP enthusiasts. StructureMap will NOT include its own AOP engine, but will allow you to use the runtime AOP technique of your choice.
  • More configuration options in both Xml and the Fluent Interface. Completely revamped the Registry DSL.
  • More options for modular configuration (mix and match Xml configuration or Registry's at will) – which basically had to trigger:
  • Completely revamped diagnostics, including the Environment Testing support
  • Transparent creation of concrete types that are not explicitly registered
  • Create objects with explicit arguments passed to the container
  • Use the underlying Container independently of ObjectFactory
  • Pluggable auto registration with your own custom Type scanning policies
  • StructureMap is now strong named (thanks to Steve Harman)
  • Pull configuration from the App.config (thanks to Josh Flanagan)
  • Generics fixes (thanks to Derrick Rapp)

Download it here: http://sourceforge.net/projects/structuremap

2 comments »Send a trackback » 192 views

VB.Net: Setter injection with StructureMap and the FI

by chrissie1


Permalink 22 Sep 2008 06:32 , Categories: Microsoft Technologies Tags: setterinjection, structuremap, vb.net

I’m using the SVN version for this.

Lets give an example of how to do this.

First we create a few interfaces.

  1. Public Interface IPerson
  2.     Function Name() As String
  3. End Interface
  1. Public Interface ITest
  2.     Function Name() As String
  3.     Property Person() As IPerson
  4. End Interface

as we can see ITest has a property Person of type IPerson. Ideally we would like to inject the IPerson.

Then we have the concrete classes.

  1. Public Class Person
  2.     Implements IPerson
  3.  
  4.     Public Function Name() As String Implements IPerson.Name
  5.         Return "Person"
  6.     End Function
  7. End Class
  1. Public Class Test
  2.     Implements ITest
  3.  
  4.     Private _Person As IPerson
  5.  
  6.     Public Sub New()
  7.  
  8.     End Sub
  9.  
  10.     Public Function Name() As String Implements ITest.Name
  11.         Return "test"
  12.     End Function
  13.  
  14.     Public Property Testprop() As IPerson Implements ITest.Person
  15.         Get
  16.             Return _Person
  17.         End Get
  18.         Set(ByVal value As IPerson)
  19.             _Person = value
  20.         End Set
  21.     End Property
  22. End Class

Now I want to inject the Person that I have already made with structureMap.

  1. Dim _Registry As StructureMap.Configuration.DSL.Registry
  2. _Registry.ForRequestedType(Of IPerson).TheDefault.Is.OfConcreteType(Of Person).WithName("default")
  3. _Registry.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test).WithName("default").SetterDependency(Of IPerson).Is(Function(e) e.TheDefault)
  4. Dim _Container As New StructureMap.IContainer(_Registry)

The first line makes a new registry.
The second line adds a Iperson to the registry with as concretetype Person that is also the default for IPerson with a name of default.
The third line adds an ITest to the registry with as concretetype Test that is also the default for ITest with a name of default and a setterdepency on IPerson that wants the default implementation of IPerson which we declared in line 2.
Line 4 creates the container.

Having done all this we can now see the result via console.writeline (our favorite testrunner ;-))

  1. Console.WriteLine(_container.GetInstance(Of ITest).Name)
  2.         Console.WriteLine(_container.GetInstance(Of ITest).Person.Name)
  3.         Console.WriteLine(_container.GetInstance(Of IPerson).Name)

This should give the following as a result.

test
Person
Person

Simple enough. Very usefull for when you want to inject your observable into a usercontrol. Usercontrols should have a default constructor for the designer, actually it is also the only constructor because the designer can have a difficult time otherwise. This is actually the only reason why I use Setterinjection instead of constructorinjection.

Leave a comment »Send a trackback » 178 views

VB.Net: A better configuration for structureMap

by chrissie1


Permalink 21 Sep 2008 01:19 , Categories: Microsoft Technologies, VB.NET Tags: structuremap, vb.net

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

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

and then this to get your implementation

  1. ObjectFactory.GetInstance(Of ITest)()

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

  1. StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
  2. ObjectFactory.GetInstance(Of ITest)
  3. StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
  4. 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.

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

Leave a comment »Send a trackback » 303 views