Search code examples
c#.net-coredependency-injectionautofac

How do I Register Autofac Module with Configuration


I have a console application with a reference to a class library which in itself has a reference to a class library (waterfall). The problem is I want to register my IConfiguration by passing it via the Autofac modules and then having them accessible in the lowest library and I am not sure how to achieve this. (The point of my design is to have a standalone NuGet package which can be referenced by console app or website)

The point is I want to register the main .dll (Notification.Send) in my new website and not have to worry about registering the lower lever .dll (Notification.Queue) yet still have access to the websites appsettings.json. Here is my example

console application

    class Program
    {
        static void Main(string[] args)
        {
            IConfiguration Configuration = new ConfigurationBuilder()
               .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
               .AddEnvironmentVariables()
               .AddCommandLine(args)
               .Build();

            var builder = new ContainerBuilder();
            //How to pass "Configuration" to SendNotificationModule?
            builder.RegisterModule(new SendNotificationModule());
        }
    }

Notifications.Send (Library)

    public class SendNotificationModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            //How to pass "Configuration" to QueueModule?
            builder.RegisterModule(new QueueModule());
        }
    }

Notifications.Queue (Library)

    public class QueueModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            //Here I want to register a custom interface which I can use to retrieve config from
            //and then use for DI in my Notifications.Queue dll

        }
    }

I'm not too familiar with Autofac and perhaps barking up the wrong tree but I have searched everywhere but cant figure this out.


Solution

  • Why don't you pass it through the module constructors?

    class Program
    {
        static void Main(string[] args)
        {
            var configuration = // ..
            builder.RegisterModule(new SendNotificationModule(configuration));
        }
    }
    

    Notifications.Send (Library)

    public class SendNotificationModule : Module
    {
        private readonly IConfiguration configuration;
    
        public SendNotificationModule(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
    
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);
            builder.RegisterModule(new QueueModule(configuration));
        }
    }
    

    Notifications.Queue (Library)

    public class QueueModule : Module
    {
        private readonly IConfiguration configuration;
    
        public QueueModule (IConfiguration configuration)
        {
            this.configuration = configuration;
        }
    
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);
    
            //Here I want to register a custom interface which I can use to retrieve config from
            //and then use for DI in my Notifications.Queue dll
    
        }
    }