Skip to content

Extract WithClearDatabaseCommand into MongoDbResourceBuilderExtensions #259

Description

@mpaulosky

Parent

Closes part of #258 — Refactor AppHost MongoDB Dev Commands into WithMongoDbDevCommands Extension

What to build

Move the 120-line inline WithCommand block from AppHost.cs into a new MongoDbResourceBuilderExtensions static class in the same directory. The class exposes a single WithMongoDbDevCommands(databaseName) extension method on IResourceBuilder<MongoDBServerResource>.

The method owns the IsRunMode guard (caller invokes unconditionally), creates a shared SemaphoreSlim(1,1), and registers the existing clear-data command under the same name it has today. AppHost.cs shrinks from ~157 lines to ~30 lines with a single mongo.WithMongoDbDevCommands("myblog") call replacing the entire inline block.

The clear-data command behavior must be preserved exactly: non-blocking semaphore (WaitAsync(0)), connection-string resolution, skip system.* collections, DeleteManyAsync (preserves shells), per-collection try/catch with warnings, per-collection count in the result message, and the health-gate UpdateState callback. The namespace is Aspire.Hosting so no using is needed at the call site.

Acceptance criteria

  • src/AppHost/MongoDbResourceBuilderExtensions.cs exists with internal static class MongoDbResourceBuilderExtensions in the Aspire.Hosting namespace
  • WithMongoDbDevCommands(string databaseName) is the only public surface; inner methods (WithClearDatabaseCommand, etc.) are private helpers
  • AppHost.cs contains a single mongo.WithMongoDbDevCommands("myblog") call and no inline command logic
  • IsRunMode check lives inside the method — caller invokes unconditionally
  • SemaphoreSlim(1,1) is created inside WithMongoDbDevCommands and captured by the clear command closure
  • All 5 existing MongoDbClearCommandTests unit tests pass without modification
  • All 5 existing MongoClearDataIntegrationTests integration tests pass without modification
  • Build succeeds (dotnet build) and no new warnings introduced

Blocked by

None — can start immediately

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgo:needs-researchNeeds investigationsquadSquad triage inbox — Lead will assign to a membersquad:samAssigned to Sam (Backend / .NET)type:choreMaintenance, refactoring, cleanup

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions