flagsmith-dotnet-client icon indicating copy to clipboard operation
flagsmith-dotnet-client copied to clipboard

Add Aspire component

Open LeftTwixWand opened this issue 1 year ago • 1 comments

It would be amazing, if we can have Flagsmith Aspire component. I can provide some help in development of this aspire component.

Why?

  • There are such requests from the community dotnet/aspire#1570
  • It's the easiest way to start using Flagsmith with .NET
  • Improves Flagsmith adoption across community
  • Adds support for automatic DI registration

How it should look like

// AppHost.Program.cs

var builder = DistributedApplication.CreateBuilder(args);

var flagsmith = builder.AddFlagsmith();

var apiService = builder.AddProject<My_WebApi>("apiservice")
	.WithEnvironment(flagsmith); // Pupulates URL and EnvironmentKey
// ServiceDefaults.Extensions.cs

public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
      builder.AddFlagsmithClient(); // Registers FlagsmithClient into DI with URL and EnvironmentKey
}

Example of configuration with database

// AppHost.Program.cs

var builder = DistributedApplication.CreateBuilder(args);

var postgresServer = builder.AddPostgres("postgresserver")
	// Mount the SQL scripts directory into the container.
	// Init script would create a flagsmith database, becuase Aspire doesn't create DB by default yet.
	.WithContainerName("postgresserver")
	.WithBindMount("../Postgres", "/docker-entrypoint-initdb.d");

var flagsmithDatabase = postgresServer.AddDatabase("flagsmithDatabase", databaseName: "flagsmithdb");

var flagsmith = builder.AddFlagsmith()
	.WithReference(flagsmithDatabase) // Populates DATABASE_URL
	.WaitFor(flagsmithDatabase)

var apiService = builder.AddProject<My_WebApi>("apiservice")
	.WithEnvironment(flagsmith);

LeftTwixWand avatar Nov 29 '24 19:11 LeftTwixWand

Hi @LeftTwixWand, thanks for raising this and for sharing the code snippets.

I'm interested to know what this would look like without an official Flagsmith component? Is there an option to, for example, add generic components? Something like builder.AddGenericComponent(MyComponent); ?

Being completely frank here, it's not something that we have the bandwidth to develop or maintain in house, but if you want to submit a PR, or create your own repository to hold the code than of course we'd be more than happy to review / endorse it.

matthewelwell avatar Dec 02 '24 10:12 matthewelwell