This example demonstrates a multi-module train simulation application showcasing
modular dependency injection, service locator composition, and compile-time
verification using inject_compile.
The example consists of multiple feature modules and local service locator interfaces composed together:
lib/common.dart: Provides common system-wide dependencies (likeCarMaintenance).lib/bike.dart: Defines the bike car feature:BikeServiceLocator: A feature-specific locator interface.BikeServices: A module that declares how to instantiateBikeRack, specifying a dependency onCarMaintenancewhich it expects to be provided by a sibling module in the final injector.
lib/food.dart: Defines the food car feature, containingFoodServicesmodule andFoodServiceLocator.lib/locomotive.dart: The central module composition layer:TrainServices: The root injector annotated with@Injector([BikeServices, FoodServices, CommonServices])that implements the individual feature locator interfaces (BikeServiceLocator,FoodServiceLocator).
example/train_example.dart: The entry point that instantiates modules and initializes the locator.
Since inject_compile is a compile-time dependency injection library, you must
generate the boilerplate dependency wiring code before running the application.
-
Generate the DI code:
dart run build_runner build
This generates the
lib/locomotive.inject.dartfile. -
Run the application:
dart run example/train_example.dart