Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

Tags: setterinjection

comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

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.

About the Author

User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
575 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback