-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from xo-energy/feature-register-service
Refactor hosting extensions to resolve ICommandApp as a service
- Loading branch information
Showing
10 changed files
with
168 additions
and
118 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 Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace XO.Console.Cli; | ||
|
||
internal static class CommandAppFactory | ||
{ | ||
public static ICommandApp BuildCommandApp(IServiceProvider services) | ||
{ | ||
var context = services.GetRequiredService<HostBuilderContext>(); | ||
var lifetime = services.GetRequiredService<IHostApplicationLifetime>(); | ||
var resolver = new ServiceProviderTypeResolver(services); | ||
|
||
var optionsAccessor = services.GetService<IOptions<CommandAppBuilderOptions>>(); | ||
var options = optionsAccessor?.Value ?? new(); | ||
|
||
var builder = options.CommandAppBuilderFactory() | ||
.SetApplicationName(context.HostingEnvironment.ApplicationName) | ||
.UseTypeResolver(resolver); | ||
|
||
foreach (var action in options.ConfigureActions) | ||
action(context, builder); | ||
|
||
return builder.Build(); | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
XO.Console.Cli.Extensions/CommandAppServiceCollectionExtensions.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,83 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace XO.Console.Cli; | ||
|
||
/// <summary> | ||
/// Extension methods for adding <see cref="ICommandApp"/>-related services to a service collection. | ||
/// </summary> | ||
public static class CommandAppServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds <see cref="ICommandApp"/> to the service collection. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/> to configure.</param> | ||
/// <param name="configure">A delegate that configures <see cref="ICommandAppBuilder"/>.</param> | ||
/// <returns>The <see cref="IServiceCollection"/>.</returns> | ||
public static IServiceCollection AddCommandApp( | ||
this IServiceCollection services, | ||
Action<HostBuilderContext, ICommandAppBuilder> configure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(configure); | ||
|
||
return services.AddCommandApp(default, configure); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="ICommandApp"/> to the service collection with a default command. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/> to configure.</param> | ||
/// <param name="configure">A delegate that configures <see cref="ICommandAppBuilder"/>.</param> | ||
/// <typeparam name="TDefaultCommand">The command implementation type.</typeparam> | ||
/// <returns>The <see cref="IServiceCollection"/>.</returns> | ||
public static IServiceCollection AddCommandApp<TDefaultCommand>( | ||
this IServiceCollection services, | ||
Action<HostBuilderContext, ICommandAppBuilder>? configure = null) | ||
where TDefaultCommand : class, ICommand | ||
{ | ||
return services.AddCommandApp( | ||
CommandAppBuilder.WithDefaultCommand<TDefaultCommand>, | ||
configure); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="ICommandApp"/> to the service collection with a default command. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/> to configure.</param> | ||
/// <param name="executeAsync">The command implementation delegate.</param> | ||
/// <param name="configure">A delegate that configures <see cref="ICommandAppBuilder"/>.</param> | ||
/// <typeparam name="TParameters">A class whose properties describe the command parameters.</typeparam> | ||
/// <returns>The <see cref="IServiceCollection"/>.</returns> | ||
public static IServiceCollection AddCommandApp<TParameters>( | ||
this IServiceCollection services, | ||
Func<ICommandContext, TParameters, CancellationToken, Task<int>> executeAsync, | ||
Action<HostBuilderContext, ICommandAppBuilder>? configure = null) | ||
where TParameters : CommandParameters | ||
{ | ||
ArgumentNullException.ThrowIfNull(executeAsync); | ||
|
||
return services.AddCommandApp( | ||
() => CommandAppBuilder.WithDefaultCommand(executeAsync), | ||
configure); | ||
} | ||
|
||
internal static IServiceCollection AddCommandApp( | ||
this IServiceCollection services, | ||
Func<ICommandAppBuilder>? builderFactory, | ||
Action<HostBuilderContext, ICommandAppBuilder>? configure) | ||
{ | ||
var optionsBuilder = services.AddOptions<CommandAppBuilderOptions>(); | ||
|
||
if (builderFactory != null) | ||
optionsBuilder.Configure(options => options.CommandAppBuilderFactory = builderFactory); | ||
|
||
if (configure != null) | ||
optionsBuilder.Configure(options => options.ConfigureActions.Add(configure)); | ||
|
||
services.TryAddSingleton( | ||
static services => CommandAppFactory.BuildCommandApp(services)); | ||
|
||
return services; | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
XO.Console.Cli.Extensions/HostingCommandAppBuilderExtensions.cs
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.