SteeltoeOSS / Documentation

The documentation, api browser, and getting started guides used for the Steeltoe's website.
Apache License 2.0
8 stars 23 forks source link

StreamHost Implementation of Streams is confusing #276

Closed Layla-P closed 2 years ago

Layla-P commented 2 years ago

Current implementation of Streams requires user to add the stream-related service to the StreamHost Builder, like so:

await StreamHost.CreateDefaultBuilder<ProducerVotingService>(args)
                 .ConfigureWebHostDefaults(webhostBuilder => webhostBuilder.UseStartup<Startup>())
                 .RunConsoleAsync();

under the hood this implementation of the generic host builder does the following:

  1. It calls the code below
    _hostBuilder = hostBuilder.AddSpringBootConfiguration()
                                .ConfigureServices((context, services) => services.AddStreamServices<T>(context.Configuration));
  2. which does this
    public static IHostBuilder AddStreamServices<T>(this IHostBuilder builder)
    {
        return builder.AddSpringBootConfiguration()
            .ConfigureServices((context, services) =>
            {
                services.AddStreamServices<T>(context.Configuration);
                services.AddHostedService<StreamLifeCycleService>();
            });
    }

IMO, this obfuscates what is actually happening and deviates from how other services are added with Steeltoe.

I think it would be better to primarily document adding Streams like so:

await Host.CreateDefaultBuilder(args)
                 .AddStreamServices<ProducerVotingService>()
                 .ConfigureWebHostDefaults(webhostBuilder => webhostBuilder.UseStartup<Startup>())
                 .RunConsoleAsync();

Which more easily supports the minimal hosting model like this:

using Microsoft.AspNetCore.Mvc;
using Steeltoe.Stream.Extensions;
using VoteProducer;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.AddStreamServices<ProducerVotingService>();
builder.Services.AddSingleton<ProducerVotingService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapGet("/weatherforecast", (ProducerVotingService vs, string choice)  =>
{
    var response = vs
                   .Record(new Vote { Choice = choice ?? "default" });
    return response? 200 : 500;
});

app.Run();

Then as an alternative implementation for a service that is solely meant to stream, offer the StreamHost implementation

Layla-P commented 2 years ago

Closing - opened in wrong repo