mspinelli (6) [Avatar] Offline
#1
I'm trying to develop a small sample project using Autofac and WPF. I'm not sure how/where to implement the composition root.

I've looked at your ProductManagementClient code example and read the corresponding area of the book. I've know that in VB the application.xmal.vb file can override the OnStartup method. I could put the following registration code there, but it doesn't seem to be the right place, especially since the Castle Windsor, Spring, and Unity code is being configured elsewhere. However, I don't know how to do something similar with Autofac (i.e. Autofac doesn't have the equivalent to IWindsorInstaller).

The container code I need to use currently looks like this (yes I realize there is no UI stuff here yet, the components being registered are service and domain layer components):

Imports Autofac

Class Application

... properties for Me.Builder, Me.DiContainer, and Me.ServiceServices

Protected Overrides Sub OnStartup(ByVal e As System.Windows.StartupEventArgs)

MyBase.OnStartup(e)

' Register (DI Step 1)
Me.Builder.
RegisterType(Of WindowsServiceControllerProvider)().
As(Of IWindowsServiceControllerProvider)()

Me.Builder.
RegisterType(Of WindowsServiceController)().
As(Of IWindowsServiceController)()

Me.Builder.
RegisterType(Of WindowsServiceServices)().
As(Of IWindowsServiceServices)()

Me.DiContainer = Me._Builder.Build()

' Resolve (DI Step 2)
Me.ServiceServices = Me.DiContainer.Resolve(Of IWindowsServiceServices)()

End Sub

End Class

I could put this in the Application.xmal.vb code behind, but I'd rather follow the better practice of not putting such there. I'm missing a conceptual piece to the puzzle.
mark.seemann (383) [Avatar] Offline
#2
Re: Composition Root, Autofac, WPF
Autofac does have something equivalent to IWindsorInstaller.- it's called a Module, and is described in section 13.1.3.

HTH