Module Scanning

210 views
Skip to first unread message

Cecil Phillip

unread,
Apr 4, 2012, 11:16:02 AM4/4/12
to aut...@googlegroups.com
I'm incorporating autofac into a project I'm working on and I want to split the registrations up into modules. Is there a built in way to handle module scanning? If not, what would be the best approach to implement this myself ?

Nicholas Blumhardt

unread,
Apr 5, 2012, 9:38:30 AM4/5/12
to aut...@googlegroups.com
Not sure how anyone feels about the following :) but I'd expect this should work:

var moduleFinder = new ContainerBuilder();
moduleFinder.RegisterAssemblyTypes(...).As<IModule>();
var moduleContainer = moduleFinder.Build();

var containerBuilder = new ContainerBuilder();
foreach (var module in moduleContainer.Resolve<IEnumerable<IModule>>())
{
    containerBuilder.RegisterModule(module);
}

// Configure/use containerBuilder...

Cheers,
Nick

On 4 April 2012 08:16, Cecil Phillip <cecilp...@gmail.com> wrote:
I'm incorporating autofac into a project I'm working on and I want to split the registrations up into modules. Is there a built in way to handle module scanning? If not, what would be the best approach to implement this myself ?

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/k8QKdbGYF_gJ.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.

Cecil Phillip

unread,
Apr 5, 2012, 11:55:29 AM4/5/12
to aut...@googlegroups.com
Thanks Nick.. Based on your code of course, I put together this extension method

      public static void RegisterAssemblyModules(this ContainerBuilder builder, params Assembly[] assemblies)
        {
            var moduleFinder = new ContainerBuilder();

            moduleFinder.RegisterAssemblyTypes(assemblies)
              .Where(t => typeof(IModule).IsAssignableFrom(t))
              .As<IModule>();

            using (var moduleContainer = moduleFinder.Build())
            {
                foreach (var m in moduleContainer.Resolve<IEnumerable<IModule>>())
                    builder.RegisterModule(m);
            }
        }

Nicholas Blumhardt

unread,
Apr 6, 2012, 1:29:46 AM4/6/12
to aut...@googlegroups.com
Looks good, we should probably provide this functionality out of the box.

If you're using the latest version (unless there's a bug lurking :)) you shouldn't need the "Where" clause in there - As() will automatically filter on compatibility.

Cheers,
Nick

            }
        }

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/jX1GPWeJiUMJ.

Cecil Phillip

unread,
Apr 6, 2012, 11:12:38 AM4/6/12
to aut...@googlegroups.com
Oh, I didn't know that. Is there a changelog for the 2.6.1?
That's good info to update the documentation with :)

Alex Meyer-Gleaves

unread,
Apr 9, 2012, 11:15:04 AM4/9/12
to aut...@googlegroups.com
Hi guys,

This functionality is now available out-of-the-box. Well, it will be after the next release anyway. :)


Register all IModule types:

[Test]
public void RegisterAssemblyModules()
{
var assembly = typeof(AComponent).Assembly;
var builder = new ContainerBuilder();
builder.RegisterAssemblyModules(assembly);
var container = builder.Build();

Assert.That(container.IsRegistered<AComponent>(), Is.True);
Assert.That(container.IsRegistered<BComponent>(), Is.True);
}

Register only certain module types:

[Test]
public void RegisterAssemblyModulesOfType()
{
var assembly = typeof(AComponent).Assembly;
var builder = new ContainerBuilder();
builder.RegisterAssemblyModules<AModule>(assembly);
var container = builder.Build();

Assert.That(container.IsRegistered<AComponent>(), Is.True);
Assert.That(container.IsRegistered<BComponent>(), Is.False);
}

Cheers,

Alex.

Cecil Phillip

unread,
Apr 9, 2012, 1:10:49 PM4/9/12
to aut...@googlegroups.com
That's amazingly awesome :)... Thank you for the update Sir

Nicholas Blumhardt

unread,
Apr 10, 2012, 1:32:54 AM4/10/12
to aut...@googlegroups.com
I think the change happened quite a few releases back now, possibly around 2.4. The commit log is the most detailed changelog ;)

On 6 April 2012 08:12, Cecil Phillip <cecilp...@gmail.com> wrote:
Oh, I didn't know that. Is there a changelog for the 2.6.1?
That's good info to update the documentation with :)

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/bg6GJJpEgKwJ.
Reply all
Reply to author
Forward
0 new messages