From 4d62f3fdb012e4d0e66e25c5b88a55b79d7910b4 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Thu, 24 Dec 2015 21:50:12 +0100 Subject: [PATCH] Add registering component factories as prototype Which makes them the opposite of singletons --- source/poodinis/context.d | 12 +++++++++++- test/poodinis/contexttest.d | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/source/poodinis/context.d b/source/poodinis/context.d index 1941231..8f67f58 100644 --- a/source/poodinis/context.d +++ b/source/poodinis/context.d @@ -36,15 +36,25 @@ struct RegisterByType(Type) { Type type; } +/** +* Components with the prototype registration will be scoped as dependencies which will created +* new instances every time they are resolved. The factory method will be called repeatedly. +*/ +struct Prototype {} + public void registerContextComponents(ApplicationContextType : ApplicationContext)(ApplicationContextType context, shared(DependencyContainer) container) { foreach (member ; __traits(allMembers, ApplicationContextType)) { static if (hasUDA!(__traits(getMember, context, member), Component)) { + auto factoryMethod = &__traits(getMember, context, member); Registration registration = null; + auto createsSingleton = CreatesSingleton.yes; foreach(attribute; __traits(getAttributes, __traits(getMember, context, member))) { static if (is(attribute == RegisterByType!T, T)) { registration = container.register!(typeof(attribute.type), ReturnType!factoryMethod); + } else static if (__traits(isSame, attribute, Prototype)) { + createsSingleton = CreatesSingleton.no; } } @@ -52,7 +62,7 @@ public void registerContextComponents(ApplicationContextType : ApplicationContex registration = container.register!(ReturnType!factoryMethod); } - registration.instanceFactory = new InstanceFactory(registration.instanceType, CreatesSingleton.yes, null, factoryMethod); + registration.instanceFactory = new InstanceFactory(registration.instanceType, createsSingleton, null, factoryMethod); } } } diff --git a/test/poodinis/contexttest.d b/test/poodinis/contexttest.d index c9b467f..2d3c2f5 100644 --- a/test/poodinis/contexttest.d +++ b/test/poodinis/contexttest.d @@ -47,6 +47,8 @@ version(unittest) { } } + class PieChart {} + class TestContext : ApplicationContext { @Component @@ -75,6 +77,12 @@ version(unittest) { public Wolf wolf() { return new Wolf(); } + + @Component + @Prototype + public PieChart pieChart() { + return new PieChart(); + } } //Test register component registrations from context @@ -117,4 +125,17 @@ version(unittest) { assert(wolf.getYell() == "Wooooooooooo"); } + //Test register component as prototype + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + auto context = new TestContext(); + context.registerContextComponents(container); + + auto firstInstance = container.resolve!PieChart; + auto secondInstance = container.resolve!PieChart; + + assert(firstInstance !is null && secondInstance !is null); + assert(firstInstance !is secondInstance); + } + }