Friday, July 16, 2010

Autofac Part 3 - Module registration and event handlers

This time we take a look at registration using modules, and using the event handlers provided by Autofac.

Registration using modules
Registration using modules allows more flexibility, reduced complexity, allows configuration parameters to be explicit and better abstraction and type safety.

To use module you have to create a class, which inherits from Autofac.Module and overrides the Load method, fx.:
class ModuleReg : Module
{
  protected override void Load(ContainerBuilder builder)
  {
    base.Load(builder);
    builder.RegisterType<object>();
  }
}
To register a module the following are done:
var builder = new ContainerBuilder();
builder.RegisterModule(new ModuleReg());
var container = builder.Build();
Now you can get all instances, types and everything else registered in the module, resolved just as normal. Modules can register anything you can outside a module, for example you can register more modules.

Modules can are more dynamic as you can set properties and such to indicate different scenarios. More information about module registration can be found here

Autofac events
There are a couple for events which can be handled. These event as described below.
Preparing
Fired before the activation process to allow parameters to be changed or an alternative instance to be provided.
Activating
Fires after the construction of an instance but before it is shared with any other or any members invoked on it.
Activated
Fire when the activation process for a new instance is complete.
Registered
Information about the occurrence of a component being registered with a container.
All events can be handlede by using the methods provided when registering something.
builder.RegisterType<object>().OnPreparing(somedelegate);
builder.RegisterType<object>().OnActivated(somedelegate);
builder.RegisterType<object>().OnActivating(somedelegate);
builder.RegisterType<object>().OnRegistered(somedelegate);
Multiple events can be handled like this:
builder.RegisterType<object>().OnPreparing(somedelegate)
  .OnActivated(somedelegate);
These events are poorly documented, so here is a quick intro. The OnPreparing method, takes this parameter:
Action<PreparingEventArgs>
And you can changed the instance parameters like this:
IEnumerable<Parameter> parameters = new Parameter[] { new NamedParameter("n", 1) };
builder.RegisterType<object>().OnPreparing(e => e.Parameters = parameters);
The OnActivating method take this parameter:
Action<IActivatingEventArgs<TLimit>>
And you can change the instance provided like this:
builder.RegisterType<object>().OnActivation(e => e.ReplaceInstance(new object());
The OnActivated method take this parameter:
Action<IActivatedEventArgs<TLimit>>
And you can use the instance provided like this:
builder.RegisterType<object>().OnActivated(e => e.Instance.Method());
And at last the OnRegistered method, which takes this parameters:
Action<ComponentRegisteredEventArgs>
In my opinion this event aren't very useful, but it is still included here for completeness. You can only get the ComponentRegistry and the ComponentRegistration from this event.
builder.RegisterType<object>().OnRegistered(e =>
  {
    registry = e.ComponentRegistry;
    cr = e.ComponentRegistration;
  });

2 comments: