Last active
June 15, 2022 11:11
-
-
Save savaged/61031bb914fa27e535df184c2d761bb4 to your computer and use it in GitHub Desktop.
Console App with dependency injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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