Skip to content

Refactor Request: Use a more standard hosting syntax #26

@DericHuynh

Description

@DericHuynh

Currently the hosting syntax looks similar to the following:

await AppHost.RunAsync<App>(null, builder =>
{
    builder.ConfigureServices(services =>
    {
        services.AddSingleton<ILoggerManager, LoggerManager>();

    });
});

This has the following issues:

  1. The order of operations is out of order which is slightly confusing, such that RunAsync comes first but runs last, with the builder inside doing the most heavy lifting.
  2. Syntax is not like modern builders, and is more similar to the obsolete IHostBuilder generic host with actions, rather than the fluent application builder style that superceded that syntax.
  3. Heavy nesting makes it harder to read and reduces flexibility in cross-collection configuration, such as if you want to get a service from service collection and use that elsewhere, such as IConfiguration.

I propose we switch to the following syntax more inline with modern application builders:

// Creates a builder with arguments already supplied, adding them to IConfiguration through builder.Configuration.AddCommandLine(args) or some IOptions alternative.
ConsoleAppBuilder builder = RazorConsole.CreateBuilder(args);

// Add services to the container.
builder.Services.AddSingleton<ILoggerManager, LoggerManager>();
builder.Services.AddSingleton<WeatherForecastService>();

// Resolves dependencies, configuration and creates the console app.
ConsoleApp<App> app = builder.Build<App>();

// Other app configuration now that services are instantiated for app.Services.GetService().

// App is now running and rendering.
await app.RunAsync(token: default);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions