Click here to Skip to main content
13,510,146 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 28 Oct 2010

Simplified autofac Registrations

, 28 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Simplified autofac registrations

I'm building an application which will monitor other applications (memory using, start them if they crash, etc). I'm trying to stop re-inventing the wheel (ohhh, it’s fun to do everything yourself) and start using other components. This time, I needed an IoC container and autofac seemed like a good match for me.

To simplify IoC registration, I created an attribute which I use on all my components. In this way, I don't have to remember to register each component, it’s done with the help of the attribute.

Using the attribute without constructor parameters will register the component with all interfaces.

class MyComponent : IConsumer<MonitorEvent>, ISomeService

While using the constructor will only register one interface.

class MyComponent : IConsumer<MonitorEvent>, ISomeService

The autofac registration looks like this:

internal class Program
    public static IContainer Components { get; private set; }

    private static void Main(string[] args)
        var builder = new ContainerBuilder();

        //i = interface type, c = concrete type
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
        assemblies.Each(assembly => assembly.FindComponents((i, c) => 

        Components = builder.Build();

Quite smooth, huh?

Extension methods making it possible:

public static class ComponentFinder
        public static void Each<T>(this IEnumerable<T> enumerable, Action<T> action)
            foreach (T item in enumerable)

        public static void FindComponents
		(this Assembly assembly, Action<Type, Type> action)
            Type componentAttribute = typeof (ComponentAttribute);
            foreach (Type type in assembly.GetTypes())
                object[] attributes = type.GetCustomAttributes(componentAttribute, false);
                if (attributes.Length == 0)
                    type.GetInterfaces().Each(i => action(i, type));

                foreach (object attribute in attributes)
                    Type interfaceType = 
			((ComponentAttribute) attribute).InterfaceType ?? type;
                    action(interfaceType, type);

Getting components is done by the resolve method:

Program.Components.Resolve<IMyService>.SendMessage("Hello world");


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Founder 1TCompany AB
Sweden Sweden
Skip logfiles, try automated error handling!

I'm one of the founders of codeRR, a .NET service which takes care of everything related to exceptions, so that you can focus on writing code.

blog | twitter

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Michał Zalewski28-Oct-10 15:41
memberMichał Zalewski28-Oct-10 15:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180417.1 | Last Updated 28 Oct 2010
Article Copyright 2010 by jgauffin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid