-
-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Marking UnsafeConfig unsafe for AOT, and adding more robust TypeConve…
…rterHelper, testing DI
- Loading branch information
1 parent
db3ce54
commit 602f7de
Showing
8 changed files
with
221 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
namespace DemoAot; | ||
|
||
public class InfoCommand(GreetingService greetingService) : Command<InfoCommand.Settings> | ||
{ | ||
public override int Execute(CommandContext context, Settings settings) | ||
{ | ||
greetingService.Greet("World"); | ||
return 0; | ||
} | ||
|
||
public class Settings : CommandSettings | ||
{ | ||
[CommandOption("-v")] | ||
public bool Verbose {get;set;} | ||
} | ||
} | ||
|
||
public class GreetingService(IAnsiConsole ansiConsole) | ||
{ | ||
public void Greet(string name) | ||
{ | ||
ansiConsole.WriteLine($"Hello {name}!"); | ||
} | ||
} |
This file contains 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
53 changes: 53 additions & 0 deletions
53
examples/Cli/DemoAot/Utilities/DependencyInjectionRegistrar.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Spectre.Console.Cli; | ||
|
||
namespace DemoAot.Utilities; | ||
|
||
public class DependencyInjectionRegistrar(IServiceCollection services) : ITypeRegistrar, IDisposable | ||
{ | ||
private IServiceCollection Services { get; } = services; | ||
private List<IDisposable> BuiltProviders { get; } = []; | ||
|
||
public ITypeResolver Build() | ||
{ | ||
var buildServiceProvider = Services.BuildServiceProvider(); | ||
BuiltProviders.Add(buildServiceProvider); | ||
return new DependencyInjectionResolver(buildServiceProvider); | ||
} | ||
|
||
public void Register(Type service, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type implementation) | ||
{ | ||
Services.AddSingleton(service, implementation); | ||
} | ||
|
||
public void RegisterInstance(Type service, object implementation) | ||
{ | ||
Services.AddSingleton(service, implementation); | ||
} | ||
|
||
public void RegisterLazy(Type service, Func<object> factory) | ||
{ | ||
Services.AddSingleton(service, _ => factory()); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
foreach (var provider in BuiltProviders) | ||
{ | ||
provider.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
internal class DependencyInjectionResolver(ServiceProvider serviceProvider) : ITypeResolver, IDisposable | ||
{ | ||
public void Dispose() => serviceProvider.Dispose(); | ||
|
||
public object Resolve(Type type) | ||
{ | ||
return serviceProvider.GetService(type); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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