My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
VisualBasicExample  
IoC in VB.NET 9.0 is simple with Autofac.
Updated Aug 6, 2010 by nicholas...@gmail.com

Introduction

Visual Basic 9.0 (2008) provides all the language features needed to effectively use Autofac for dependency injection.

Vehicles

This example shows the syntax for registering a component with a lambda expression.

The Service

First, the service exposed by the component:

Public Interface IVehicle
  Sub TransportTo(ByVal destination As String)
End Interface

Services are usually represented by interfaces, like IVehicle in this example. It is also possible to use a regular class as a service.

The Component

Now, a 'car' implementation of IVehicle:

Public Class Car
  Implements IVehicle

  Public Sub TransportTo(ByVal destination As String)
    System.Console.WriteLine("Driving to " & destination & "... Vroom!")
  End Sub

End Class

Components are always classes. More than one component can provide a given service, and likewise a single component can provide multiple services (by implementing multiple interfaces.)

Wiring Up to the Container

Autofac is a container much like a HashTable or Dictionary that maps services to the components providing them.

Rather than adding the components themselves, however, we tell the container how to create them (Autofac terms this registration.)

Dim builder As Autofac.ContainerBuilder = New Autofac.ContainerBuilder()

builder.Register(GetType(Car)).As(GetType(IVehicle))

Dim container As Autofac.Container = builder.Build()

We can now use the container to access the IVehicle service without worrying about which class implements it:

Dim vehicle As IVehicle = container.Resolve(Of IVehicle)()
vehicle.TransportTo("Byron Bay")

The code above prints "Driving to Byron Bay... Vroom!".

Getting a service from the container is called resolving an instance - the container has the task of deciding whether to create a new instance or use an existing one, and if it creates an instance, then it must resolve the dependencies of that instance. Instructing the container how to behave is done at registration time, by setting scope and ownership.

If we'd registered Yacht instead of Car then the above code might print a message about sailing. We're free to change the implementation of IVehicle to any component that exposes it, without having to change the code that is using it.

Registering Expressions

The Register() example above uses the Car type and reflection to create cars. Let's say our Car class needs to be constructed with a colour for its paint job:

Public Class Car
  Implements IVehicle

  Public Sub New(ByVal colour As System.Drawing.Color)
    ...

The code we've written above won't work anymore, because Autofac doesn't know which colour the car should be.

To get around this, use a lambda expression to register the Car component:

builder.Register(Function(c) New Car(System.Drawing.Color.Orange)).As(GetType(IVehicle))

The parameter to the expression c provides access back into the container, so that Car can be initialised with other services that would themselves be resolved if necessary.

The call to Car.New() won't happen until we resolve an instance, however the parameters we've provided will be kept around until it is needed then. Using the IVehicle service still looks identical:

Dim vehicle As IVehicle = container.Resolve(Of IVehicle)()
vehicle.TransportTo("Byron Bay")
Comment by RobertMi...@gmail.com, Oct 31, 2009

Hello the examples are false,

in VB.NET typeof() is GetType? .. so the example should be

Dim builder As ContainerBuilder? = New ContainerBuilder?() builder.Register(GetType?(Car)).As(GetType?(IVehicle))

Comment by project member nicholas...@gmail.com, Nov 15, 2009

Thanks for picking that up Robert! I've updated the samples now.


Sign in to add a comment
Powered by Google Project Hosting