| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using Inversion;
- using Invercargill;
- /**
- * Multi-Registration Example
- *
- * This example demonstrates:
- * - Registering multiple implementations for the same interface
- * - Using resolve_all<T>() to get all implementations
- * - Using custom factory delegates
- */
- public class MultiRegistrationExample : Object {
- // Define a handler interface
- public interface IHandler : Object {
- public abstract string name { get; }
- public abstract void handle(string request);
- }
- // First handler implementation
- public class AuthHandler : Object, IHandler {
- public string name { get { return "Auth"; } }
- public void handle(string request) {
- stdout.printf(" [AuthHandler] Processing: %s\n", request);
- }
- }
- // Second handler implementation
- public class LoggingHandler : Object, IHandler {
- public string name { get { return "Logging"; } }
- public void handle(string request) {
- stdout.printf(" [LoggingHandler] Processing: %s\n", request);
- }
- }
- // Third handler implementation with custom configuration
- public class CacheHandler : Object, IHandler {
- private int ttl_seconds;
- public CacheHandler(int ttl_seconds) {
- this.ttl_seconds = ttl_seconds;
- }
- public string name { get { return "Cache"; } }
- public void handle(string request) {
- stdout.printf(" [CacheHandler] Processing: %s (TTL: %ds)\n", request, this.ttl_seconds);
- }
- }
- public static int main(string[] args) {
- stdout.printf("=== Inversion IoC Multi-Registration Example ===\n\n");
- var container = new Container();
- // Register multiple handlers for the same interface with factory delegates
- container.register_singleton<AuthHandler>((scope) => new AuthHandler())
- .as_type(typeof(IHandler));
- stdout.printf("Registered AuthHandler as IHandler (Singleton)\n");
- container.register_singleton<LoggingHandler>((scope) => new LoggingHandler())
- .as_type(typeof(IHandler));
- stdout.printf("Registered LoggingHandler as IHandler (Singleton)\n");
- // Register with a custom factory delegate that requires parameters
- container.register_singleton<CacheHandler>((scope) => {
- return new CacheHandler(300); // 5 minute TTL
- })
- .as_type(typeof(IHandler));
- stdout.printf("Registered CacheHandler as IHandler (with custom factory)\n\n");
- // Create a scope
- var scope = container.create_scope();
- // Resolve a single handler (gets any one)
- stdout.printf("--- Resolving Single Handler ---\n");
- try {
- var handler = scope.resolve<IHandler>();
- stdout.printf("Got single handler: %s\n", handler.name);
- } catch (Error e) {
- stderr.printf("Error: %s\n", e.message);
- }
- // Resolve all handlers
- stdout.printf("\n--- Resolving All Handlers ---\n");
- var handlers = scope.resolve_all<IHandler>();
-
- stdout.printf("Processing request through all handlers:\n");
- handlers.iterate((obj) => {
- var handler = (IHandler) obj;
- handler.handle("GET /api/users");
- });
- // Show count
- var count = handlers.count();
- stdout.printf("\nTotal handlers resolved: %u\n", count);
- stdout.printf("\n=== Multi-Registration Example Complete ===\n");
- return 0;
- }
- }
|