C# Autofac example

Date: 2020-01-24
using Autofac;

namespace AutoFacTester
{
    class Program
    {
        static void Main(string[] args)
        {
            Run(Wire());
        }

        static IContainer Wire()
        {
            var builder = new ContainerBuilder();

            //builder.RegisterAssemblyTypes(typeof(ConsoleOutput).Assembly)
            //    .Where(t => typeof(IStartable).IsAssignableFrom(t))
            //    .As<IStartable>()
            //    .SingleInstance();

            //builder.Register(c => new ConsoleOutput()).As<IOutput>().SingleInstance();
            builder.RegisterType<ConsoleOutput>().As<IOutput>().SingleInstance();
            builder.RegisterType<TodayWriter>().As<IDateWriter>();
            return builder.Build();
        }

        static void Run(IContainer container) 
        {
            using (var scope = container.BeginLifetimeScope())
            {
                var writer = scope.Resolve<IDateWriter>();
                writer.WriteDate();
            }
        }
    }
}
32490cookie-checkC# Autofac example