| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using Inversion;
- /**
- * Injection Demo Example
- *
- * This example demonstrates the inject<T>() method for property/field injection.
- * The inject method allows dependencies to be resolved during object construction
- * without requiring explicit constructor parameters.
- *
- * Key concepts:
- * - Using inject<T>() for field initialization
- * - The injection context is automatically set up during container resolution
- * - Multiple dependencies can be injected in the same class
- */
- public class InjectionDemoExample : Object {
- // Define service interfaces
- public interface IHandler : Object {
- public abstract void handle(string request);
- }
- public interface IRepository : Object {
- public abstract string get_data();
- }
- // Console handler implementation
- public class ConsoleHandler : Object, IHandler {
- public void handle(string request) {
- stdout.printf("[HANDLER] Processing: %s\n", request);
- }
- }
- // Memory repository implementation
- public class MemoryRepository : Object, IRepository {
- public string get_data() {
- return "Sample data from repository";
- }
- }
- // A service that uses inject<T>() for dependency injection
- // This class demonstrates field injection using the inject method
- public class Controller : Object {
- // Dependencies are injected during object construction
- private IHandler handler = inject<IHandler>();
- private IRepository repository = inject<IRepository>();
- public void process_request(string request) {
- var data = this.repository.get_data();
- stdout.printf("[CONTROLLER] Retrieved data: %s\n", data);
- this.handler.handle(request);
- }
- }
- // Another service demonstrating nested injection
- public class ApiService : Object {
- private Controller controller = inject<Controller>();
- public void execute(string request) {
- stdout.printf("[API] Executing request: %s\n", request);
- this.controller.process_request(request);
- }
- }
- public static int main(string[] args) {
- stdout.printf("=== Inversion IoC Injection Demo ===\n\n");
- // Create the container
- var container = new Container();
- // Register all services with factory delegates
- container.register_singleton<ConsoleHandler>((scope) => new ConsoleHandler())
- .as_type(typeof(IHandler));
- container.register_singleton<MemoryRepository>((scope) => new MemoryRepository())
- .as_type(typeof(IRepository));
- // Register Controller - it will use inject<T>() to resolve its dependencies
- container.register_scoped<Controller>((scope) => new Controller());
- // Register ApiService - demonstrates nested injection
- container.register_transient<ApiService>((scope) => new ApiService());
- stdout.printf("Registered services:\n");
- stdout.printf(" - ConsoleHandler as IHandler (Singleton)\n");
- stdout.printf(" - MemoryRepository as IRepository (Singleton)\n");
- stdout.printf(" - Controller (Scoped) - uses inject<T>()\n");
- stdout.printf(" - ApiService (Transient) - uses inject<T>()\n\n");
- // Create a scope
- var scope = container.create_scope();
- try {
- stdout.printf("--- Resolving ApiService (demonstrates nested injection) ---\n\n");
-
- // Resolve ApiService - this will trigger:
- // 1. ApiService construction -> inject<Controller>()
- // 2. Controller construction -> inject<IHandler>() and inject<IRepository>()
- var api_service = scope.resolve<ApiService>();
- api_service.execute("Test request #1");
- stdout.printf("\n--- Resolving Controller directly ---\n\n");
-
- // Resolve Controller directly
- var controller = scope.resolve<Controller>();
- controller.process_request("Test request #2");
- // Verify scoped behavior - same controller instance within scope
- var controller2 = scope.resolve<Controller>();
- if (controller == controller2) {
- stdout.printf("\n✓ Scoped working: Same Controller instance within scope\n");
- }
- stdout.printf("\n--- Creating another scope ---\n\n");
-
- // Create another scope - should get a new Controller instance
- var scope2 = container.create_scope();
- var controller_in_scope2 = scope2.resolve<Controller>();
- controller_in_scope2.process_request("Test request #3");
- if (controller != controller_in_scope2) {
- stdout.printf("\n✓ Scoped working: Different Controller instance in different scope\n");
- }
- } catch (Error e) {
- stderr.printf("Error: %s\n", e.message);
- return 1;
- }
- stdout.printf("\n=== Example Complete ===\n");
- return 0;
- }
- }
|