My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Scanning  
Autofac can use conventions to find and register components in assemblies.
Autofac2
Updated Aug 6, 2010 by nicholas...@gmail.com

Component Discovery

Otherwise known as convention-driven registration or scanning, Autofac 2 can register a set of types from an assembly according to user-specified rules:

var dataAccess = Assembly.GetExecutingAssembly();

builder.RegisterAssemblyTypes(dataAccess)
    .Where(t => t.Name.EndsWith("Repository"))
    .AsImplementedInterfaces();

Each RegisterAssemblyTypes() call will apply one set of rules only - multiple invocations of RegisterAssemblyTypes() are necessary if there are multiple different sets of components to register.

Selecting Types

RegisterAssemblyTypes() accepts an array of assemblies. By default, all public, concrete classes in the assembly will be registered.

To filter the types that are registered, use Where(), as in:

    Where(t => t.Name.EndsWith("Repository"))

To exclude types from scanning, use Except():

    Except<MyUnwantedType>()

Or, customise the registration for the excluded type with:

    Except<MyCustomisedType>(ct =>
        ct.As<ISpecial>().SingleInstance())

Multiple filters can be used, in which case they will be applied with logical AND.

Specifying Services

The registration syntax for RegisterAssemblyTypes() is a superset of the registration syntax for single types, so methods like As() all work with assemblies as well, e.g.:

    As<IRepository>()

Additional overloads to As() and Named() accept lambda expressions that determine, for a type, which services it will provide:

    As(t => t.GetInterfaces()[0])

As with normal component registrations, multiple calls to As() are added together.

Convenience Methods

A number of convenience methods make it easier to build up common conventions.

  • AsImplementedInterfaces() - register the type as providing all of its public interfaces as services
  • AsClosedTypesOf(open) - register types that are assignable to a closed instance of the open generic type
  • AsSelf() - the default: register types as themselves - useful when also overriding the default with another service specification
Comment by jim.bolla, Aug 27, 2010

I like selecting my types via "opt-in" by putting a custom attribute on the class:

[AttributeUsage(AttributeTargets.Class)]
public class RegisterServiceAttribute : Attribute {}

Then select the types like this:

builder.RegisterAssemblyTypes(assemblies)
    .Where(t => t.GetCustomAttributes(typeof (RegisterServiceAttribute), false).Any())
    .AsSelf()
    .AsImplementedInterfaces();
Comment by ICantFin...@gmail.com, Jan 27, 2011

A common one is AsDefaultInterface?. Instead of registering all the interfaces defined by the type I want to register IProductRepository for ProductRepository?. I added an extension method to do this. Have a look at https://gist.github.com/799706

While doing that I noticed that AsImplementedInterfaces? has a different behavior if it's applied to a type or assembly. For the latter it excludes IDisposable. It might be worth unifying this behavior (IMO IDisposable should be excluded in both cases).

Comment by project member nicholas...@gmail.com, Jan 27, 2011

Miguel, thanks for noticing the IDisposable issue, I'll add a note to the tracker.


Sign in to add a comment
Powered by Google Project Hosting