|
| 1 | +using System; |
| 2 | +using ArcadeManager; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Spectre.Console.Cli; |
| 5 | + |
| 6 | +namespace ArcadeManager.Console; |
| 7 | + |
| 8 | +// copied from github.com/spectreconsole/examples/blob/main/examples/Cli/Injection/Infrastructure/TypeRegistrar.cs |
| 9 | +public sealed class TypeRegistrar : ITypeRegistrar |
| 10 | +{ |
| 11 | + private readonly IServiceCollection builder; |
| 12 | + |
| 13 | + public TypeRegistrar() |
| 14 | + { |
| 15 | + builder = new ServiceCollection(); |
| 16 | + |
| 17 | + // environment |
| 18 | + builder.AddSingleton<IEnvironment, ConsoleEnvironment>(); |
| 19 | + |
| 20 | + // infrastructure |
| 21 | + builder.AddSingleton<Infrastructure.IWebClientFactory, Infrastructure.WebClientFactory>(); |
| 22 | + builder.AddSingleton<Infrastructure.IFileSystem, Infrastructure.FileSystem>(); |
| 23 | + builder.AddSingleton<Infrastructure.IDatFile, Infrastructure.DatFile>(); |
| 24 | + |
| 25 | + // services |
| 26 | + builder.AddSingleton<Services.IDownloader, Services.Downloader>(); |
| 27 | + builder.AddSingleton<Services.ICsv, Services.Csv>(); |
| 28 | + builder.AddSingleton<Services.IOverlays, Services.Overlays>(); |
| 29 | + builder.AddSingleton<Services.IRoms, Services.Roms>(); |
| 30 | + builder.AddSingleton<Services.IUpdater, Services.Updater>(); |
| 31 | + builder.AddSingleton<Services.ILocalizer, Services.Localizer>(); |
| 32 | + builder.AddSingleton<Services.IWizard, Services.Wizard>(); |
| 33 | + builder.AddSingleton<Services.IDatChecker, Services.DatChecker>(); |
| 34 | + builder.AddSingleton<Services.IServiceProvider, Services.ServiceProvider>(); |
| 35 | + |
| 36 | + builder.AddSingleton<IMessageHandler, ConsoleMessageHandler>(); |
| 37 | + } |
| 38 | + |
| 39 | + public ITypeResolver Build() |
| 40 | + { |
| 41 | + return new TypeResolver(builder.BuildServiceProvider()); |
| 42 | + } |
| 43 | + |
| 44 | + public void Register(Type service, Type implementation) |
| 45 | + { |
| 46 | + builder.AddSingleton(service, implementation); |
| 47 | + } |
| 48 | + |
| 49 | + public void RegisterInstance(Type service, object implementation) |
| 50 | + { |
| 51 | + builder.AddSingleton(service, implementation); |
| 52 | + } |
| 53 | + |
| 54 | + public void RegisterLazy(Type service, Func<object> factory) |
| 55 | + { |
| 56 | + ArgumentNullException.ThrowIfNull(factory); |
| 57 | + |
| 58 | + builder.AddSingleton(service, (provider) => factory()); |
| 59 | + } |
| 60 | +} |
0 commit comments