The proper way to use UnityContainer? Newbie ask

Jun 17, 2010 at 3:12 AM

Let's say I have class A, B and C
A depends on B
B depends on C

I'd declared like this

Public Class A
    Private _b As B
    <Dependency()> Public Property B() As B
        Get
            Return _b
        End Get
        Set(ByVal value As B)
            _b = value
        End Set
    End Property
End Class

Public Class B
    Private _c As C
    <Dependency()> Public Property C() As C
        Get
            Return _c
        End Get
        Set(ByVal value As C)
            _c = value
        End Set
    End Property
End Class

Public Class C
End Class

What I expect is whenever I created an instance of A.
There should be an instance of B and C automatically created consequently.
Firstly I wrote something like

Dim Container = New UnityContainer
Container.RegisterType(Of C)(New InjectionFactory(Function(c) New C))
Container.RegisterType(Of B)(New InjectionFactory(Function(c) c.Resolve(Of B)()))
Dim myA = Container.Resolve(Of A)()


But the code above causes StackOverflowException.
I don't know how to fix it.
Lastly I have to write code like this

Dim Container = New UnityContainer
Dim myC = New C
Container.RegisterInstance(GetType(C), myC)
Dim myB = Container.Resolve(Of B)()
Container.RegisterInstance(GetType(B), myB)
Dim myA = Container.Resolve(Of A)()
Which looks very smell and seems pointless to use UnityContainer.
So can somebody show me the proper way to write it?
Thanks.

Jun 17, 2010 at 5:28 AM

Your stack overflow is pretty obviously due to this line:

Container.RegisterType(Of B)(New InjectionFactory(Function(c) c.Resolve(Of B)()))

This is telling the container, "When resolving a B, call into the container to resolve a B." Boom, infinite loop and stack overflow.

With this particular set of classes, you don't need to register B or C at all - you don't have any constructor parameters, and your properties are already decorated with attributes, so the container already has enough information to properly create the objects. Just do:

Dim Container = New UnityContainer
Dim myA = Container.Resolve(Of A)()

And I expect you'll find it working.

Also, in general for simple constructors, you don't need to use InjectionFactory. Use either the default (calls the longest constructor) or InjectionConstructor (to choose which constructor to call). This is all in the documentation.

 

Jul 30, 2012 at 10:24 PM

"This is all in the documentation."

LOL - Have you seen the "Documentation" page:

This project does not have documentation yet. Visit the Discussions tab to ask questions.
Jul 30, 2012 at 11:28 PM

To be fair, there is documentation, although not on the "documentation page".  The downloads page does have documentation in a variety of formats.  Also, the documentation is available online at MSDN.  But, point taken, perhaps that page should contain pointers to the actual documentation.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jul 31, 2012 at 12:15 AM

Yes, there is documentation. I was just pointing out the irony of the "documentation page", which is where I would go first if someone pointed me at documentation. I thought I had put a link in the post, but I must have messed it up.

Jul 31, 2012 at 12:57 AM

Yes, I see the irony.  :)

The Documentation page is part of Codeplex and is a wiki for creating documentation pages.  Unity and Enterprise Library have extensive documentation which are authored outside of Codeplex so it doesn't make sense to put it there. 

I've updated the Documentation page with pointers to the docs.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com