# Laravel Container Examples These examples demonstrate how to use the Laravel-style dependency injection container in Dart. ## Basic Container Features (container_usage.dart) Shows core container features using a payment processing example: ```dart // Basic binding container.bind((c) => StripeGateway(c.make())); // Singleton registration container.singleton((c) => PaymentService(c.make())); // Contextual binding container.when(PaymentService) .needs() .give(PayPalGateway()); // Tagged services container.tag([StripeGateway, PayPalGateway], 'gateways'); final gateways = container.tagged('gateways'); // Instance extending container.extend(PaymentGateway, (gateway) { print('Payment gateway was created'); }); ``` Run with: ```bash dart run container_usage.dart ``` ## Advanced Container Features (advanced_usage.dart) Demonstrates advanced container capabilities: ```dart // Child containers final rootContainer = IlluminateContainer(reflector); rootContainer.singleton((c) => ProductionLogger()); final testContainer = rootContainer.createChild(); testContainer.singleton((c) => TestLogger()); // Scoped bindings container.scoped((c) => RequestScope()); container.bind((c) => UserRepository( c.make(), c.make() )); // Scope lifecycle final repo1 = container.make(); final repo2 = container.make(); assert(identical(repo1.scope, repo2.scope)); // Same scope container.forgetScopedInstances(); // Clear scope final repo3 = container.make(); assert(!identical(repo1.scope, repo3.scope)); // New scope ``` Run with: ```bash dart run advanced_usage.dart ``` ## Application Architecture (app_example.dart) Shows how to use the container in a real application: ```dart // Register repositories container.singleton((c) => DatabaseUserRepository()); // Register services container.singleton((c) => EmailService()); container.singleton((c) => UserService( c.make(), c.make() )); // Register controllers container.singleton((c) => UserController( c.make() )); // Use the application final controller = container.make(); controller.createUser('John Doe', 'john@example.com'); ``` Run with: ```bash dart run app_example.dart ``` ## Key Features Demonstrated 1. Dependency Registration - Basic bindings - Singleton registration - Factory bindings - Instance registration - Scoped bindings 2. Service Resolution - Automatic dependency injection - Contextual binding - Tagged services - Instance extending 3. Container Features - Child containers - Scope management - Service location - Interface binding 4. Architecture Patterns - Repository pattern - Service layer - Dependency injection - Interface segregation