Skip to content

Instantly share code, notes, and snippets.

@savaged
Last active June 15, 2022 11:11
Show Gist options
  • Select an option

  • Save savaged/61031bb914fa27e535df184c2d761bb4 to your computer and use it in GitHub Desktop.

Select an option

Save savaged/61031bb914fa27e535df184c2d761bb4 to your computer and use it in GitHub Desktop.
Console App with dependency injection
using Autofac;
using savaged.cli.io;
using savaged.cli.modules;
namespace savaged.cli
{
/// <Summary>
/// Usage in Program.cs
/// App.Current.MainModule.Run();
/// </Summary>
public sealed class App
{
private static readonly App _instance = new App();
private readonly ILifetimeScope _scope;
private App()
{
_scope = Build().BeginLifetimeScope();
MainModule = _scope.Resolve<MainModule>();
}
public static App Current => _instance;
public IModule MainModule { get; }
private IContainer Build()
{
var builder = new ContainerBuilder();
// One might create a class to output to the console (for example)
builder.RegisterType<COut>().As<ICOut>();
builder.RegisterType<MainModule>();
// There could be more modules
return builder.Build();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment