# DI Container - Modular Structure

## Overview

The DI container has been refactored into a modular structure for better organization and maintainability.

## Directory Structure

```
├── container/          # Core container logic
│   ├── builder.ts      # Fluent API for building containers
│   ├── factory.ts      # Factory functions (legacy compatibility)
│   └── types.ts        # Type definitions
├── registrations/      # Service registration modules
│   ├── core.ts         # Core services (config, logger)
│   ├── cache.ts        # Cache services
│   ├── database.ts     # Database clients
│   └── service.ts      # Application services
├── config/             # Configuration management
│   └── schemas/        # Zod schemas for validation
├── factories/          # Service factories
│   └── cache.factory.ts # Cache factory utilities
└── utils/              # Utilities
    └── lifecycle.ts    # Service lifecycle management
```

## Usage Examples

### Using the Builder Pattern (Recommended)

```typescript
import { ServiceContainerBuilder } from '@stock-bot/di';

// Create container with fluent API
const container = await new ServiceContainerBuilder()
  .withConfig({
    redis: { host: 'localhost', port: 6379 },
    mongodb: { uri: 'mongodb://localhost', database: 'mydb' },
    postgres: { host: 'localhost', database: 'mydb', user: 'user', password: 'pass' }
  })
  .enableService('enableQueue', false) // Disable queue service
  .enableService('enableBrowser', false) // Disable browser service
  .build();

// Services are automatically initialized
const cache = container.cradle.cache;
const mongoClient = container.cradle.mongoClient;
```

### Creating Namespaced Caches

```typescript
import { CacheFactory } from '@stock-bot/di';

// Create a cache for a specific service
const serviceCache = CacheFactory.createCacheForService(container, 'myservice');

// Create a cache for a handler
const handlerCache = CacheFactory.createCacheForHandler(container, 'myhandler');

// Create a cache with custom prefix
const customCache = CacheFactory.createCacheWithPrefix(container, 'custom');
```

### Manual Service Lifecycle

```typescript
import { ServiceContainerBuilder, ServiceLifecycleManager } from '@stock-bot/di';

// Create container without auto-initialization
const container = await new ServiceContainerBuilder()
  .withConfig(config)
  .skipInitialization()
  .build();

// Manually initialize services
const lifecycle = new ServiceLifecycleManager();
await lifecycle.initializeServices(container);

// ... use services ...

// Manually shutdown services
await lifecycle.shutdownServices(container);
```

### Legacy API (Backward Compatible)

```typescript
import { createServiceContainerFromConfig } from '@stock-bot/di';

// Old way still works
const container = createServiceContainerFromConfig(appConfig, {
  enableQueue: true,
  enableCache: true
});

// Manual initialization required with legacy API
await initializeServices(container);
```

## Migration Guide

1. Replace direct container creation with `ServiceContainerBuilder`
2. Use `CacheFactory` instead of manually creating `NamespacedCache`
3. Let the builder handle service initialization automatically
4. Use typed configuration schemas for better validation