Inject dependency into Windows Form

Jan 8, 2010 at 6:08 AM

 

Hi All,
I may not have understood the concept of dependency injection but here is the question.
I have created a ServiceLocator class that configures the container with the dependent objects.
The startup object for my application is Form1.vb (lets say). I have set this in the project properties.
If i want to get hold of a LogWriter in Form1.Vb, how would i go about doing this?
the ServiceLocator looks like this
Private Shared Sub ConfigureContainer()
 
_container = New UnityContainer
        _container.AddNewExtension(Of EnterpriseLibraryCoreExtension)()
        _container.AddNewExtension(Of LoggingBlockExtension)()
        _container.RegisterType(Of IExtractor, CSONYExtractor)(Constants.SONY)
Private Shared Sub ConfigureContainer()
 _container = New UnityContainer
        _container.AddNewExtension(Of EnterpriseLibraryCoreExtension)()
        _container.AddNewExtension(Of LoggingBlockExtension)()
        _container.RegisterType(Of IExtractor, CSONYExtractor)(Constants.SONY)
end sub
In my client tools, this is what i have done.
    Private _logWriter As LogWriter
    Public Sub New(ByVal logWriter As LogWriter)
        _logWriter = logWriter
    End Sub
Any advice on this would be very helpful.
thanks, Hari.

Hi All,

 

I may not have understood the concept of dependency injection but here is the question.

 

I have created a ServiceLocator class that configures the container with the dependent objects.

 

The startup object for my application is Form1.vb (lets say). I have set this in the project properties.

 

If i want to get hold of a LogWriter in Form1.Vb, how would i go about doing this?

 

the ServiceLocator looks like this

 

Private Shared Sub ConfigureContainer()

 

 

 

_container = New UnityContainer

        _container.AddNewExtension(Of EnterpriseLibraryCoreExtension)()

        _container.AddNewExtension(Of LoggingBlockExtension)()

        _container.RegisterType(Of IExtractor, CSONYExtractor)(Constants.SONY)

 

Private Shared Sub ConfigureContainer()

 

 _container = New UnityContainer

 

        _container.AddNewExtension(Of EnterpriseLibraryCoreExtension)()

 

        _container.AddNewExtension(Of LoggingBlockExtension)()

 

        _container.RegisterType(Of IExtractor, CSONYExtractor)(Constants.SONY)

 

end sub

 

In my client tools, this is what i have done.

 

    Private _logWriter As LogWriter

 

    Public Sub New(ByVal logWriter As LogWriter)

 

        _logWriter = logWriter

 

    End Sub

 

Any advice on this would be very helpful.

thanks, Hari.

 

Jan 8, 2010 at 7:49 AM

Actually, you've got the basic idea, the problem is that you're getting tripped up by VB "helping" you. Dependencies get injected when the container creates an object. But when you set a form as your startup object, the container doesn't create it, the VB startup code does. So what you need to do is use a startup method instead, and in that call container.Resolve<Form1>(). Then set the resulting object as your main form and that should do it.

Sorry I can't be more specific, I haven't used VB.NET in quite a while. Does anyone else in the community have more VB experience that can help?

 

Jan 11, 2010 at 2:39 PM

Hey,

Thanks for the post. Your advice is very helpful.

Assume my Forms exist in a windows forms application. And i have referenced a Class library that has several class which use Logwriter.

The question is, where should the container exist?

A. Should it be in the Client end, where all the classes from the class library are loaded? If this is the case, how would i instantiate a Class (say) B in Class (say) A that exists within the class library?

B. Should there be two containers? One managing the classes in the Forms application and the other for the class library? 

Have I over-complicated my application by using Unity in an improper fashion? If my questions make sense, any advice on the rightful usage is much appreciated.

 

thanks, Hari