import 'package:platform_contracts/contracts.dart'; import 'package:platform_container/platform_container.dart'; // Example services abstract class Logger { void log(String message); } class ProductionLogger implements Logger { @override void log(String message) => print('PROD: $message'); } class TestLogger implements Logger { @override void log(String message) => print('TEST: $message'); } class RequestScope { final String id = DateTime.now().toIso8601String(); @override String toString() => 'RequestScope($id)'; } class UserRepository { final Logger logger; final RequestScope scope; UserRepository(this.logger, this.scope); void save(String userId) { logger.log('[${scope.id}] Saving user: $userId'); } } void main() { // Create root container final container = IlluminateContainer(ExampleReflector()); print('1. Child Containers'); print('------------------'); // Register production logger in root container.singleton((c) => ProductionLogger()); // Create child container with test logger final testContainer = container.createChild(); testContainer.singleton((c) => TestLogger()); // Use both containers container.make().log('Using production logger'); testContainer.make().log('Using test logger'); container.make().log('Still using production logger'); print('\n2. Scoped Bindings'); print('-----------------'); // Register scoped request container.scoped((c) { final scope = RequestScope(); print('Created new scope: $scope'); return scope; }); // Register repository that uses scope container.bind( (c) => UserRepository(c.make(), c.make())); print('\nFirst request:'); // Same scope within request final repo1 = container.make(); final repo2 = container.make(); repo1.save('user1'); repo2.save('user2'); // Verify same scope print('Using same scope: ${identical(repo1.scope, repo2.scope)}'); print('\nNew request:'); // Clear scope container.forgetScopedInstances(); // New scope for new request final repo3 = container.make(); repo3.save('user3'); // Verify different scope print('Using different scope: ${!identical(repo1.scope, repo3.scope)}'); } /// Example reflector implementation class ExampleReflector implements ReflectorContract { @override String? getName(Symbol symbol) => null; @override ReflectedClassContract? reflectClass(Type clazz) => null; @override ReflectedFunctionContract? reflectFunction(Function function) => null; @override ReflectedInstanceContract? reflectInstance(Object object) => null; @override ReflectedTypeContract reflectFutureOf(Type type) { throw UnsupportedError('Future reflection not needed for example'); } @override ReflectedTypeContract? reflectType(Type type) => null; }