From 52936379e409d02d13a3bd815bb99a3990e981d4 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Fri, 30 May 2014 00:42:58 +0200 Subject: [PATCH] Autowire upon resolving instances --- source/poodinis/container.d | 6 +++++- test/poodinis/containertest.d | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 066a68e..ac5be1f 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -4,6 +4,7 @@ import std.string; import std.array; public import poodinis.registration; +public import poodinis.autowire; class RegistrationException : Exception { this(string message, TypeInfo registeredType, TypeInfo_Class instantiatableType) { @@ -64,7 +65,10 @@ class Container { if (!registration) { throw new ResolveException("Type not registered.", resolveType); } - return cast(ClassType) registration.getInstance(); + + ClassType instance = cast(ClassType) registration.getInstance(); + this.autowire!(ClassType)(instance); + return instance; } public void clearRegistrations() { diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index 772acb8..eed57d2 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -18,6 +18,14 @@ version(unittest) { } } + class AutowiredClass { + } + + class ComponentClass { + @Autowire + public AutowiredClass autowiredClass; + } + // Test register concrete type unittest { auto container = new Container(); @@ -121,4 +129,14 @@ version(unittest) { assert(expectedInstance is actualInstance, "Resolved instance from existing instance scope is not the same as the registered instance"); } + // Test autowire resolved instances + unittest { + auto container = new Container(); + container.register!AutowiredClass; + container.register!ComponentClass; + auto componentInstance = container.resolve!ComponentClass; + auto autowiredInstance = container.resolve!AutowiredClass; + assert(componentInstance.autowiredClass is autowiredInstance, "Member is not autowired upon resolving"); + } + } \ No newline at end of file