Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide simple ServiceCollection.AddAutofac<TModule>() extension method #665

Closed
twsouthwick opened this issue Jul 24, 2015 · 1 comment
Closed

Comments

@twsouthwick
Copy link
Contributor

A lot of the APIs in the DNX frameworks are fluent. I was wondering if there would be any desire to have a method like the following:

using Autofac;
using Autofac.Dnx;
using System;

namespace Microsoft.Framework.DependencyInjection
{
    public static class AutofacExtensions
    {
        public static IServiceProvider AddAutofac<T>(this IServiceCollection collection)
            where T : Module, new()
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule<T>();
            builder.Populate(collection);

            var container = builder.Build();

            return container.Resolve<IServiceProvider>();
        }
    }
}

This way, a meta-module that contains all the registrations for the application can be loaded like this:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    return services.AddAutofac<TestModule>();
}

This does not require any usings to be added and the Startup class can be kept simpler than doing the registrations in it.

@tillig
Copy link
Member

tillig commented Nov 9, 2015

While this is an interesting idea, I think we will probably not be implementing this in core Autofac.

  • It's nice to be able to register all of your dependencies in one module, but in practice I've seen that most apps don't have just one central module registration.
  • We had a similar pattern in the OWIN integration package and we're finding that folks value control over exactly when things happen - registrations of dependencies, calling Populate, that sort of thing.

It sounds like something you may want to add to a project-specific extensions library or possibly create a public Autofac extensions library and publish it for the community to use.

Thanks for the idea, though!

@tillig tillig closed this as completed Nov 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants