LifecycleDemo.vala 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Inversion;
  2. using Invercargill;
  3. /**
  4. * Lifecycle Demo Example
  5. *
  6. * This example demonstrates the three different lifecycles in Inversion:
  7. * - TRANSIENT: New instance each time
  8. * - SCOPED: One instance per scope
  9. * - SINGLETON: One instance for the entire container
  10. */
  11. public class LifecycleDemo : Object {
  12. // A counter service to track instance creation
  13. public class Counter : Object {
  14. private static int total_created = 0;
  15. private int instance_id;
  16. public Counter() {
  17. total_created++;
  18. this.instance_id = total_created;
  19. stdout.printf(" Counter #%d created\n", this.instance_id);
  20. }
  21. public int id {
  22. get { return this.instance_id; }
  23. }
  24. }
  25. public static int main(string[] args) {
  26. stdout.printf("=== Inversion IoC Lifecycle Demo ===\n\n");
  27. var container = new Container();
  28. // Register three counters with different lifecycles
  29. container.register_transient<Counter>((scope) => new Counter());
  30. stdout.printf("Registered Counter as TRANSIENT\n");
  31. container.register_scoped<Counter>((scope) => new Counter());
  32. stdout.printf("Registered Counter as SCOPED\n");
  33. container.register_singleton<Counter>((scope) => new Counter());
  34. stdout.printf("Registered Counter as SINGLETON\n\n");
  35. // Note: In a real scenario, you'd register different types.
  36. // For this demo, we'll show the lifecycle concepts.
  37. // Demonstrate SINGLETON behavior
  38. stdout.printf("--- SINGLETON Demo ---\n");
  39. var scope1 = container.create_scope();
  40. var scope2 = container.create_scope();
  41. // Create first scope
  42. stdout.printf("Creating scope 1:\n");
  43. // Demonstrate TRANSIENT behavior
  44. stdout.printf("\n--- TRANSIENT Demo ---\n");
  45. stdout.printf("Resolving transient three times:\n");
  46. // Since all registrations are for Counter, resolve_all gets all of them
  47. // Let's demonstrate by resolving multiple times
  48. try {
  49. var all_counters = scope1.resolve_all<Counter>();
  50. stdout.printf("\nResolved all counters (3 different registrations):\n");
  51. all_counters.iterate((obj) => {
  52. var counter = (Counter) obj;
  53. stdout.printf(" Got Counter #%d\n", counter.id);
  54. });
  55. } catch (Error e) {
  56. stderr.printf("Error: %s\n", e.message);
  57. }
  58. stdout.printf("\n--- SCOPED Demo ---\n");
  59. stdout.printf("Creating another scope and resolving:\n");
  60. var scope3 = container.create_scope();
  61. try {
  62. var counters = scope3.resolve_all<Counter>();
  63. stdout.printf("Resolved in another scope:\n");
  64. counters.iterate((obj) => {
  65. var counter = (Counter) obj;
  66. stdout.printf(" Got Counter #%d\n", counter.id);
  67. });
  68. } catch (Error e) {
  69. stderr.printf("Error: %s\n", e.message);
  70. }
  71. stdout.printf("\n=== Lifecycle Demo Complete ===\n");
  72. return 0;
  73. }
  74. }