sâmbătă, 15 august 2009

IoC in .NET part 1: Autofac

This is an interesting IoC project that I used in a couple of occasions, nothing big, but I sure enjoyed it immensely.

Autofac is wrist friendly so to speak and it tries to solve your configuration problems in a simple and concise manner.

Without further ado go at http://code.google.com/p/autofac/ to get the latest version.

Next I’ll try to create a simple example that I hope we’ll use for the most part of this series.

In learning the inner workings of IoC containers we’ll develop a console application that provides some simple autocomplete for it’s features.

The features of our application are:

  • Autocomplete for internal commands
  • Commands
    • Alert – displays a message box
    • NewLine – goes to the next line
    • Calculator – with support for add

All of this features will be organized under the Components folder as functions and services.

In general you will want the concrete classes to be linked to an interface, and is by default the way I like to handle it.

A lot of voices in the community are against the use of xml files for configuration, and to that regard Autofac offers the option of using a fluent interface for configuring the builder. Although good for contracts that you are not likely to change without recompiling the application, when you need to have functionality similar to that of a plug-in you will prefer to use an IoC capable of reading xml configuration. Obviously Autofac has support for xml files as well.

Note xml configuration support is very limited in Autofac, so I’ll only show the bootstraping for this file

As I said earlier an IoC is just a form of factory, the only real difference is that this is a factory with nice configuration capabilities.

In more complex applications I recommend you use modules for configuring but for this example is sufficient to have everything configured in only one place.

As a preference I always configure the IoC I’m using in a class called Bootstrap in my highest level module ( commonly identified as the assembly containing the Main function, or if in a web app the assembly that contains the Global.asax).

The code for the Bootstrap.cs looks like this:

public class Bootstrap
{
public static IContainer Components(){
var builder = new ContainerBuilder();

builder.Register<ConsoleClearScreen>().As<IClearScreen>().SingletonScoped();
builder.Register<ConsoleWriteString>().As<IWriteString>().SingletonScoped();

builder.Register<AlarmFunctionState>().As<IFunctionState>().Named( "AlarmFunctionState" );
builder.Register<NewLineFunctionState>().As<IFunctionState>().Named( "NewLineTransition" );
builder.Register<CalculatorFunctionState>().As<IFunctionState>().Named( "CalculatorTransition" );


builder.Register<IConsoleInputService>(
c => new ConsoleInputServiceImpl(
new IFunctionState[]{
c.Resolve<IFunctionState>("AlarmFunctionState"),
c.Resolve<IFunctionState>("NewLineTransition"),
c.Resolve<IFunctionState>("CalculatorTransition")
} ) );

return builder.Build();
}
}


Autofac uses the ContainerBuilder class to register services and it offers a simple method of directly creating those services through the use of lambda expressions.



For the actual implementation of the app you can download the source code here.

Niciun comentariu: