import 'package:angel_container/angel_container.dart'; import 'package:angel_container/mirrors.dart'; void main() { // Create a container instance. var container = new Container(MirrorsReflector()); // Register a singleton. container.registerSingleton(Engine(40)); // Register a factory that creates a truck. container.registerFactory((container) { return _TruckImpl(container.make()); }); // Use `make` to create an instance. var truck = container.make(); // Should print: 'Vroom! I have 40 horsepower in my engine.' truck.drive(); // We can make a child container with its own factory. var childContainer = container.createChild(); childContainer.registerFactory((container) { return _TruckImpl(Engine(5666)); }); // Make a truck with 5666 HP. childContainer.make().drive(); // However, calling `make` will return the Engine singleton we created above. print(childContainer.make().horsePower); } abstract class Truck { void drive(); } class Engine { final int horsePower; Engine(this.horsePower); } class _TruckImpl implements Truck { final Engine engine; _TruckImpl(this.engine); @override void drive() { print('Vroom! I have ${engine.horsePower} horsepower in my engine.'); } }