MultiRegistration.vala 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Inversion;
  2. using Invercargill;
  3. /**
  4. * Multi-Registration Example
  5. *
  6. * This example demonstrates:
  7. * - Registering multiple implementations for the same interface
  8. * - Using resolve_all<T>() to get all implementations
  9. * - Using custom factory delegates
  10. */
  11. public class MultiRegistrationExample : Object {
  12. // Define a handler interface
  13. public interface IHandler : Object {
  14. public abstract string name { get; }
  15. public abstract void handle(string request);
  16. }
  17. // First handler implementation
  18. public class AuthHandler : Object, IHandler {
  19. public string name { get { return "Auth"; } }
  20. public void handle(string request) {
  21. stdout.printf(" [AuthHandler] Processing: %s\n", request);
  22. }
  23. }
  24. // Second handler implementation
  25. public class LoggingHandler : Object, IHandler {
  26. public string name { get { return "Logging"; } }
  27. public void handle(string request) {
  28. stdout.printf(" [LoggingHandler] Processing: %s\n", request);
  29. }
  30. }
  31. // Third handler implementation with custom configuration
  32. public class CacheHandler : Object, IHandler {
  33. private int ttl_seconds;
  34. public CacheHandler(int ttl_seconds) {
  35. this.ttl_seconds = ttl_seconds;
  36. }
  37. public string name { get { return "Cache"; } }
  38. public void handle(string request) {
  39. stdout.printf(" [CacheHandler] Processing: %s (TTL: %ds)\n", request, this.ttl_seconds);
  40. }
  41. }
  42. public static int main(string[] args) {
  43. stdout.printf("=== Inversion IoC Multi-Registration Example ===\n\n");
  44. var container = new Container();
  45. // Register multiple handlers for the same interface with factory delegates
  46. container.register_singleton<AuthHandler>((scope) => new AuthHandler())
  47. .as_type(typeof(IHandler));
  48. stdout.printf("Registered AuthHandler as IHandler (Singleton)\n");
  49. container.register_singleton<LoggingHandler>((scope) => new LoggingHandler())
  50. .as_type(typeof(IHandler));
  51. stdout.printf("Registered LoggingHandler as IHandler (Singleton)\n");
  52. // Register with a custom factory delegate that requires parameters
  53. container.register_singleton<CacheHandler>((scope) => {
  54. return new CacheHandler(300); // 5 minute TTL
  55. })
  56. .as_type(typeof(IHandler));
  57. stdout.printf("Registered CacheHandler as IHandler (with custom factory)\n\n");
  58. // Create a scope
  59. var scope = container.create_scope();
  60. // Resolve a single handler (gets any one)
  61. stdout.printf("--- Resolving Single Handler ---\n");
  62. try {
  63. var handler = scope.resolve<IHandler>();
  64. stdout.printf("Got single handler: %s\n", handler.name);
  65. } catch (Error e) {
  66. stderr.printf("Error: %s\n", e.message);
  67. }
  68. // Resolve all handlers
  69. stdout.printf("\n--- Resolving All Handlers ---\n");
  70. var handlers = scope.resolve_all<IHandler>();
  71. stdout.printf("Processing request through all handlers:\n");
  72. handlers.iterate((obj) => {
  73. var handler = (IHandler) obj;
  74. handler.handle("GET /api/users");
  75. });
  76. // Show count
  77. var count = handlers.count();
  78. stdout.printf("\nTotal handlers resolved: %u\n", count);
  79. stdout.printf("\n=== Multi-Registration Example Complete ===\n");
  80. return 0;
  81. }
  82. }