Skip to content

Upgrade Task Repository from In-Memory to Azure Cosmos DB #12

@shawnewallace

Description

@shawnewallace

Overview

Upgrade the current InMemoryTaskRepository implementation to use Azure Cosmos DB as the persistent data store. This change will demonstrate best practices for database integration in Clean Architecture while maintaining domain-driven design principles.

Background

The current implementation uses an in-memory dictionary (Dictionary<TaskId, DomainTask>) in TaskManager.Infrastructure/Repositories/InMemoryTaskRepository.cs. This is suitable for demos and testing but not production-ready.

Goals

  • Replace in-memory storage with Azure Cosmos DB
  • Maintain Clean Architecture boundaries (Domain → Application → Infrastructure → API)
  • Follow DDD repository pattern with business-intent method names
  • Support proper async/await patterns throughout
  • Add appropriate configuration and dependency injection
  • Ensure comprehensive test coverage

Acceptance Criteria

Code Changes (C# - @shawnewallace)

  • Create CosmosDbTaskRepository implementing ITaskRepository
  • Add Cosmos DB configuration to appsettings.json and appsettings.Development.json
  • Update DI registration in ServiceExtensions.cs to use Cosmos DB repository
  • Add proper error handling and logging using ILogger<T>
  • Implement partition key strategy (suggested: /taskId or /status)
  • Add necessary NuGet packages (Microsoft.Azure.Cosmos or Azure.Cosmos)
  • Update README with Cosmos DB configuration instructions
  • Add unit tests for CosmosDbTaskRepository using Testcontainers or emulator
  • Add integration tests validating end-to-end functionality

Infrastructure Changes (Bicep - @mcollier)

  • Create infra/ directory structure
  • Create main.bicep for Cosmos DB account provisioning
  • Create cosmos.bicep module for Cosmos DB configuration
  • Add parameter files for multiple environments (dev, staging, prod)
  • Create App Service Bicep module for API hosting
  • Document deployment instructions in infra/README.md
  • Set up environment-specific configuration

Technical Requirements

Cosmos DB Configuration

{
  "CosmosDb": {
    "AccountEndpoint": "https://<account-name>.documents.azure.com:443/",
    "DatabaseName": "TaskManagerDb",
    "ContainerName": "Tasks",
    "PartitionKeyPath": "/taskId"
  }
}

Repository Pattern Compliance

  • Maintain existing method signatures in ITaskRepository
  • Use business-intent method names (not generic CRUD)
  • Support CancellationToken for all async operations
  • Return domain entities, not data transfer objects

Testing Strategy

  • Unit tests with mocked Cosmos SDK client
  • Integration tests with Cosmos DB Emulator or Testcontainers
  • Verify partition key strategy prevents hot partitions
  • Test concurrent write scenarios

Implementation Approach

Phase 1: Setup & Configuration

  1. Add NuGet packages to TaskManager.Infrastructure.csproj
  2. Create configuration classes and options pattern
  3. Set up dependency injection

Phase 2: Repository Implementation

  1. Create CosmosDbTaskRepository class
  2. Implement each ITaskRepository method
  3. Add proper error handling and logging
  4. Handle Cosmos DB-specific concerns (partition keys, consistency)

Phase 3: Testing

  1. Write unit tests with mocked Cosmos client
  2. Write integration tests with Cosmos DB Emulator
  3. Validate against existing acceptance tests

Phase 4: Infrastructure

  1. Create Bicep templates for Cosmos DB provisioning
  2. Set up multi-environment support
  3. Document deployment process

Out of Scope

  • Data migration from in-memory to Cosmos DB (fresh start)
  • Advanced Cosmos DB features (change feed, stored procedures)
  • Multi-region replication (can be added later)

Dependencies

  • Azure subscription for Cosmos DB provisioning
  • Cosmos DB Emulator for local development/testing
  • Bicep CLI for infrastructure deployment

References

Notes

This work item is part of the demo/coi branch demonstration. Both code and infrastructure changes will be developed in parallel by @shawnewallace (C# code) and @mcollier (Bicep infrastructure) and merged via pull requests.


Demo Focus Areas:

  • Agent planning mode and work item decomposition
  • Parallel development workflow
  • Custom Copilot instructions for consistency
  • Code review automation with /check command
  • Infrastructure as Code with Bicep best practices

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions