Autoformat code according to The D Style

This commit is contained in:
Mike Bierlee 2021-05-01 22:16:44 +03:00
parent 2b40ab95cf
commit dcaa4d77be
26 changed files with 1534 additions and 856 deletions

View file

@ -12,47 +12,53 @@ import std.digest.md;
import std.stdio; import std.stdio;
import std.conv; import std.conv;
class SecurityAuditor { class SecurityAuditor
public void submitAudit() { {
public void submitAudit()
{
writeln("Hmmmyes I have received your audit. It is.... adequate."); writeln("Hmmmyes I have received your audit. It is.... adequate.");
} }
} }
class SuperSecurityDevice { class SuperSecurityDevice
{
private int seed; private int seed;
public this() { public this()
{
auto randomGenerator = Random(unpredictableSeed); auto randomGenerator = Random(unpredictableSeed);
seed = uniform(0, 999, randomGenerator); seed = uniform(0, 999, randomGenerator);
} }
public string getPassword() { public string getPassword()
{
return to!string(seed) ~ "t1m3sp13!!:"; return to!string(seed) ~ "t1m3sp13!!:";
} }
} }
class SecurityManager { class SecurityManager
@Autowire {
private SuperSecurityDevice levelOneSecurity; @Autowire private SuperSecurityDevice levelOneSecurity;
@Autowire @Autowire @AssignNewInstance private SuperSecurityDevice levelTwoSecurity;
@AssignNewInstance
private SuperSecurityDevice levelTwoSecurity;
@Autowire @Autowire @OptionalDependency private SecurityAuditor auditor;
@OptionalDependency
private SecurityAuditor auditor;
public void doAudit() { public void doAudit()
if (auditor !is null) { {
if (auditor !is null)
{
auditor.submitAudit(); auditor.submitAudit();
} else { }
else
{
writeln("I uh, will skip the audit for now..."); writeln("I uh, will skip the audit for now...");
} }
} }
} }
void main() { void main()
{
auto dependencies = new shared DependencyContainer(); auto dependencies = new shared DependencyContainer();
dependencies.register!SuperSecurityDevice; // Registered with the default "Single instance" scope dependencies.register!SuperSecurityDevice; // Registered with the default "Single instance" scope
dependencies.register!SecurityManager; dependencies.register!SecurityManager;
@ -62,9 +68,12 @@ void main() {
writeln("Password for user one: " ~ manager.levelOneSecurity.getPassword()); writeln("Password for user one: " ~ manager.levelOneSecurity.getPassword());
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword()); writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
if (manager.levelOneSecurity is manager.levelTwoSecurity) { if (manager.levelOneSecurity is manager.levelTwoSecurity)
{
writeln("SECURITY BREACH!!!!!"); // Should not be printed since levelTwoSecurity is a new instance. writeln("SECURITY BREACH!!!!!"); // Should not be printed since levelTwoSecurity is a new instance.
} else { }
else
{
writeln("Security okay!"); writeln("Security okay!");
} }

View file

@ -9,57 +9,64 @@ import poodinis;
import std.stdio; import std.stdio;
class TownSquare { class TownSquare
{
@Autowire @Autowire private MarketStall marketStall;
private MarketStall marketStall;
public void makeSound() { public void makeSound()
{
marketStall.announceGoodsForSale(); marketStall.announceGoodsForSale();
} }
} }
interface Goods { interface Goods
{
public string getGoodsName(); public string getGoodsName();
} }
class Fish : Goods { class Fish : Goods
public override string getGoodsName() { {
public override string getGoodsName()
{
return "Fish"; return "Fish";
} }
} }
class MarketStall { class MarketStall
{
private Goods goods; private Goods goods;
this(Goods goods) { this(Goods goods)
{
this.goods = goods; this.goods = goods;
} }
public void announceGoodsForSale() { public void announceGoodsForSale()
{
writeln(goods.getGoodsName() ~ " for sale!"); writeln(goods.getGoodsName() ~ " for sale!");
} }
} }
class ExampleApplicationContext : ApplicationContext { class ExampleApplicationContext : ApplicationContext
{
@Autowire @Autowire private Goods goods;
private Goods goods;
public override void registerDependencies(shared(DependencyContainer) container) { public override void registerDependencies(shared(DependencyContainer) container)
{
container.register!(Goods, Fish); container.register!(Goods, Fish);
container.register!TownSquare; container.register!TownSquare;
} }
@Component @Component public MarketStall marketStall()
public MarketStall marketStall() { {
return new MarketStall(goods); return new MarketStall(goods);
} }
} }
void main() { void main()
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.registerContext!ExampleApplicationContext; container.registerContext!ExampleApplicationContext;

View file

@ -9,40 +9,50 @@ import poodinis;
import std.stdio; import std.stdio;
interface Pie { interface Pie
{
public void eat(); public void eat();
} }
class BlueBerryPie : Pie { class BlueBerryPie : Pie
public override void eat() { {
public override void eat()
{
writeln("Nom nom nom. I like this one!"); writeln("Nom nom nom. I like this one!");
} }
} }
class ApplePie : Pie { class ApplePie : Pie
public override void eat() { {
public override void eat()
{
writeln("Nom nom nom. These aren't real apples..."); writeln("Nom nom nom. These aren't real apples...");
} }
} }
class CardboardBoxPie : Pie { class CardboardBoxPie : Pie
public override void eat() { {
public override void eat()
{
writeln("Nom nom nom. This... is not a pie."); writeln("Nom nom nom. This... is not a pie.");
} }
} }
class PieEater { class PieEater
@Autowire {
private Pie[] pies; @Autowire private Pie[] pies;
public void eatThemAll() { public void eatThemAll()
foreach(pie ; pies) { {
foreach (pie; pies)
{
pie.eat(); pie.eat();
} }
} }
} }
void main() { void main()
{
auto dependencies = new shared DependencyContainer(); auto dependencies = new shared DependencyContainer();
dependencies.register!(Pie, BlueBerryPie); dependencies.register!(Pie, BlueBerryPie);
dependencies.register!(Pie, ApplePie); dependencies.register!(Pie, ApplePie);

View file

@ -5,54 +5,66 @@
* The full terms of the license can be found in the LICENSE file. * The full terms of the license can be found in the LICENSE file.
*/ */
import std.stdio; import std.stdio;
class Scheduler { class Scheduler
{
private Calendar calendar; private Calendar calendar;
// All parameters will autmatically be assigned when Scheduler is created. // All parameters will autmatically be assigned when Scheduler is created.
this(Calendar calendar) { this(Calendar calendar)
{
this.calendar = calendar; this.calendar = calendar;
} }
public void scheduleJob() { public void scheduleJob()
{
calendar.findOpenDate(); calendar.findOpenDate();
} }
} }
class Calendar { class Calendar
{
private HardwareClock hardwareClock; private HardwareClock hardwareClock;
// This constructor contains built-in type "int" and thus will not be used. // This constructor contains built-in type "int" and thus will not be used.
this(int initialDateTimeStamp, HardwareClock hardwareClock) { this(int initialDateTimeStamp, HardwareClock hardwareClock)
{
} }
// This constructor is chosen instead as candidate for injection when Calendar is created. // This constructor is chosen instead as candidate for injection when Calendar is created.
this(HardwareClock hardwareClock) { this(HardwareClock hardwareClock)
{
this.hardwareClock = hardwareClock; this.hardwareClock = hardwareClock;
} }
public void findOpenDate() { public void findOpenDate()
{
hardwareClock.doThings(); hardwareClock.doThings();
} }
} }
class HardwareClock { class HardwareClock
{
// Parameterless constructors will halt any further selection of constructors. // Parameterless constructors will halt any further selection of constructors.
this() {} this()
{
}
// As a result, this constructor will not be used when HardwareClock is created. // As a result, this constructor will not be used when HardwareClock is created.
this(Calendar calendar) { this(Calendar calendar)
{
throw new Exception("This constructor should not be used by Poodinis"); throw new Exception("This constructor should not be used by Poodinis");
} }
public void doThings() { public void doThings()
{
writeln("Things are being done!"); writeln("Things are being done!");
} }
} }
void main() { void main()
{
import poodinis; // Locally imported to emphasize that classes do not depend on Poodinis. import poodinis; // Locally imported to emphasize that classes do not depend on Poodinis.
auto dependencies = new shared DependencyContainer(); auto dependencies = new shared DependencyContainer();

View file

@ -9,38 +9,44 @@ import poodinis;
import std.stdio; import std.stdio;
class ADependency { class ADependency
@PostConstruct {
public void postConstructor() { @PostConstruct public void postConstructor()
{
writeln("The dependency is created."); writeln("The dependency is created.");
} }
public void callMe() { public void callMe()
{
writeln("The dependency was called."); writeln("The dependency was called.");
} }
} }
class AClass { class AClass
@Autowire {
public ADependency dependency; // Dependencies are autowired before the post-constructor is called. @Autowire public ADependency dependency; // Dependencies are autowired before the post-constructor is called.
@PostConstruct @PostConstruct public void postConstructor()
public void postConstructor() { {
writeln("The class is created."); writeln("The class is created.");
if (dependency !is null) { if (dependency !is null)
{
writeln("The dependency is autowired."); writeln("The dependency is autowired.");
} else { }
else
{
writeln("The dependency was NOT autowired."); writeln("The dependency was NOT autowired.");
} }
} }
@PreDestroy @PreDestroy public void preDestructor()
public void preDestructor() { {
writeln("The class is no longer registered with the container."); writeln("The class is no longer registered with the container.");
} }
} }
public void main() { public void main()
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(ADependency).onConstructed((Object obj) { container.register!(ADependency).onConstructed((Object obj) {
writeln("ADependency constructed"); writeln("ADependency constructed");

View file

@ -9,41 +9,50 @@ import poodinis;
import std.stdio; import std.stdio;
interface Engine { interface Engine
{
public void engage(); public void engage();
} }
class FuelEngine : Engine { class FuelEngine : Engine
public void engage() { {
public void engage()
{
writeln("VROOOOOOM!"); writeln("VROOOOOOM!");
} }
} }
class ElectricEngine : Engine { class ElectricEngine : Engine
public void engage() { {
public void engage()
{
writeln("hummmmmmmm...."); writeln("hummmmmmmm....");
} }
} }
class HybridCar { class HybridCar
{
alias KilometersPerHour = int; alias KilometersPerHour = int;
@Autowire!FuelEngine @Autowire!FuelEngine private Engine fuelEngine;
private Engine fuelEngine;
@Autowire!ElectricEngine @Autowire!ElectricEngine private Engine electricEngine;
private Engine electricEngine;
public void moveAtSpeed(KilometersPerHour speed) { public void moveAtSpeed(KilometersPerHour speed)
if (speed <= 45) { {
if (speed <= 45)
{
electricEngine.engage(); electricEngine.engage();
} else { }
else
{
fuelEngine.engage(); fuelEngine.engage();
} }
} }
} }
void main() { void main()
{
auto dependencies = new shared DependencyContainer(); auto dependencies = new shared DependencyContainer();
dependencies.register!HybridCar; dependencies.register!HybridCar;

View file

@ -7,24 +7,31 @@
import poodinis; import poodinis;
class Driver {} class Driver
{
}
interface Database {}; interface Database
{
}
class RelationalDatabase : Database { class RelationalDatabase : Database
{
private Driver driver; private Driver driver;
this(Driver driver) { // Automatically injected on creation by container this(Driver driver)
{ // Automatically injected on creation by container
this.driver = driver; this.driver = driver;
} }
} }
class DataWriter { class DataWriter
@Autowire {
private Database database; // Automatically injected when class is resolved @Autowire private Database database; // Automatically injected when class is resolved
} }
void main() { void main()
{
auto dependencies = new shared DependencyContainer(); auto dependencies = new shared DependencyContainer();
dependencies.register!Driver; dependencies.register!Driver;
dependencies.register!DataWriter; dependencies.register!DataWriter;

View file

@ -7,25 +7,28 @@
import poodinis; import poodinis;
class Violin { class Violin
{
} }
interface InstrumentPlayer { interface InstrumentPlayer
{
} }
class ViolinPlayer : InstrumentPlayer { class ViolinPlayer : InstrumentPlayer
{
// Autowired concrete types can be registered on resolve // Autowired concrete types can be registered on resolve
@Autowire @Autowire private Violin violin;
private Violin violin;
} }
class Orchestra { class Orchestra
{
// Autowired non-concrete types can be registered on resolved, given they have a qualifier. // Autowired non-concrete types can be registered on resolved, given they have a qualifier.
@Autowire!ViolinPlayer @Autowire!ViolinPlayer private InstrumentPlayer violinPlayer;
private InstrumentPlayer violinPlayer;
} }
void main() { void main()
{
auto dependencies = new shared DependencyContainer(); auto dependencies = new shared DependencyContainer();
/* /*

View file

@ -10,31 +10,38 @@ import poodinis;
import std.stdio; import std.stdio;
import std.string; import std.string;
class IntValueInjector : ValueInjector!int { class IntValueInjector : ValueInjector!int
int get(string key) { {
switch(key) { int get(string key)
case "http.port": {
return 8080; switch (key)
case "http.keep_alive": {
return 60; case "http.port":
default: return 8080;
throw new ValueNotAvailableException(key); case "http.keep_alive":
return 60;
default:
throw new ValueNotAvailableException(key);
} }
} }
} }
class StringValueInjector : ValueInjector!string { class StringValueInjector : ValueInjector!string
string get(string key) { {
switch(key) { string get(string key)
case "http.hostname": {
return "acme.org"; switch (key)
default: {
throw new ValueNotAvailableException(key); case "http.hostname":
return "acme.org";
default:
throw new ValueNotAvailableException(key);
} }
} }
} }
class HttpServer { class HttpServer
{
@Value("http.port") @Value("http.port")
private int port = 80; private int port = 80;
@ -48,12 +55,15 @@ class HttpServer {
@MandatoryValue("http.keep_alive") @MandatoryValue("http.keep_alive")
private int keepAliveTime; // A ResolveException is thrown when the value is not available, default assignments are not used. private int keepAliveTime; // A ResolveException is thrown when the value is not available, default assignments are not used.
public void serve() { public void serve()
writeln(format("Serving pages for %s:%s with max connection count of %s", hostName, port, maxConnections)); {
writeln(format("Serving pages for %s:%s with max connection count of %s",
hostName, port, maxConnections));
} }
} }
void main() { void main()
{
auto dependencies = new shared DependencyContainer(); auto dependencies = new shared DependencyContainer();
dependencies.register!(ValueInjector!int, IntValueInjector); dependencies.register!(ValueInjector!int, IntValueInjector);
dependencies.register!(ValueInjector!string, StringValueInjector); dependencies.register!(ValueInjector!string, StringValueInjector);

View file

@ -20,7 +20,8 @@ module poodinis.autowire;
import poodinis.container : DependencyContainer, PreDestroy, ResolveException, ResolveOption; import poodinis.container : DependencyContainer, PreDestroy, ResolveException, ResolveOption;
import poodinis.registration : Registration, InstantiationContext; import poodinis.registration : Registration, InstantiationContext;
import poodinis.factory : InstanceFactory, InstanceFactoryParameters, CreatesSingleton; import poodinis.factory : InstanceFactory, InstanceFactoryParameters, CreatesSingleton;
import poodinis.valueinjection : ValueInjector, ValueInjectionException, ValueNotAvailableException, Value, MandatoryValue; import poodinis.valueinjection : ValueInjector, ValueInjectionException,
ValueNotAvailableException, Value, MandatoryValue;
import poodinis.altphobos : isFunction; import poodinis.altphobos : isFunction;
import poodinis.imports : createImportsString; import poodinis.imports : createImportsString;
@ -29,11 +30,14 @@ import std.string : format;
import std.traits : BaseClassesTuple, FieldNameTuple, fullyQualifiedName, hasUDA, isDynamicArray; import std.traits : BaseClassesTuple, FieldNameTuple, fullyQualifiedName, hasUDA, isDynamicArray;
import std.range : ElementType; import std.range : ElementType;
debug { debug
{
import std.stdio : writeln; import std.stdio : writeln;
} }
private struct UseMemberType {} private struct UseMemberType
{
}
/** /**
* UDA for annotating class members as candidates for autowiring. * UDA for annotating class members as candidates for autowiring.
@ -68,7 +72,8 @@ private struct UseMemberType {}
* autowire member "fuelEngine" as if it's of type "FuelEngine". This means that the members of instance "fuelEngine" * autowire member "fuelEngine" as if it's of type "FuelEngine". This means that the members of instance "fuelEngine"
* will also be autowired because the autowire mechanism knows that member "fuelEngine" is an instance of "FuelEngine" * will also be autowired because the autowire mechanism knows that member "fuelEngine" is an instance of "FuelEngine"
*/ */
struct Autowire(QualifierType) { struct Autowire(QualifierType)
{
QualifierType qualifier; QualifierType qualifier;
} }
@ -77,7 +82,9 @@ struct Autowire(QualifierType) {
* Optional dependencies will not lead to a resolveException when there is no type registered for them. * Optional dependencies will not lead to a resolveException when there is no type registered for them.
* The member will remain null. * The member will remain null.
*/ */
struct OptionalDependency {} struct OptionalDependency
{
}
/** /**
* UDA for annotating class members to be autowired with a new instance regardless of their registration scope. * UDA for annotating class members to be autowired with a new instance regardless of their registration scope.
@ -92,10 +99,14 @@ struct OptionalDependency {}
*--- *---
* antenna will always be assigned a new instance of class Antenna. * antenna will always be assigned a new instance of class Antenna.
*/ */
struct AssignNewInstance {} struct AssignNewInstance
{
}
private void printDebugAutowiredInstance(TypeInfo instanceType, void* instanceAddress) { private void printDebugAutowiredInstance(TypeInfo instanceType, void* instanceAddress)
debug { {
debug
{
writeln(format("DEBUG: Autowiring members of [%s@%s]", instanceType, instanceAddress)); writeln(format("DEBUG: Autowiring members of [%s@%s]", instanceType, instanceAddress));
} }
} }
@ -109,135 +120,207 @@ private void printDebugAutowiredInstance(TypeInfo instanceType, void* instanceAd
* *
* See_Also: Autowire * See_Also: Autowire
*/ */
public void autowire(Type)(shared(DependencyContainer) container, Type instance) { public void autowire(Type)(shared(DependencyContainer) container, Type instance)
debug(poodinisVerbose) { {
debug (poodinisVerbose)
{
printDebugAutowiredInstance(typeid(Type), &instance); printDebugAutowiredInstance(typeid(Type), &instance);
} }
// Recurse into base class if there are more between Type and Object in the hierarchy // Recurse into base class if there are more between Type and Object in the hierarchy
static if(BaseClassesTuple!Type.length > 1) static if (BaseClassesTuple!Type.length > 1)
{ {
autowire!(BaseClassesTuple!Type[0])(container, instance); autowire!(BaseClassesTuple!Type[0])(container, instance);
} }
foreach(index, name; FieldNameTuple!Type) { foreach (index, name; FieldNameTuple!Type)
{
autowireMember!(name, index, Type)(container, instance); autowireMember!(name, index, Type)(container, instance);
} }
} }
private void printDebugAutowiringCandidate(TypeInfo candidateInstanceType, void* candidateInstanceAddress, TypeInfo instanceType, void* instanceAddress, string member) { private void printDebugAutowiringCandidate(TypeInfo candidateInstanceType,
debug { void* candidateInstanceAddress, TypeInfo instanceType, void* instanceAddress, string member)
writeln(format("DEBUG: Autowired instance [%s@%s] to [%s@%s].%s", candidateInstanceType, candidateInstanceAddress, instanceType, instanceAddress, member)); {
debug
{
writeln(format("DEBUG: Autowired instance [%s@%s] to [%s@%s].%s", candidateInstanceType,
candidateInstanceAddress, instanceType, instanceAddress, member));
} }
} }
private void printDebugAutowiringArray(TypeInfo superTypeInfo, TypeInfo instanceType, void* instanceAddress, string member) { private void printDebugAutowiringArray(TypeInfo superTypeInfo,
debug { TypeInfo instanceType, void* instanceAddress, string member)
writeln(format("DEBUG: Autowired all registered instances of super type %s to [%s@%s].%s", superTypeInfo, instanceType, instanceAddress, member)); {
debug
{
writeln(format("DEBUG: Autowired all registered instances of super type %s to [%s@%s].%s",
superTypeInfo, instanceType, instanceAddress, member));
} }
} }
private void autowireMember(string member, size_t memberIndex, Type)(shared(DependencyContainer) container, Type instance) { private void autowireMember(string member, size_t memberIndex, Type)(
foreach(attribute; __traits(getAttributes, Type.tupleof[memberIndex])) { shared(DependencyContainer) container, Type instance)
static if (is(attribute == Autowire!T, T)) { {
foreach (attribute; __traits(getAttributes, Type.tupleof[memberIndex]))
{
static if (is(attribute == Autowire!T, T))
{
injectInstance!(member, memberIndex, typeof(attribute.qualifier))(container, instance); injectInstance!(member, memberIndex, typeof(attribute.qualifier))(container, instance);
} else static if (__traits(isSame, attribute, Autowire)) { }
else static if (__traits(isSame, attribute, Autowire))
{
injectInstance!(member, memberIndex, UseMemberType)(container, instance); injectInstance!(member, memberIndex, UseMemberType)(container, instance);
} else static if (is(typeof(attribute) == Value)) { }
else static if (is(typeof(attribute) == Value))
{
enum key = attribute.key; enum key = attribute.key;
injectValue!(member, memberIndex, key, false)(container, instance); injectValue!(member, memberIndex, key, false)(container, instance);
} else static if (is(typeof(attribute) == MandatoryValue)) { }
else static if (is(typeof(attribute) == MandatoryValue))
{
enum key = attribute.key; enum key = attribute.key;
injectValue!(member, memberIndex, key, true)(container, instance); injectValue!(member, memberIndex, key, true)(container, instance);
} }
} }
} }
private void injectInstance(string member, size_t memberIndex, QualifierType, Type)(shared(DependencyContainer) container, Type instance) { private void injectInstance(string member, size_t memberIndex, QualifierType, Type)(
if (instance.tupleof[memberIndex] is null) { shared(DependencyContainer) container, Type instance)
{
if (instance.tupleof[memberIndex] is null)
{
alias MemberType = typeof(Type.tupleof[memberIndex]); alias MemberType = typeof(Type.tupleof[memberIndex]);
enum isOptional = hasUDA!(Type.tupleof[memberIndex], OptionalDependency); enum isOptional = hasUDA!(Type.tupleof[memberIndex], OptionalDependency);
static if (isDynamicArray!MemberType) { static if (isDynamicArray!MemberType)
injectMultipleInstances!(member, memberIndex, isOptional, MemberType)(container, instance); {
} else { injectMultipleInstances!(member, memberIndex, isOptional, MemberType)(container,
injectSingleInstance!(member, memberIndex, isOptional, MemberType, QualifierType)(container, instance); instance);
}
else
{
injectSingleInstance!(member, memberIndex, isOptional, MemberType, QualifierType)(container,
instance);
} }
} }
} }
private void injectMultipleInstances(string member, size_t memberIndex, bool isOptional, MemberType, Type)(shared(DependencyContainer) container, Type instance) { private void injectMultipleInstances(string member, size_t memberIndex,
bool isOptional, MemberType, Type)(shared(DependencyContainer) container, Type instance)
{
alias MemberElementType = ElementType!MemberType; alias MemberElementType = ElementType!MemberType;
static if (isOptional) { static if (isOptional)
{
auto instances = container.resolveAll!MemberElementType(ResolveOption.noResolveException); auto instances = container.resolveAll!MemberElementType(ResolveOption.noResolveException);
} else { }
else
{
auto instances = container.resolveAll!MemberElementType; auto instances = container.resolveAll!MemberElementType;
} }
instance.tupleof[memberIndex] = instances; instance.tupleof[memberIndex] = instances;
debug(poodinisVerbose) { debug (poodinisVerbose)
{
printDebugAutowiringArray(typeid(MemberElementType), typeid(Type), &instance, member); printDebugAutowiringArray(typeid(MemberElementType), typeid(Type), &instance, member);
} }
} }
private void injectSingleInstance(string member, size_t memberIndex, bool isOptional, MemberType, QualifierType, Type)(shared(DependencyContainer) container, Type instance) { private void injectSingleInstance(string member, size_t memberIndex,
debug(poodinisVerbose) { bool isOptional, MemberType, QualifierType, Type)(
shared(DependencyContainer) container, Type instance)
{
debug (poodinisVerbose)
{
TypeInfo qualifiedInstanceType = typeid(MemberType); TypeInfo qualifiedInstanceType = typeid(MemberType);
} }
enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance); enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance);
MemberType qualifiedInstance; MemberType qualifiedInstance;
static if (!is(QualifierType == UseMemberType)) { static if (!is(QualifierType == UseMemberType))
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance, isOptional)(container); {
debug(poodinisVerbose) { qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType,
assignNewInstance, isOptional)(container);
debug (poodinisVerbose)
{
qualifiedInstanceType = typeid(QualifierType); qualifiedInstanceType = typeid(QualifierType);
} }
} else { }
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance, isOptional)(container); else
{
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType,
assignNewInstance, isOptional)(container);
} }
instance.tupleof[memberIndex] = qualifiedInstance; instance.tupleof[memberIndex] = qualifiedInstance;
debug(poodinisVerbose) { debug (poodinisVerbose)
printDebugAutowiringCandidate(qualifiedInstanceType, &qualifiedInstance, typeid(Type), &instance, member); {
printDebugAutowiringCandidate(qualifiedInstanceType,
&qualifiedInstance, typeid(Type), &instance, member);
} }
} }
private QualifierType createOrResolveInstance(MemberType, QualifierType, bool createNew, bool isOptional)(shared(DependencyContainer) container) { private QualifierType createOrResolveInstance(MemberType, QualifierType,
static if (createNew) { bool createNew, bool isOptional)(shared(DependencyContainer) container)
{
static if (createNew)
{
auto instanceFactory = new InstanceFactory(); auto instanceFactory = new InstanceFactory();
instanceFactory.factoryParameters = InstanceFactoryParameters(typeid(MemberType), CreatesSingleton.no); instanceFactory.factoryParameters = InstanceFactoryParameters(typeid(MemberType),
CreatesSingleton.no);
return cast(MemberType) instanceFactory.getInstance(); return cast(MemberType) instanceFactory.getInstance();
} else { }
static if (isOptional) { else
{
static if (isOptional)
{
return container.resolve!(MemberType, QualifierType)(ResolveOption.noResolveException); return container.resolve!(MemberType, QualifierType)(ResolveOption.noResolveException);
} else { }
else
{
return container.resolve!(MemberType, QualifierType); return container.resolve!(MemberType, QualifierType);
} }
} }
} }
private void injectValue(string member, size_t memberIndex, string key, bool mandatory, Type)(shared(DependencyContainer) container, Type instance) { private void injectValue(string member, size_t memberIndex, string key, bool mandatory, Type)(
shared(DependencyContainer) container, Type instance)
{
alias MemberType = typeof(Type.tupleof[memberIndex]); alias MemberType = typeof(Type.tupleof[memberIndex]);
try { try
{
auto injector = container.resolve!(ValueInjector!MemberType); auto injector = container.resolve!(ValueInjector!MemberType);
instance.tupleof[memberIndex] = injector.get(key); instance.tupleof[memberIndex] = injector.get(key);
debug(poodinisVerbose) { debug (poodinisVerbose)
{
printDebugValueInjection(typeid(Type), &instance, member, typeid(MemberType), key); printDebugValueInjection(typeid(Type), &instance, member, typeid(MemberType), key);
} }
} catch (ResolveException e) { }
throw new ValueInjectionException(format("Could not inject value of type %s into %s.%s: value injector is missing for this type.", typeid(MemberType), typeid(Type), member)); catch (ResolveException e)
} catch (ValueNotAvailableException e) { {
static if (mandatory) { throw new ValueInjectionException(format(
throw new ValueInjectionException(format("Could not inject value of type %s into %s.%s", typeid(MemberType), typeid(Type), member), e); "Could not inject value of type %s into %s.%s: value injector is missing for this type.",
typeid(MemberType), typeid(Type), member));
}
catch (ValueNotAvailableException e)
{
static if (mandatory)
{
throw new ValueInjectionException(format("Could not inject value of type %s into %s.%s",
typeid(MemberType), typeid(Type), member), e);
} }
} }
} }
private void printDebugValueInjection(TypeInfo instanceType, void* instanceAddress, string member, TypeInfo valueType, string key) { private void printDebugValueInjection(TypeInfo instanceType,
debug { void* instanceAddress, string member, TypeInfo valueType, string key)
writeln(format("DEBUG: Injected value with key '%s' of type %s into [%s@%s].%s", key, valueType, instanceType, instanceAddress, member)); {
debug
{
writeln(format("DEBUG: Injected value with key '%s' of type %s into [%s@%s].%s",
key, valueType, instanceType, instanceAddress, member));
} }
} }
@ -247,25 +330,33 @@ private void printDebugValueInjection(TypeInfo instanceType, void* instanceAddre
* See_Also: DependencyContainer * See_Also: DependencyContainer
* Deprecated: Using the global container is undesired. See DependencyContainer.getInstance(). * Deprecated: Using the global container is undesired. See DependencyContainer.getInstance().
*/ */
public deprecated void globalAutowire(Type)(Type instance) { public deprecated void globalAutowire(Type)(Type instance)
{
DependencyContainer.getInstance().autowire(instance); DependencyContainer.getInstance().autowire(instance);
} }
class AutowiredRegistration(RegistrationType : Object) : Registration { class AutowiredRegistration(RegistrationType : Object) : Registration
{
private shared(DependencyContainer) container; private shared(DependencyContainer) container;
public this(TypeInfo registeredType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) { public this(TypeInfo registeredType, InstanceFactory instanceFactory,
shared(DependencyContainer) originatingContainer)
{
super(registeredType, typeid(RegistrationType), instanceFactory, originatingContainer); super(registeredType, typeid(RegistrationType), instanceFactory, originatingContainer);
} }
public override Object getInstance(InstantiationContext context = new AutowireInstantiationContext()) { public override Object getInstance(
enforce(!(originatingContainer is null), "The registration's originating container is null. There is no way to resolve autowire dependencies."); InstantiationContext context = new AutowireInstantiationContext())
{
enforce(!(originatingContainer is null),
"The registration's originating container is null. There is no way to resolve autowire dependencies.");
RegistrationType instance = cast(RegistrationType) super.getInstance(context); RegistrationType instance = cast(RegistrationType) super.getInstance(context);
AutowireInstantiationContext autowireContext = cast(AutowireInstantiationContext) context; AutowireInstantiationContext autowireContext = cast(AutowireInstantiationContext) context;
enforce(!(autowireContext is null), "Given instantiation context type could not be cast to an AutowireInstantiationContext. If you relied on using the default assigned context: make sure you're calling getInstance() on an instance of type AutowiredRegistration!"); enforce(!(autowireContext is null), "Given instantiation context type could not be cast to an AutowireInstantiationContext. If you relied on using the default assigned context: make sure you're calling getInstance() on an instance of type AutowiredRegistration!");
if (autowireContext.autowireInstance) { if (autowireContext.autowireInstance)
{
originatingContainer.autowire(instance); originatingContainer.autowire(instance);
} }
@ -274,15 +365,18 @@ class AutowiredRegistration(RegistrationType : Object) : Registration {
return instance; return instance;
} }
private void delegate() getPreDestructor(RegistrationType instance) { private void delegate() getPreDestructor(RegistrationType instance)
{
void delegate() preDestructor = null; void delegate() preDestructor = null;
foreach (memberName; __traits(allMembers, RegistrationType)) { foreach (memberName; __traits(allMembers, RegistrationType))
{
mixin(createImportsString!RegistrationType); mixin(createImportsString!RegistrationType);
enum QualifiedName = fullyQualifiedName!RegistrationType ~ `.` ~ memberName; enum QualifiedName = fullyQualifiedName!RegistrationType ~ `.` ~ memberName;
static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName))) static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName)))
&& __traits(getProtection, __traits(getMember, instance, memberName)) == "public" && __traits(getProtection, __traits(getMember, instance, memberName)) == "public"
&& isFunction!(mixin(QualifiedName)) && isFunction!(mixin(QualifiedName))
&& hasUDA!(__traits(getMember, instance, memberName), PreDestroy)) { && hasUDA!(__traits(getMember, instance, memberName), PreDestroy))
{
preDestructor = &__traits(getMember, instance, memberName); preDestructor = &__traits(getMember, instance, memberName);
} }
} }
@ -291,6 +385,7 @@ class AutowiredRegistration(RegistrationType : Object) : Registration {
} }
} }
class AutowireInstantiationContext : InstantiationContext { class AutowireInstantiationContext : InstantiationContext
{
public bool autowireInstance = true; public bool autowireInstance = true;
} }

View file

@ -21,23 +21,27 @@ import poodinis.altphobos : isFunction;
import poodinis.imports : createImportsString; import poodinis.imports : createImportsString;
import std.string : format; import std.string : format;
import std.algorithm: canFind; import std.algorithm : canFind;
import std.traits : fullyQualifiedName, hasUDA, BaseTypeTuple; import std.traits : fullyQualifiedName, hasUDA, BaseTypeTuple;
import std.meta : AliasSeq; import std.meta : AliasSeq;
debug { debug
{
import std.stdio; import std.stdio;
} }
/** /**
* Exception thrown when errors occur while resolving a type in a dependency container. * Exception thrown when errors occur while resolving a type in a dependency container.
*/ */
class ResolveException : Exception { class ResolveException : Exception
this(string message, TypeInfo resolveType) { {
this(string message, TypeInfo resolveType)
{
super(format("Exception while resolving type %s: %s", resolveType.toString(), message)); super(format("Exception while resolving type %s: %s", resolveType.toString(), message));
} }
this(Throwable cause, TypeInfo resolveType) { this(Throwable cause, TypeInfo resolveType)
{
super(format("Exception while resolving type %s", resolveType.toString()), cause); super(format("Exception while resolving type %s", resolveType.toString()), cause);
} }
} }
@ -45,16 +49,20 @@ class ResolveException : Exception {
/** /**
* Exception thrown when errors occur while registering a type in a dependency container. * Exception thrown when errors occur while registering a type in a dependency container.
*/ */
class RegistrationException : Exception { class RegistrationException : Exception
this(string message, TypeInfo registrationType) { {
super(format("Exception while registering type %s: %s", registrationType.toString(), message)); this(string message, TypeInfo registrationType)
{
super(format("Exception while registering type %s: %s",
registrationType.toString(), message));
} }
} }
/** /**
* Options which influence the process of registering dependencies * Options which influence the process of registering dependencies
*/ */
public enum RegistrationOption { public enum RegistrationOption
{
none = 0, none = 0,
/** /**
* Prevent a concrete type being registered on itself. With this option you will always need * Prevent a concrete type being registered on itself. With this option you will always need
@ -66,7 +74,8 @@ public enum RegistrationOption {
/** /**
* Options which influence the process of resolving dependencies * Options which influence the process of resolving dependencies
*/ */
public enum ResolveOption { public enum ResolveOption
{
none = 0, none = 0,
/** /**
* Registers the type you're trying to resolve before returning it. * Registers the type you're trying to resolve before returning it.
@ -89,7 +98,9 @@ public enum ResolveOption {
* Multiple methods can be marked and will all be called after construction. The order in which * Multiple methods can be marked and will all be called after construction. The order in which
* methods are called is undetermined. Methods should have the signature void(void). * methods are called is undetermined. Methods should have the signature void(void).
*/ */
struct PostConstruct {} struct PostConstruct
{
}
/** /**
* Methods marked with this UDA within dependencies are called before the container * Methods marked with this UDA within dependencies are called before the container
@ -98,7 +109,9 @@ struct PostConstruct {}
* This method is called when removeRegistration or clearAllRegistrations is called. * This method is called when removeRegistration or clearAllRegistrations is called.
* It will also be called when the container's destructor is called. * It will also be called when the container's destructor is called.
*/ */
struct PreDestroy {} struct PreDestroy
{
}
/** /**
* The dependency container maintains all dependencies registered with it. * The dependency container maintains all dependencies registered with it.
@ -110,7 +123,8 @@ struct PreDestroy {}
* In most cases you want to use a global singleton dependency container provided by getInstance() to manage all dependencies. * In most cases you want to use a global singleton dependency container provided by getInstance() to manage all dependencies.
* You can still create new instances of this class for exceptional situations. * You can still create new instances of this class for exceptional situations.
*/ */
synchronized class DependencyContainer { synchronized class DependencyContainer
{
private Registration[][TypeInfo] registrations; private Registration[][TypeInfo] registrations;
private Registration[] autowireStack; private Registration[] autowireStack;
@ -118,7 +132,8 @@ synchronized class DependencyContainer {
private RegistrationOption persistentRegistrationOptions; private RegistrationOption persistentRegistrationOptions;
private ResolveOption persistentResolveOptions; private ResolveOption persistentResolveOptions;
~this() { ~this()
{
clearAllRegistrations(); clearAllRegistrations();
} }
@ -142,7 +157,8 @@ synchronized class DependencyContainer {
* *
* See_Also: singleInstance, newInstance, existingInstance * See_Also: singleInstance, newInstance, existingInstance
*/ */
public Registration register(ConcreteType)(RegistrationOption options = RegistrationOption.none) { public Registration register(ConcreteType)(RegistrationOption options = RegistrationOption.none)
{
return register!(ConcreteType, ConcreteType)(options); return register!(ConcreteType, ConcreteType)(options);
} }
@ -163,27 +179,36 @@ synchronized class DependencyContainer {
* *
* See_Also: singleInstance, newInstance, existingInstance, RegistrationOption * See_Also: singleInstance, newInstance, existingInstance, RegistrationOption
*/ */
public Registration register(SuperType, ConcreteType : SuperType)( public Registration register(SuperType, ConcreteType:
RegistrationOption options = RegistrationOption.none) if (!is(ConcreteType == struct)) { SuperType)(RegistrationOption options = RegistrationOption.none)
if (!is(ConcreteType == struct))
{
TypeInfo registeredType = typeid(SuperType); TypeInfo registeredType = typeid(SuperType);
TypeInfo_Class concreteType = typeid(ConcreteType); TypeInfo_Class concreteType = typeid(ConcreteType);
debug(poodinisVerbose) { debug (poodinisVerbose)
writeln(format("DEBUG: Register type %s (as %s)", concreteType.toString(), registeredType.toString())); {
writeln(format("DEBUG: Register type %s (as %s)",
concreteType.toString(), registeredType.toString()));
} }
auto existingRegistration = getExistingRegistration(registeredType, concreteType); auto existingRegistration = getExistingRegistration(registeredType, concreteType);
if (existingRegistration) { if (existingRegistration)
{
return existingRegistration; return existingRegistration;
} }
auto instanceFactory = new ConstructorInjectingInstanceFactory!ConcreteType(this); auto instanceFactory = new ConstructorInjectingInstanceFactory!ConcreteType(this);
auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, instanceFactory, this); auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType,
instanceFactory, this);
newRegistration.singleInstance(); newRegistration.singleInstance();
static if (!is(SuperType == ConcreteType)) { static if (!is(SuperType == ConcreteType))
if (!hasOption(options, persistentRegistrationOptions, RegistrationOption.doNotAddConcreteTypeRegistration)) { {
if (!hasOption(options, persistentRegistrationOptions,
RegistrationOption.doNotAddConcreteTypeRegistration))
{
auto concreteTypeRegistration = register!ConcreteType; auto concreteTypeRegistration = register!ConcreteType;
concreteTypeRegistration.linkTo(newRegistration); concreteTypeRegistration.linkTo(newRegistration);
} }
@ -193,36 +218,49 @@ synchronized class DependencyContainer {
return newRegistration; return newRegistration;
} }
public Registration register(SuperType, ConcreteType)(RegistrationOption options = RegistrationOption.none) public Registration register(SuperType, ConcreteType)(
if (!is(SuperType == ConcreteType) && !is(BaseTypeTuple!ConcreteType == AliasSeq!(Object, SuperType)) && !is(BaseTypeTuple!ConcreteType == AliasSeq!(SuperType))) { RegistrationOption options = RegistrationOption.none)
pragma(msg, "Cannot register dependency: ", ConcreteType, " is not derived from ", SuperType); if (!is(SuperType == ConcreteType) && !is(BaseTypeTuple!ConcreteType == AliasSeq!(Object,
SuperType)) && !is(BaseTypeTuple!ConcreteType == AliasSeq!(SuperType)))
{
pragma(msg, "Cannot register dependency: ", ConcreteType,
" is not derived from ", SuperType);
static assert(0, "Cannot register dependency"); static assert(0, "Cannot register dependency");
} }
private bool hasOption(OptionType)(OptionType options, OptionType persistentOptions, OptionType option) { private bool hasOption(OptionType)(OptionType options,
OptionType persistentOptions, OptionType option)
{
return ((options | persistentOptions) & option) != 0; return ((options | persistentOptions) & option) != 0;
} }
private OptionType buildFlags(OptionType)(OptionType[] options) { private OptionType buildFlags(OptionType)(OptionType[] options)
{
OptionType flags; OptionType flags;
foreach (option; options) { foreach (option; options)
{
flags |= option; flags |= option;
} }
return flags; return flags;
} }
private Registration getExistingRegistration(TypeInfo registrationType, TypeInfo qualifierType) { private Registration getExistingRegistration(TypeInfo registrationType, TypeInfo qualifierType)
{
auto existingCandidates = registrationType in registrations; auto existingCandidates = registrationType in registrations;
if (existingCandidates) { if (existingCandidates)
return getRegistration(cast(Registration[]) *existingCandidates, qualifierType); {
return getRegistration(cast(Registration[])*existingCandidates, qualifierType);
} }
return null; return null;
} }
private Registration getRegistration(Registration[] candidates, TypeInfo concreteType) { private Registration getRegistration(Registration[] candidates, TypeInfo concreteType)
foreach(existingRegistration ; candidates) { {
if (existingRegistration.instanceType == concreteType) { foreach (existingRegistration; candidates)
{
if (existingRegistration.instanceType == concreteType)
{
return existingRegistration; return existingRegistration;
} }
} }
@ -269,7 +307,10 @@ synchronized class DependencyContainer {
* --- * ---
* You need to use the resolve method which allows you to specify a qualifier. * You need to use the resolve method which allows you to specify a qualifier.
*/ */
public RegistrationType resolve(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) if (!is(RegistrationType == struct)) { public RegistrationType resolve(RegistrationType)(
ResolveOption resolveOptions = ResolveOption.none)
if (!is(RegistrationType == struct))
{
return resolve!(RegistrationType, RegistrationType)(resolveOptions); return resolve!(RegistrationType, RegistrationType)(resolveOptions);
} }
@ -299,53 +340,73 @@ synchronized class DependencyContainer {
* container.resolve!(Animal, Dog); * container.resolve!(Animal, Dog);
* --- * ---
*/ */
public QualifierType resolve(RegistrationType, QualifierType : RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) if (!is(QualifierType == struct)) { public QualifierType resolve(RegistrationType, QualifierType:
RegistrationType)(ResolveOption resolveOptions = ResolveOption.none)
if (!is(QualifierType == struct))
{
TypeInfo resolveType = typeid(RegistrationType); TypeInfo resolveType = typeid(RegistrationType);
TypeInfo qualifierType = typeid(QualifierType); TypeInfo qualifierType = typeid(QualifierType);
debug(poodinisVerbose) { debug (poodinisVerbose)
{
writeln("DEBUG: Resolving type " ~ resolveType.toString() ~ " with qualifier " ~ qualifierType.toString()); writeln("DEBUG: Resolving type " ~ resolveType.toString() ~ " with qualifier " ~ qualifierType.toString());
} }
static if (__traits(compiles, new QualifierType())) { static if (__traits(compiles, new QualifierType()))
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.registerBeforeResolving)) { {
if (hasOption(resolveOptions, persistentResolveOptions,
ResolveOption.registerBeforeResolving))
{
register!(RegistrationType, QualifierType)(); register!(RegistrationType, QualifierType)();
} }
} }
auto candidates = resolveType in registrations; auto candidates = resolveType in registrations;
if (!candidates) { if (!candidates)
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) { {
if (hasOption(resolveOptions, persistentResolveOptions,
ResolveOption.noResolveException))
{
return null; return null;
} }
throw new ResolveException("Type not registered.", resolveType); throw new ResolveException("Type not registered.", resolveType);
} }
Registration registration = getQualifiedRegistration(resolveType, qualifierType, cast(Registration[]) *candidates); Registration registration = getQualifiedRegistration(resolveType,
qualifierType, cast(Registration[])*candidates);
try { try
{
QualifierType newInstance = resolveAutowiredInstance!QualifierType(registration); QualifierType newInstance = resolveAutowiredInstance!QualifierType(registration);
callPostConstructors(newInstance); callPostConstructors(newInstance);
return newInstance; return newInstance;
} catch (ValueInjectionException e) { }
catch (ValueInjectionException e)
{
throw new ResolveException(e, resolveType); throw new ResolveException(e, resolveType);
} }
} }
bool isRegistered(RegistrationType)() { bool isRegistered(RegistrationType)()
{
TypeInfo typeInfo = typeid(RegistrationType); TypeInfo typeInfo = typeid(RegistrationType);
auto candidates = typeInfo in registrations; auto candidates = typeInfo in registrations;
return candidates !is null; return candidates !is null;
} }
private QualifierType resolveAutowiredInstance(QualifierType)(Registration registration) { private QualifierType resolveAutowiredInstance(QualifierType)(Registration registration)
{
QualifierType instance; QualifierType instance;
if (!(cast(Registration[]) autowireStack).canFind(registration)) { if (!(cast(Registration[]) autowireStack).canFind(registration))
{
autowireStack ~= cast(shared(Registration)) registration; autowireStack ~= cast(shared(Registration)) registration;
instance = cast(QualifierType) registration.getInstance(new AutowireInstantiationContext()); instance = cast(QualifierType) registration.getInstance(
autowireStack = autowireStack[0 .. $-1]; new AutowireInstantiationContext());
} else { autowireStack = autowireStack[0 .. $ - 1];
}
else
{
auto autowireContext = new AutowireInstantiationContext(); auto autowireContext = new AutowireInstantiationContext();
autowireContext.autowireInstance = false; autowireContext.autowireInstance = false;
instance = cast(QualifierType) registration.getInstance(autowireContext); instance = cast(QualifierType) registration.getInstance(autowireContext);
@ -370,31 +431,43 @@ synchronized class DependencyContainer {
* Animal[] animals = container.resolveAll!Animal; * Animal[] animals = container.resolveAll!Animal;
* --- * ---
*/ */
public RegistrationType[] resolveAll(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) { public RegistrationType[] resolveAll(RegistrationType)(
ResolveOption resolveOptions = ResolveOption.none)
{
RegistrationType[] instances; RegistrationType[] instances;
TypeInfo resolveType = typeid(RegistrationType); TypeInfo resolveType = typeid(RegistrationType);
auto qualifiedRegistrations = resolveType in registrations; auto qualifiedRegistrations = resolveType in registrations;
if (!qualifiedRegistrations) { if (!qualifiedRegistrations)
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) { {
if (hasOption(resolveOptions, persistentResolveOptions,
ResolveOption.noResolveException))
{
return []; return [];
} }
throw new ResolveException("Type not registered.", resolveType); throw new ResolveException("Type not registered.", resolveType);
} }
foreach(registration ; cast(Registration[]) *qualifiedRegistrations) { foreach (registration; cast(Registration[])*qualifiedRegistrations)
{
instances ~= resolveAutowiredInstance!RegistrationType(registration); instances ~= resolveAutowiredInstance!RegistrationType(registration);
} }
return instances; return instances;
} }
private Registration getQualifiedRegistration(TypeInfo resolveType, TypeInfo qualifierType, Registration[] candidates) { private Registration getQualifiedRegistration(TypeInfo resolveType,
if (resolveType == qualifierType) { TypeInfo qualifierType, Registration[] candidates)
if (candidates.length > 1) { {
if (resolveType == qualifierType)
{
if (candidates.length > 1)
{
string candidateList = candidates.toConcreteTypeListString(); string candidateList = candidates.toConcreteTypeListString();
throw new ResolveException("Multiple qualified candidates available: " ~ candidateList ~ ". Please use a qualifier.", resolveType); throw new ResolveException(
"Multiple qualified candidates available: " ~ candidateList ~ ". Please use a qualifier.",
resolveType);
} }
return candidates[0]; return candidates[0];
@ -403,14 +476,17 @@ synchronized class DependencyContainer {
return getRegistration(candidates, qualifierType); return getRegistration(candidates, qualifierType);
} }
private void callPostConstructors(Type)(Type instance) { private void callPostConstructors(Type)(Type instance)
foreach (memberName; __traits(allMembers, Type)) { {
foreach (memberName; __traits(allMembers, Type))
{
mixin(createImportsString!Type); mixin(createImportsString!Type);
enum QualifiedName = fullyQualifiedName!Type ~ `.` ~ memberName; enum QualifiedName = fullyQualifiedName!Type ~ `.` ~ memberName;
static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName))) static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName)))
&& __traits(getProtection, __traits(getMember, instance, memberName)) == "public" && __traits(getProtection, __traits(getMember, instance, memberName)) == "public"
&& isFunction!(mixin(QualifiedName)) && isFunction!(mixin(QualifiedName))
&& hasUDA!(__traits(getMember, instance, memberName), PostConstruct)) { && hasUDA!(__traits(getMember, instance, memberName), PostConstruct))
{
__traits(getMember, instance, memberName)(); __traits(getMember, instance, memberName)();
} }
} }
@ -419,8 +495,10 @@ synchronized class DependencyContainer {
/** /**
* Clears all dependency registrations managed by this container. * Clears all dependency registrations managed by this container.
*/ */
public void clearAllRegistrations() { public void clearAllRegistrations()
foreach(registrationsOfType; registrations) { {
foreach (registrationsOfType; registrations)
{
callPreDestructorsOfRegistrations(registrationsOfType); callPreDestructorsOfRegistrations(registrationsOfType);
} }
registrations.destroy(); registrations.destroy();
@ -436,16 +514,20 @@ synchronized class DependencyContainer {
* container.removeRegistration!Animal; * container.removeRegistration!Animal;
* --- * ---
*/ */
public void removeRegistration(RegistrationType)() { public void removeRegistration(RegistrationType)()
{
auto registrationsOfType = *(typeid(RegistrationType) in registrations); auto registrationsOfType = *(typeid(RegistrationType) in registrations);
callPreDestructorsOfRegistrations(registrationsOfType); callPreDestructorsOfRegistrations(registrationsOfType);
registrations.remove(typeid(RegistrationType)); registrations.remove(typeid(RegistrationType));
} }
private void callPreDestructorsOfRegistrations(shared(Registration[]) registrations) { private void callPreDestructorsOfRegistrations(shared(Registration[]) registrations)
foreach(registration; registrations) { {
foreach (registration; registrations)
{
Registration unsharedRegistration = cast(Registration) registration; Registration unsharedRegistration = cast(Registration) registration;
if (unsharedRegistration.preDestructor !is null) { if (unsharedRegistration.preDestructor !is null)
{
unsharedRegistration.preDestructor()(); unsharedRegistration.preDestructor()();
} }
} }
@ -454,28 +536,32 @@ synchronized class DependencyContainer {
/** /**
* Apply persistent registration options which will be used everytime register() is called. * Apply persistent registration options which will be used everytime register() is called.
*/ */
public void setPersistentRegistrationOptions(RegistrationOption options) { public void setPersistentRegistrationOptions(RegistrationOption options)
{
persistentRegistrationOptions = options; persistentRegistrationOptions = options;
} }
/** /**
* Unsets all applied persistent registration options * Unsets all applied persistent registration options
*/ */
public void unsetPersistentRegistrationOptions() { public void unsetPersistentRegistrationOptions()
{
persistentRegistrationOptions = RegistrationOption.none; persistentRegistrationOptions = RegistrationOption.none;
} }
/** /**
* Apply persistent resolve options which will be used everytime resolve() is called. * Apply persistent resolve options which will be used everytime resolve() is called.
*/ */
public void setPersistentResolveOptions(ResolveOption options) { public void setPersistentResolveOptions(ResolveOption options)
{
persistentResolveOptions = options; persistentResolveOptions = options;
} }
/** /**
* Unsets all applied persistent resolve options * Unsets all applied persistent resolve options
*/ */
public void unsetPersistentResolveOptions() { public void unsetPersistentResolveOptions()
{
persistentResolveOptions = ResolveOption.none; persistentResolveOptions = ResolveOption.none;
} }

View file

@ -20,21 +20,27 @@ import poodinis.autowire : autowire;
import std.traits : hasUDA, ReturnType; import std.traits : hasUDA, ReturnType;
class ApplicationContext { class ApplicationContext
public void registerDependencies(shared(DependencyContainer) container) {} {
public void registerDependencies(shared(DependencyContainer) container)
{
}
} }
/** /**
* A component annotation is used for specifying which factory methods produce components in * A component annotation is used for specifying which factory methods produce components in
* an application context. * an application context.
*/ */
struct Component {} struct Component
{
}
/** /**
* This annotation allows you to specify by which super type the component should be registered. This * This annotation allows you to specify by which super type the component should be registered. This
* enables you to use type-qualified alternatives for dependencies. * enables you to use type-qualified alternatives for dependencies.
*/ */
struct RegisterByType(Type) { struct RegisterByType(Type)
{
Type type; Type type;
} }
@ -42,7 +48,9 @@ struct RegisterByType(Type) {
* Components with the prototype registration will be scoped as dependencies which will create * Components with the prototype registration will be scoped as dependencies which will create
* new instances every time they are resolved. The factory method will be called repeatedly. * new instances every time they are resolved. The factory method will be called repeatedly.
*/ */
struct Prototype {} struct Prototype
{
}
/** /**
* Register dependencies through an application context. * Register dependencies through an application context.
@ -51,7 +59,8 @@ struct Prototype {}
* It is mostly used for dependencies which come from an external library or when you don't * It is mostly used for dependencies which come from an external library or when you don't
* want to use annotations to set-up dependencies in your classes. * want to use annotations to set-up dependencies in your classes.
*/ */
public void registerContext(Context : ApplicationContext)(shared(DependencyContainer) container) { public void registerContext(Context : ApplicationContext)(shared(DependencyContainer) container)
{
auto context = new Context(); auto context = new Context();
context.registerDependencies(container); context.registerDependencies(container);
context.registerContextComponents(container); context.registerContextComponents(container);
@ -59,26 +68,38 @@ public void registerContext(Context : ApplicationContext)(shared(DependencyConta
autowire(container, context); autowire(container, context);
} }
public void registerContextComponents(ApplicationContextType : ApplicationContext)(ApplicationContextType context, shared(DependencyContainer) container) { public void registerContextComponents(ApplicationContextType : ApplicationContext)(
foreach (member ; __traits(allMembers, ApplicationContextType)) { ApplicationContextType context, shared(DependencyContainer) container)
static if (__traits(getProtection, __traits(getMember, context, member)) == "public" && hasUDA!(__traits(getMember, context, member), Component)) { {
foreach (member; __traits(allMembers, ApplicationContextType))
{
static if (__traits(getProtection, __traits(getMember, context,
member)) == "public" && hasUDA!(__traits(getMember, context, member), Component))
{
auto factoryMethod = &__traits(getMember, context, member); auto factoryMethod = &__traits(getMember, context, member);
Registration registration = null; Registration registration = null;
auto createsSingleton = CreatesSingleton.yes; auto createsSingleton = CreatesSingleton.yes;
foreach(attribute; __traits(getAttributes, __traits(getMember, context, member))) { foreach (attribute; __traits(getAttributes, __traits(getMember, context, member)))
static if (is(attribute == RegisterByType!T, T)) { {
registration = container.register!(typeof(attribute.type), ReturnType!factoryMethod); static if (is(attribute == RegisterByType!T, T))
} else static if (__traits(isSame, attribute, Prototype)) { {
registration = container.register!(typeof(attribute.type),
ReturnType!factoryMethod);
}
else static if (__traits(isSame, attribute, Prototype))
{
createsSingleton = CreatesSingleton.no; createsSingleton = CreatesSingleton.no;
} }
} }
if (registration is null) { if (registration is null)
{
registration = container.register!(ReturnType!factoryMethod); registration = container.register!(ReturnType!factoryMethod);
} }
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, createsSingleton, null, factoryMethod); registration.instanceFactory.factoryParameters = InstanceFactoryParameters(
registration.instanceType, createsSingleton, null, factoryMethod);
} }
} }
} }

View file

@ -19,7 +19,8 @@ import std.exception : enforce;
import std.traits : Parameters, isBuiltinType, fullyQualifiedName; import std.traits : Parameters, isBuiltinType, fullyQualifiedName;
import std.string : format; import std.string : format;
debug { debug
{
import std.stdio : writeln; import std.stdio : writeln;
} }
@ -27,34 +28,42 @@ alias CreatesSingleton = Flag!"CreatesSingleton";
alias InstanceFactoryMethod = Object delegate(); alias InstanceFactoryMethod = Object delegate();
alias InstanceEventHandler = void delegate(Object instance); alias InstanceEventHandler = void delegate(Object instance);
class InstanceCreationException : Exception { class InstanceCreationException : Exception
this(string message, string file = __FILE__, size_t line = __LINE__) { {
this(string message, string file = __FILE__, size_t line = __LINE__)
{
super(message, file, line); super(message, file, line);
} }
} }
struct InstanceFactoryParameters { struct InstanceFactoryParameters
{
TypeInfo_Class instanceType; TypeInfo_Class instanceType;
CreatesSingleton createsSingleton = CreatesSingleton.yes; CreatesSingleton createsSingleton = CreatesSingleton.yes;
Object existingInstance; Object existingInstance;
InstanceFactoryMethod factoryMethod; InstanceFactoryMethod factoryMethod;
} }
class InstanceFactory { class InstanceFactory
{
private Object instance = null; private Object instance = null;
private InstanceFactoryParameters _factoryParameters; private InstanceFactoryParameters _factoryParameters;
private InstanceEventHandler _constructionHandler; private InstanceEventHandler _constructionHandler;
this() { this()
{
factoryParameters = InstanceFactoryParameters(); factoryParameters = InstanceFactoryParameters();
} }
public @property void factoryParameters(InstanceFactoryParameters factoryParameters) { public @property void factoryParameters(InstanceFactoryParameters factoryParameters)
if (factoryParameters.factoryMethod is null) { {
if (factoryParameters.factoryMethod is null)
{
factoryParameters.factoryMethod = &this.createInstance; factoryParameters.factoryMethod = &this.createInstance;
} }
if (factoryParameters.existingInstance !is null) { if (factoryParameters.existingInstance !is null)
{
factoryParameters.createsSingleton = CreatesSingleton.yes; factoryParameters.createsSingleton = CreatesSingleton.yes;
this.instance = factoryParameters.existingInstance; this.instance = factoryParameters.existingInstance;
} }
@ -62,73 +71,99 @@ class InstanceFactory {
_factoryParameters = factoryParameters; _factoryParameters = factoryParameters;
} }
public @property InstanceFactoryParameters factoryParameters() { public @property InstanceFactoryParameters factoryParameters()
{
return _factoryParameters; return _factoryParameters;
} }
public Object getInstance() { public Object getInstance()
if (_factoryParameters.createsSingleton && instance !is null) { {
debug(poodinisVerbose) { if (_factoryParameters.createsSingleton && instance !is null)
{
debug (poodinisVerbose)
{
printDebugUseExistingInstance(); printDebugUseExistingInstance();
} }
return instance; return instance;
} }
debug(poodinisVerbose) { debug (poodinisVerbose)
{
printDebugCreateNewInstance(); printDebugCreateNewInstance();
} }
instance = _factoryParameters.factoryMethod(); instance = _factoryParameters.factoryMethod();
if(_constructionHandler !is null) { if (_constructionHandler !is null)
{
_constructionHandler(instance); _constructionHandler(instance);
} }
return instance; return instance;
} }
void onConstructed(InstanceEventHandler handler) { void onConstructed(InstanceEventHandler handler)
{
_constructionHandler = handler; _constructionHandler = handler;
} }
private void printDebugUseExistingInstance() { private void printDebugUseExistingInstance()
debug { {
if (_factoryParameters.instanceType !is null) { debug
writeln(format("DEBUG: Existing instance returned of type %s", _factoryParameters.instanceType.toString())); {
} else { if (_factoryParameters.instanceType !is null)
{
writeln(format("DEBUG: Existing instance returned of type %s",
_factoryParameters.instanceType.toString()));
}
else
{
writeln("DEBUG: Existing instance returned from custom factory method"); writeln("DEBUG: Existing instance returned from custom factory method");
} }
} }
} }
private void printDebugCreateNewInstance() { private void printDebugCreateNewInstance()
debug { {
if (_factoryParameters.instanceType !is null) { debug
writeln(format("DEBUG: Creating new instance of type %s", _factoryParameters.instanceType.toString())); {
} else { if (_factoryParameters.instanceType !is null)
{
writeln(format("DEBUG: Creating new instance of type %s",
_factoryParameters.instanceType.toString()));
}
else
{
writeln("DEBUG: Creating new instance from custom factory method"); writeln("DEBUG: Creating new instance from custom factory method");
} }
} }
} }
protected Object createInstance() { protected Object createInstance()
enforce!InstanceCreationException(_factoryParameters.instanceType, "Instance type is not defined, cannot create instance without knowing its type."); {
enforce!InstanceCreationException(_factoryParameters.instanceType,
"Instance type is not defined, cannot create instance without knowing its type.");
return _factoryParameters.instanceType.create(); return _factoryParameters.instanceType.create();
} }
} }
class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory { class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory
{
private shared DependencyContainer container; private shared DependencyContainer container;
private bool isBeingInjected = false; private bool isBeingInjected = false;
this(shared DependencyContainer container) { this(shared DependencyContainer container)
{
this.container = container; this.container = container;
} }
private static string createArgumentList(Params...)() { private static string createArgumentList(Params...)()
{
string argumentList = ""; string argumentList = "";
foreach(param; Params) { foreach (param; Params)
if (argumentList.length > 0) { {
if (argumentList.length > 0)
{
argumentList ~= ","; argumentList ~= ",";
} }
@ -137,18 +172,23 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory {
return argumentList; return argumentList;
} }
private static string createImportList(Params...)() { private static string createImportList(Params...)()
{
string importList = ""; string importList = "";
foreach(param; Params) { foreach (param; Params)
{
importList ~= createImportsString!param; importList ~= createImportsString!param;
} }
return importList; return importList;
} }
private static bool parametersAreValid(Params...)() { private static bool parametersAreValid(Params...)()
{
bool isValid = true; bool isValid = true;
foreach(param; Params) { foreach (param; Params)
if (isBuiltinType!param || is(param == struct)) { {
if (isBuiltinType!param || is(param == struct))
{
isValid = false; isValid = false;
break; break;
} }
@ -157,18 +197,26 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory {
return isValid; return isValid;
} }
protected override Object createInstance() { protected override Object createInstance()
enforce!InstanceCreationException(container, "A dependency container is not defined. Cannot perform constructor injection without one."); {
enforce!InstanceCreationException(!isBeingInjected, format("%s is already being created and injected; possible circular dependencies in constructors?", InstanceType.stringof)); enforce!InstanceCreationException(container,
"A dependency container is not defined. Cannot perform constructor injection without one.");
enforce!InstanceCreationException(!isBeingInjected,
format("%s is already being created and injected; possible circular dependencies in constructors?",
InstanceType.stringof));
Object instance = null; Object instance = null;
static if (__traits(compiles, __traits(getOverloads, InstanceType, `__ctor`))) { static if (__traits(compiles, __traits(getOverloads, InstanceType, `__ctor`)))
foreach(ctor ; __traits(getOverloads, InstanceType, `__ctor`)) { {
static if (parametersAreValid!(Parameters!ctor)) { foreach (ctor; __traits(getOverloads, InstanceType, `__ctor`))
{
static if (parametersAreValid!(Parameters!ctor))
{
isBeingInjected = true; isBeingInjected = true;
mixin(createImportsString!InstanceType mixin(createImportsString!InstanceType ~ createImportList!(
~ createImportList!(Parameters!ctor) ~ ` Parameters!ctor) ~ `
instance = new ` ~ fullyQualifiedName!InstanceType ~ `(` ~ createArgumentList!(Parameters!ctor) ~ `); instance = new ` ~ fullyQualifiedName!InstanceType ~ `(` ~ createArgumentList!(
Parameters!ctor) ~ `);
`); `);
isBeingInjected = false; isBeingInjected = false;
break; break;
@ -176,11 +224,14 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory {
} }
} }
if (instance is null) { if (instance is null)
{
instance = typeid(InstanceType).create(); instance = typeid(InstanceType).create();
} }
enforce!InstanceCreationException(instance !is null, "Unable to create instance of type" ~ InstanceType.stringof ~ ", does it have injectable constructors?"); enforce!InstanceCreationException(instance !is null,
"Unable to create instance of type" ~ InstanceType.stringof
~ ", does it have injectable constructors?");
return instance; return instance;
} }

View file

@ -14,15 +14,20 @@ module poodinis.imports;
import std.meta : staticIndexOf; import std.meta : staticIndexOf;
import std.traits : moduleName, TemplateArgsOf, isBuiltinType; import std.traits : moduleName, TemplateArgsOf, isBuiltinType;
public static string createImportsString(Type, ParentTypeList...)() { public static string createImportsString(Type, ParentTypeList...)()
{
string imports = `import ` ~ moduleName!Type ~ `;`; string imports = `import ` ~ moduleName!Type ~ `;`;
static if (__traits(compiles, TemplateArgsOf!Type)) { static if (__traits(compiles, TemplateArgsOf!Type))
foreach(TemplateArgType; TemplateArgsOf!Type) { {
static if (!isBuiltinType!TemplateArgType && staticIndexOf!(TemplateArgType, ParentTypeList) == -1) { foreach (TemplateArgType; TemplateArgsOf!Type)
{
static if (!isBuiltinType!TemplateArgType
&& staticIndexOf!(TemplateArgType, ParentTypeList) == -1)
{
imports ~= createImportsString!(TemplateArgType, ParentTypeList, Type); imports ~= createImportsString!(TemplateArgType, ParentTypeList, Type);
} }
} }
} }
return imports; return imports;
} }

View file

@ -27,7 +27,8 @@ module poodinis.polyfill;
import std.exception; import std.exception;
static if (!__traits(compiles, basicExceptionCtors)) { static if (!__traits(compiles, basicExceptionCtors))
{
mixin template basicExceptionCtors() mixin template basicExceptionCtors()
{ {
/++ /++
@ -37,8 +38,7 @@ static if (!__traits(compiles, basicExceptionCtors)) {
line = The line number where the exception occurred. line = The line number where the exception occurred.
next = The previous exception in the chain of exceptions, if any. next = The previous exception in the chain of exceptions, if any.
+/ +/
this(string msg, string file = __FILE__, size_t line = __LINE__, this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @nogc @safe pure nothrow
Throwable next = null) @nogc @safe pure nothrow
{ {
super(msg, file, line, next); super(msg, file, line, next);
} }
@ -50,12 +50,13 @@ static if (!__traits(compiles, basicExceptionCtors)) {
file = The file where the exception occurred. file = The file where the exception occurred.
line = The line number where the exception occurred. line = The line number where the exception occurred.
+/ +/
this(string msg, Throwable next, string file = __FILE__, this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @nogc @safe pure nothrow
size_t line = __LINE__) @nogc @safe pure nothrow
{ {
super(msg, file, line, next); super(msg, file, line, next);
} }
} }
} else { }
else
{
public import std.exception : basicExceptionCtors; public import std.exception : basicExceptionCtors;
} }

View file

@ -14,9 +14,11 @@
module poodinis.registration; module poodinis.registration;
import poodinis.container : DependencyContainer; import poodinis.container : DependencyContainer;
import poodinis.factory : InstanceFactory, InstanceEventHandler, InstanceCreationException, InstanceFactoryParameters, CreatesSingleton; import poodinis.factory : InstanceFactory, InstanceEventHandler,
InstanceCreationException, InstanceFactoryParameters, CreatesSingleton;
class Registration { class Registration
{
private TypeInfo _registeredType = null; private TypeInfo _registeredType = null;
private TypeInfo_Class _instanceType = null; private TypeInfo_Class _instanceType = null;
private Registration linkedRegistration; private Registration linkedRegistration;
@ -24,56 +26,70 @@ class Registration {
private InstanceFactory _instanceFactory; private InstanceFactory _instanceFactory;
private void delegate() _preDestructor; private void delegate() _preDestructor;
public @property registeredType() { public @property registeredType()
{
return _registeredType; return _registeredType;
} }
public @property instanceType() { public @property instanceType()
{
return _instanceType; return _instanceType;
} }
public @property originatingContainer() { public @property originatingContainer()
{
return _originatingContainer; return _originatingContainer;
} }
public @property instanceFactory() { public @property instanceFactory()
{
return _instanceFactory; return _instanceFactory;
} }
public @property preDestructor() { public @property preDestructor()
{
return _preDestructor; return _preDestructor;
} }
protected @property preDestructor(void delegate() preDestructor) { protected @property preDestructor(void delegate() preDestructor)
{
_preDestructor = preDestructor; _preDestructor = preDestructor;
} }
this(TypeInfo registeredType, TypeInfo_Class instanceType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) { this(TypeInfo registeredType, TypeInfo_Class instanceType,
InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer)
{
this._registeredType = registeredType; this._registeredType = registeredType;
this._instanceType = instanceType; this._instanceType = instanceType;
this._originatingContainer = originatingContainer; this._originatingContainer = originatingContainer;
this._instanceFactory = instanceFactory; this._instanceFactory = instanceFactory;
} }
public Object getInstance(InstantiationContext context = new InstantiationContext()) { public Object getInstance(InstantiationContext context = new InstantiationContext())
if (linkedRegistration !is null) { {
if (linkedRegistration !is null)
{
return linkedRegistration.getInstance(context); return linkedRegistration.getInstance(context);
} }
if (instanceFactory is null) { if (instanceFactory is null)
throw new InstanceCreationException("No instance factory defined for registration of type " ~ registeredType.toString()); {
throw new InstanceCreationException(
"No instance factory defined for registration of type " ~ registeredType.toString());
} }
return instanceFactory.getInstance(); return instanceFactory.getInstance();
} }
public Registration linkTo(Registration registration) { public Registration linkTo(Registration registration)
{
this.linkedRegistration = registration; this.linkedRegistration = registration;
return this; return this;
} }
Registration onConstructed(InstanceEventHandler handler) { Registration onConstructed(InstanceEventHandler handler)
if(instanceFactory !is null) {
if (instanceFactory !is null)
instanceFactory.onConstructed(handler); instanceFactory.onConstructed(handler);
return this; return this;
} }
@ -84,53 +100,66 @@ class Registration {
* *
* Effectively makes the given registration a singleton. * Effectively makes the given registration a singleton.
*/ */
public Registration singleInstance(Registration registration) { public Registration singleInstance(Registration registration)
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes); {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(
registration.instanceType, CreatesSingleton.yes);
return registration; return registration;
} }
/** /**
* Scopes registrations to return a new instance every time the given registration is resolved. * Scopes registrations to return a new instance every time the given registration is resolved.
*/ */
public Registration newInstance(Registration registration) { public Registration newInstance(Registration registration)
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.no); {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(
registration.instanceType, CreatesSingleton.no);
return registration; return registration;
} }
/** /**
* Scopes registrations to return the given instance every time the given registration is resolved. * Scopes registrations to return the given instance every time the given registration is resolved.
*/ */
public Registration existingInstance(Registration registration, Object instance) { public Registration existingInstance(Registration registration, Object instance)
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes, instance); {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(
registration.instanceType, CreatesSingleton.yes, instance);
return registration; return registration;
} }
/** /**
* Scopes registrations to create new instances using the given initializer delegate. * Scopes registrations to create new instances using the given initializer delegate.
*/ */
public Registration initializedBy(T : Object)(Registration registration, T delegate() initializer) { public Registration initializedBy(T : Object)(Registration registration, T delegate() initializer)
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.no, null, { {
return cast(Object) initializer(); registration.instanceFactory.factoryParameters = InstanceFactoryParameters(
registration.instanceType, CreatesSingleton.no, null, {
return cast(Object) initializer();
}); });
return registration; return registration;
} }
/** /**
* Scopes registrations to create a new instance using the given initializer delegate. On subsequent resolves the same instance is returned. * Scopes registrations to create a new instance using the given initializer delegate. On subsequent resolves the same instance is returned.
*/ */
public Registration initializedOnceBy(T : Object)(Registration registration, T delegate() initializer) { public Registration initializedOnceBy(T : Object)(Registration registration, T delegate() initializer)
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes, null, { {
return cast(Object) initializer(); registration.instanceFactory.factoryParameters = InstanceFactoryParameters(
registration.instanceType, CreatesSingleton.yes, null, {
return cast(Object) initializer();
}); });
return registration; return registration;
} }
public string toConcreteTypeListString(Registration[] registrations) { public string toConcreteTypeListString(Registration[] registrations)
{
auto concreteTypeListString = ""; auto concreteTypeListString = "";
foreach (registration ; registrations) { foreach (registration; registrations)
if (concreteTypeListString.length > 0) { {
if (concreteTypeListString.length > 0)
{
concreteTypeListString ~= ", "; concreteTypeListString ~= ", ";
} }
concreteTypeListString ~= registration.instanceType.toString(); concreteTypeListString ~= registration.instanceType.toString();
@ -138,4 +167,6 @@ public string toConcreteTypeListString(Registration[] registrations) {
return concreteTypeListString; return concreteTypeListString;
} }
class InstantiationContext {} class InstantiationContext
{
}

View file

@ -18,19 +18,23 @@ import std.string : format;
/** /**
* Thrown when something goes wrong during value injection. * Thrown when something goes wrong during value injection.
*/ */
class ValueInjectionException : Exception { class ValueInjectionException : Exception
{
mixin basicExceptionCtors; mixin basicExceptionCtors;
} }
/** /**
* Thrown by injectors when the value with the given key cannot be found. * Thrown by injectors when the value with the given key cannot be found.
*/ */
class ValueNotAvailableException : Exception { class ValueNotAvailableException : Exception
this(string key) { {
this(string key)
{
super(format("Value for key %s is not available", key)); super(format("Value for key %s is not available", key));
} }
this(string key, Throwable cause) { this(string key, Throwable cause)
{
super(format("Value for key %s is not available", key), cause); super(format("Value for key %s is not available", key), cause);
} }
} }
@ -52,7 +56,8 @@ class ValueNotAvailableException : Exception {
* } * }
* --- * ---
*/ */
struct Value { struct Value
{
/** /**
* The textual key used to find the value by injectors. * The textual key used to find the value by injectors.
* *
@ -78,7 +83,8 @@ struct Value {
* } * }
* --- * ---
*/ */
struct MandatoryValue { struct MandatoryValue
{
/** /**
* The textual key used to find the value by injectors. * The textual key used to find the value by injectors.
* *
@ -109,7 +115,8 @@ struct MandatoryValue {
* container.register!(ValueInjector!int, MyIntInjector); * container.register!(ValueInjector!int, MyIntInjector);
* --- * ---
*/ */
interface ValueInjector(Type) { interface ValueInjector(Type)
{
/** /**
* Get a value from the injector by key. * Get a value from the injector by key.
* *
@ -120,4 +127,3 @@ interface ValueInjector(Type) {
*/ */
Type get(string key); Type get(string key);
} }

View file

@ -10,10 +10,12 @@ import poodinis.test.testClasses;
import std.exception; import std.exception;
version(unittest) { version (unittest)
{
// Test autowiring concrete type to existing instance // Test autowiring concrete type to existing instance
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ComponentA; container.register!ComponentA;
auto componentB = new ComponentB(); auto componentB = new ComponentB();
@ -22,7 +24,8 @@ version(unittest) {
} }
// Test autowiring interface type to existing instance // Test autowiring interface type to existing instance
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC); container.register!(InterfaceA, ComponentC);
auto componentD = new ComponentD(); auto componentD = new ComponentD();
@ -31,16 +34,19 @@ version(unittest) {
} }
// Test autowiring private members // Test autowiring private members
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC); container.register!(InterfaceA, ComponentC);
auto componentD = new ComponentD(); auto componentD = new ComponentD();
container.autowire(componentD); container.autowire(componentD);
assert(componentD.privateComponentC is componentD.componentC, "Autowire private dependency failed"); assert(componentD.privateComponentC is componentD.componentC,
"Autowire private dependency failed");
} }
// Test autowiring will only happen once // Test autowiring will only happen once
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC).newInstance(); container.register!(InterfaceA, ComponentC).newInstance();
auto componentD = new ComponentD(); auto componentD = new ComponentD();
@ -48,37 +54,45 @@ version(unittest) {
auto expectedComponent = componentD.componentC; auto expectedComponent = componentD.componentC;
container.autowire(componentD); container.autowire(componentD);
auto actualComponent = componentD.componentC; auto actualComponent = componentD.componentC;
assert(expectedComponent is actualComponent, "Autowiring the second time wired a different instance"); assert(expectedComponent is actualComponent,
"Autowiring the second time wired a different instance");
} }
// Test autowiring unregistered type // Test autowiring unregistered type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto componentD = new ComponentD(); auto componentD = new ComponentD();
assertThrown!(ResolveException)(container.autowire(componentD), "Autowiring unregistered type should throw ResolveException"); assertThrown!(ResolveException)(container.autowire(componentD),
"Autowiring unregistered type should throw ResolveException");
} }
// Test autowiring member with non-autowire attribute does not autowire // Test autowiring member with non-autowire attribute does not autowire
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto componentE = new ComponentE(); auto componentE = new ComponentE();
container.autowire(componentE); container.autowire(componentE);
assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire"); assert(componentE.componentC is null,
"Autowiring should not occur for members with attributes other than @Autowire");
} }
// Test autowire class with alias declaration // Test autowire class with alias declaration
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ComponentA; container.register!ComponentA;
auto componentDeclarationCocktail = new ComponentDeclarationCocktail(); auto componentDeclarationCocktail = new ComponentDeclarationCocktail();
container.autowire(componentDeclarationCocktail); container.autowire(componentDeclarationCocktail);
assert(componentDeclarationCocktail.componentA !is null, "Autowiring class with non-assignable declarations failed"); assert(componentDeclarationCocktail.componentA !is null,
"Autowiring class with non-assignable declarations failed");
} }
// Test autowire class with qualifier // Test autowire class with qualifier
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC); container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX); container.register!(InterfaceA, ComponentX);
@ -91,7 +105,8 @@ version(unittest) {
} }
// Test autowire class with multiple qualifiers // Test autowire class with multiple qualifiers
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC); container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX); container.register!(InterfaceA, ComponentX);
@ -101,23 +116,29 @@ version(unittest) {
auto bootstrapBootstrap = new BootstrapBootstrap(); auto bootstrapBootstrap = new BootstrapBootstrap();
container.autowire(bootstrapBootstrap); container.autowire(bootstrapBootstrap);
assert(bootstrapBootstrap.componentX is componentX, "Autowiring class with multiple qualifiers failed"); assert(bootstrapBootstrap.componentX is componentX,
assert(bootstrapBootstrap.componentC is componentC, "Autowiring class with multiple qualifiers failed"); "Autowiring class with multiple qualifiers failed");
assert(bootstrapBootstrap.componentC is componentC,
"Autowiring class with multiple qualifiers failed");
} }
// Test getting instance from autowired registration will autowire instance // Test getting instance from autowired registration will autowire instance
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ComponentA; container.register!ComponentA;
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), new InstanceFactory(), container).singleInstance(); auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB),
auto instance = cast(ComponentB) registration.getInstance(new AutowireInstantiationContext()); new InstanceFactory(), container).singleInstance();
auto instance = cast(ComponentB) registration.getInstance(
new AutowireInstantiationContext());
assert(instance.componentA !is null); assert(instance.componentA !is null);
} }
// Test autowiring a dynamic array with all qualified types // Test autowiring a dynamic array with all qualified types
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC); container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX); container.register!(InterfaceA, ComponentX);
@ -129,7 +150,8 @@ version(unittest) {
} }
// Test autowiring new instance of singleinstance registration with newInstance UDA // Test autowiring new instance of singleinstance registration with newInstance UDA
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ComponentA; container.register!ComponentA;
@ -138,11 +160,13 @@ version(unittest) {
container.autowire(charlie); container.autowire(charlie);
assert(charlie.componentA !is regularComponentA, "Autowiring class with AssignNewInstance did not yield a different instance"); assert(charlie.componentA !is regularComponentA,
"Autowiring class with AssignNewInstance did not yield a different instance");
} }
// Test autowiring members from base class // Test autowiring members from base class
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ComponentA; container.register!ComponentA;
container.register!ComponentB; container.register!ComponentB;
@ -155,7 +179,8 @@ version(unittest) {
} }
// Test autowiring optional dependencies // Test autowiring optional dependencies
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto instance = new OuttaTime(); auto instance = new OuttaTime();
@ -167,7 +192,8 @@ version(unittest) {
} }
// Test autowiring class using value injection // Test autowiring class using value injection
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, TestInjector); container.register!(ValueInjector!int, TestInjector);
@ -181,7 +207,8 @@ version(unittest) {
} }
// Test autowiring classes with recursive template parameters // Test autowiring classes with recursive template parameters
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!CircularTemplateComponentA; container.register!CircularTemplateComponentA;
container.register!CircularTemplateComponentB; container.register!CircularTemplateComponentB;

View file

@ -12,17 +12,21 @@ import poodinis.test.foreignDependencies;
import std.exception; import std.exception;
import core.thread; import core.thread;
version(unittest) { version (unittest)
{
// Test register concrete type // Test register concrete type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto registration = container.register!TestClass; auto registration = container.register!TestClass;
assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same"); assert(registration.registeredType == typeid(TestClass),
"Type of registered type not the same");
} }
// Test resolve registered type // Test resolve registered type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestClass; container.register!TestClass;
TestClass actualInstance = container.resolve!TestClass; TestClass actualInstance = container.resolve!TestClass;
@ -31,110 +35,125 @@ version(unittest) {
} }
// Test register interface // Test register interface
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(TestInterface, TestClass); container.register!(TestInterface, TestClass);
TestInterface actualInstance = container.resolve!TestInterface; TestInterface actualInstance = container.resolve!TestInterface;
assert(actualInstance !is null, "Resolved type is null"); assert(actualInstance !is null, "Resolved type is null");
assert(cast(TestInterface) actualInstance, "Resolved class is not the same type as expected"); assert(cast(TestInterface) actualInstance,
"Resolved class is not the same type as expected");
} }
// Test resolve non-registered type // Test resolve non-registered type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
assertThrown!ResolveException(container.resolve!TestClass, "Resolving non-registered type does not fail"); assertThrown!ResolveException(container.resolve!TestClass,
"Resolving non-registered type does not fail");
} }
// Test clear registrations // Test clear registrations
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestClass; container.register!TestClass;
container.clearAllRegistrations(); container.clearAllRegistrations();
assertThrown!ResolveException(container.resolve!TestClass, "Resolving cleared type does not fail"); assertThrown!ResolveException(container.resolve!TestClass,
"Resolving cleared type does not fail");
} }
// Test resolve single instance for type // Test resolve single instance for type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestClass.singleInstance(); container.register!TestClass.singleInstance();
auto instance1 = container.resolve!TestClass; auto instance1 = container.resolve!TestClass;
auto instance2 = container.resolve!TestClass; auto instance2 = container.resolve!TestClass;
assert(instance1 is instance2, "Resolved instance from single instance scope is not the each time it is resolved"); assert(instance1 is instance2,
"Resolved instance from single instance scope is not the each time it is resolved");
} }
// Test resolve new instance for type // Test resolve new instance for type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestClass.newInstance(); container.register!TestClass.newInstance();
auto instance1 = container.resolve!TestClass; auto instance1 = container.resolve!TestClass;
auto instance2 = container.resolve!TestClass; auto instance2 = container.resolve!TestClass;
assert(instance1 !is instance2, "Resolved instance from new instance scope is the same each time it is resolved"); assert(instance1 !is instance2,
"Resolved instance from new instance scope is the same each time it is resolved");
} }
// Test resolve existing instance for type // Test resolve existing instance for type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto expectedInstance = new TestClass(); auto expectedInstance = new TestClass();
container.register!TestClass.existingInstance(expectedInstance); container.register!TestClass.existingInstance(expectedInstance);
auto actualInstance = container.resolve!TestClass; auto actualInstance = container.resolve!TestClass;
assert(expectedInstance is actualInstance, "Resolved instance from existing instance scope is not the same as the registered instance"); assert(expectedInstance is actualInstance,
"Resolved instance from existing instance scope is not the same as the registered instance");
} }
// Test creating instance via custom initializer on resolve // Test creating instance via custom initializer on resolve
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto expectedInstance = new TestClass(); auto expectedInstance = new TestClass();
container.register!TestClass.initializedBy({ container.register!TestClass.initializedBy({ return expectedInstance; });
return expectedInstance;
});
auto actualInstance = container.resolve!TestClass; auto actualInstance = container.resolve!TestClass;
assert(expectedInstance is actualInstance, "Resolved instance does not come from the custom initializer"); assert(expectedInstance is actualInstance,
"Resolved instance does not come from the custom initializer");
} }
// Test creating instance via initializedBy creates new instance every time // Test creating instance via initializedBy creates new instance every time
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestClass.initializedBy({ container.register!TestClass.initializedBy({ return new TestClass(); });
return new TestClass();
});
auto firstInstance = container.resolve!TestClass; auto firstInstance = container.resolve!TestClass;
auto secondInstance = container.resolve!TestClass; auto secondInstance = container.resolve!TestClass;
assert(firstInstance !is secondInstance, "Resolved instance are not different instances"); assert(firstInstance !is secondInstance, "Resolved instance are not different instances");
} }
// Test creating instance via initializedOnceBy creates a singleton instance // Test creating instance via initializedOnceBy creates a singleton instance
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestClass.initializedOnceBy({ container.register!TestClass.initializedOnceBy({ return new TestClass(); });
return new TestClass();
});
auto firstInstance = container.resolve!TestClass; auto firstInstance = container.resolve!TestClass;
auto secondInstance = container.resolve!TestClass; auto secondInstance = container.resolve!TestClass;
assert(firstInstance is secondInstance, "Resolved instance are different instances"); assert(firstInstance is secondInstance, "Resolved instance are different instances");
} }
// Test autowire resolved instances // Test autowire resolved instances
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!AutowiredClass; container.register!AutowiredClass;
container.register!ComponentClass; container.register!ComponentClass;
auto componentInstance = container.resolve!ComponentClass; auto componentInstance = container.resolve!ComponentClass;
auto autowiredInstance = container.resolve!AutowiredClass; auto autowiredInstance = container.resolve!AutowiredClass;
assert(componentInstance.autowiredClass is autowiredInstance, "Member is not autowired upon resolving"); assert(componentInstance.autowiredClass is autowiredInstance,
"Member is not autowired upon resolving");
} }
// Test circular autowiring // Test circular autowiring
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ComponentMouse; container.register!ComponentMouse;
container.register!ComponentCat; container.register!ComponentCat;
auto mouse = container.resolve!ComponentMouse; auto mouse = container.resolve!ComponentMouse;
auto cat = container.resolve!ComponentCat; auto cat = container.resolve!ComponentCat;
assert(mouse.cat is cat && cat.mouse is mouse && mouse !is cat, "Circular dependencies should be autowirable"); assert(mouse.cat is cat && cat.mouse is mouse && mouse !is cat,
"Circular dependencies should be autowirable");
} }
// Test remove registration // Test remove registration
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestClass; container.register!TestClass;
container.removeRegistration!TestClass; container.removeRegistration!TestClass;
@ -142,7 +161,8 @@ version(unittest) {
} }
// Test autowiring does not autowire member where instance is non-null // Test autowiring does not autowire member where instance is non-null
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto existingA = new AutowiredClass(); auto existingA = new AutowiredClass();
auto existingB = new ComponentClass(); auto existingB = new ComponentClass();
@ -153,11 +173,13 @@ version(unittest) {
auto resolvedA = container.resolve!AutowiredClass; auto resolvedA = container.resolve!AutowiredClass;
auto resolvedB = container.resolve!ComponentClass; auto resolvedB = container.resolve!ComponentClass;
assert(resolvedB.autowiredClass is existingA && resolvedA !is existingA, "Autowiring shouldn't rewire member when it is already wired to an instance"); assert(resolvedB.autowiredClass is existingA && resolvedA !is existingA,
"Autowiring shouldn't rewire member when it is already wired to an instance");
} }
// Test autowiring circular dependency by third-degree // Test autowiring circular dependency by third-degree
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Eenie; container.register!Eenie;
container.register!Meenie; container.register!Meenie;
@ -165,11 +187,13 @@ version(unittest) {
auto eenie = container.resolve!Eenie; auto eenie = container.resolve!Eenie;
assert(eenie.meenie.moe.eenie is eenie, "Autowiring third-degree circular dependency failed"); assert(eenie.meenie.moe.eenie is eenie,
"Autowiring third-degree circular dependency failed");
} }
// Test autowiring deep circular dependencies // Test autowiring deep circular dependencies
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Ittie; container.register!Ittie;
container.register!Bittie; container.register!Bittie;
@ -181,7 +205,8 @@ version(unittest) {
} }
// Test autowiring deep circular dependencies with newInstance scope does not autowire new instance second time // Test autowiring deep circular dependencies with newInstance scope does not autowire new instance second time
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Ittie.newInstance(); container.register!Ittie.newInstance();
container.register!Bittie.newInstance(); container.register!Bittie.newInstance();
@ -189,29 +214,37 @@ version(unittest) {
auto ittie = container.resolve!Ittie; auto ittie = container.resolve!Ittie;
assert(ittie.bittie.banana.bittie.banana is null, "Autowiring deep dependencies with newInstance scope autowired a reoccuring type."); assert(ittie.bittie.banana.bittie.banana is null,
"Autowiring deep dependencies with newInstance scope autowired a reoccuring type.");
} }
// Test autowiring type registered by interface // Test autowiring type registered by interface
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Bunena; container.register!Bunena;
container.register!Bittie; container.register!Bittie;
container.register!(SuperInterface, SuperImplementation); container.register!(SuperInterface, SuperImplementation);
SuperImplementation superInstance = cast(SuperImplementation) container.resolve!SuperInterface; SuperImplementation superInstance = cast(SuperImplementation) container
.resolve!SuperInterface;
assert(!(superInstance.banana is null), "Instance which was resolved by interface type was not autowired."); assert(!(superInstance.banana is null),
"Instance which was resolved by interface type was not autowired.");
} }
// Test reusing a container after clearing all registrations // Test reusing a container after clearing all registrations
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Banana; container.register!Banana;
container.clearAllRegistrations(); container.clearAllRegistrations();
try { try
{
container.resolve!Banana; container.resolve!Banana;
} catch (ResolveException e) { }
catch (ResolveException e)
{
container.register!Banana; container.register!Banana;
return; return;
} }
@ -219,14 +252,16 @@ version(unittest) {
} }
// Test register multiple concrete classess to same interface type // Test register multiple concrete classess to same interface type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(Color, Blue); container.register!(Color, Blue);
container.register!(Color, Red); container.register!(Color, Red);
} }
// Test removing all registrations for type with multiple registrations. // Test removing all registrations for type with multiple registrations.
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(Color, Blue); container.register!(Color, Blue);
container.register!(Color, Red); container.register!(Color, Red);
@ -234,42 +269,51 @@ version(unittest) {
} }
// Test registering same registration again // Test registering same registration again
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto firstRegistration = container.register!(Color, Blue); auto firstRegistration = container.register!(Color, Blue);
auto secondRegistration = container.register!(Color, Blue); auto secondRegistration = container.register!(Color, Blue);
assert(firstRegistration is secondRegistration, "First registration is not the same as the second of equal types"); assert(firstRegistration is secondRegistration,
"First registration is not the same as the second of equal types");
} }
// Test resolve registration with multiple qualifiers // Test resolve registration with multiple qualifiers
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(Color, Blue); container.register!(Color, Blue);
container.register!(Color, Red); container.register!(Color, Red);
try { try
{
container.resolve!Color; container.resolve!Color;
} catch (ResolveException e) { }
catch (ResolveException e)
{
return; return;
} }
assert(false); assert(false);
} }
// Test resolve registration with multiple qualifiers using a qualifier // Test resolve registration with multiple qualifiers using a qualifier
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(Color, Blue); container.register!(Color, Blue);
container.register!(Color, Red); container.register!(Color, Red);
auto blueInstance = container.resolve!(Color, Blue); auto blueInstance = container.resolve!(Color, Blue);
auto redInstance = container.resolve!(Color, Red); auto redInstance = container.resolve!(Color, Red);
assert(blueInstance !is redInstance, "Resolving type with multiple, different registrations yielded the same instance"); assert(blueInstance !is redInstance,
"Resolving type with multiple, different registrations yielded the same instance");
assert(blueInstance !is null, "Resolved blue instance to null"); assert(blueInstance !is null, "Resolved blue instance to null");
assert(redInstance !is null, "Resolved red instance to null"); assert(redInstance !is null, "Resolved red instance to null");
} }
// Test autowire of unqualified member typed by interface. // Test autowire of unqualified member typed by interface.
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Spiders; container.register!Spiders;
container.register!(TestInterface, TestClass); container.register!(TestInterface, TestClass);
@ -280,38 +324,45 @@ version(unittest) {
} }
// Register existing registration // Register existing registration
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto firstRegistration = container.register!TestClass; auto firstRegistration = container.register!TestClass;
auto secondRegistration = container.register!TestClass; auto secondRegistration = container.register!TestClass;
assert(firstRegistration is secondRegistration, "Registering the same registration twice registers the dependencies twice."); assert(firstRegistration is secondRegistration,
"Registering the same registration twice registers the dependencies twice.");
} }
// Register existing registration by supertype // Register existing registration by supertype
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto firstRegistration = container.register!(TestInterface, TestClass); auto firstRegistration = container.register!(TestInterface, TestClass);
auto secondRegistration = container.register!(TestInterface, TestClass); auto secondRegistration = container.register!(TestInterface, TestClass);
assert(firstRegistration is secondRegistration, "Registering the same registration by super type twice registers the dependencies twice."); assert(firstRegistration is secondRegistration,
"Registering the same registration by super type twice registers the dependencies twice.");
} }
// Resolve dependency depending on itself // Resolve dependency depending on itself
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Recursive; container.register!Recursive;
auto instance = container.resolve!Recursive; auto instance = container.resolve!Recursive;
assert(instance.recursive is instance, "Resolving dependency that depends on itself fails."); assert(instance.recursive is instance, "Resolving dependency that depends on itself fails.");
assert(instance.recursive.recursive is instance, "Resolving dependency that depends on itself fails."); assert(instance.recursive.recursive is instance,
"Resolving dependency that depends on itself fails.");
} }
// Test autowire stack pop-back // Test autowire stack pop-back
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Moolah; container.register!Moolah;
container.register!Wants.newInstance(); container.register!Wants.newInstance();
@ -324,12 +375,11 @@ version(unittest) {
} }
// Test resolving registration registered in different thread // Test resolving registration registered in different thread
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto thread = new Thread(delegate() { auto thread = new Thread(delegate() { container.register!TestClass; });
container.register!TestClass;
});
thread.start(); thread.start();
thread.join(); thread.join();
@ -337,41 +387,47 @@ version(unittest) {
} }
// Test resolving instance previously resolved in different thread // Test resolving instance previously resolved in different thread
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
shared(TestClass) actualTestClass; shared(TestClass) actualTestClass;
container.register!TestClass; container.register!TestClass;
auto thread = new Thread(delegate() { auto thread = new Thread(delegate() {
actualTestClass = cast(shared(TestClass)) container.resolve!TestClass; actualTestClass = cast(shared(TestClass)) container.resolve!TestClass;
}); });
thread.start(); thread.start();
thread.join(); thread.join();
shared(TestClass) expectedTestClass = cast(shared(TestClass)) container.resolve!TestClass; shared(TestClass) expectedTestClass = cast(shared(TestClass)) container.resolve!TestClass;
assert(expectedTestClass is actualTestClass, "Instance resolved in main thread is not the one resolved in thread"); assert(expectedTestClass is actualTestClass,
"Instance resolved in main thread is not the one resolved in thread");
} }
// Test registering type with option doNotAddConcreteTypeRegistration // Test registering type with option doNotAddConcreteTypeRegistration
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(TestInterface, TestClass)(RegistrationOption.doNotAddConcreteTypeRegistration); container.register!(TestInterface,
TestClass)(RegistrationOption.doNotAddConcreteTypeRegistration);
auto firstInstance = container.resolve!TestInterface; auto firstInstance = container.resolve!TestInterface;
assertThrown!ResolveException(container.resolve!TestClass); assertThrown!ResolveException(container.resolve!TestClass);
} }
// Test registering conrete type with registration option doNotAddConcreteTypeRegistration does nothing // Test registering conrete type with registration option doNotAddConcreteTypeRegistration does nothing
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestClass(RegistrationOption.doNotAddConcreteTypeRegistration); container.register!TestClass(RegistrationOption.doNotAddConcreteTypeRegistration);
container.resolve!TestClass; container.resolve!TestClass;
} }
// Test registering type will register by contrete type by default // Test registering type will register by contrete type by default
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(TestInterface, TestClass); container.register!(TestInterface, TestClass);
@ -382,7 +438,8 @@ version(unittest) {
} }
// Test resolving all registrations to an interface // Test resolving all registrations to an interface
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(Color, Blue); container.register!(Color, Blue);
container.register!(Color, Red); container.register!(Color, Red);
@ -393,7 +450,8 @@ version(unittest) {
} }
// Test autowiring instances resolved in array // Test autowiring instances resolved in array
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!UnrelatedClass; container.register!UnrelatedClass;
container.register!(TestInterface, TestClassDeux); container.register!(TestInterface, TestClassDeux);
@ -405,38 +463,45 @@ version(unittest) {
} }
// Test set persistent registration options // Test set persistent registration options
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.setPersistentRegistrationOptions(RegistrationOption.doNotAddConcreteTypeRegistration); container.setPersistentRegistrationOptions(
RegistrationOption.doNotAddConcreteTypeRegistration);
container.register!(TestInterface, TestClass); container.register!(TestInterface, TestClass);
assertThrown!ResolveException(container.resolve!TestClass); assertThrown!ResolveException(container.resolve!TestClass);
} }
// Test unset persistent registration options // Test unset persistent registration options
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.setPersistentRegistrationOptions(RegistrationOption.doNotAddConcreteTypeRegistration); container.setPersistentRegistrationOptions(
RegistrationOption.doNotAddConcreteTypeRegistration);
container.unsetPersistentRegistrationOptions(); container.unsetPersistentRegistrationOptions();
container.register!(TestInterface, TestClass); container.register!(TestInterface, TestClass);
container.resolve!TestClass; container.resolve!TestClass;
} }
// Test registration when resolving // Test registration when resolving
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.resolve!(TestInterface, TestClass)(ResolveOption.registerBeforeResolving); container.resolve!(TestInterface, TestClass)(ResolveOption.registerBeforeResolving);
container.resolve!TestClass; container.resolve!TestClass;
} }
// Test set persistent resolve options // Test set persistent resolve options
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.setPersistentResolveOptions(ResolveOption.registerBeforeResolving); container.setPersistentResolveOptions(ResolveOption.registerBeforeResolving);
container.resolve!TestClass; container.resolve!TestClass;
} }
// Test unset persistent resolve options // Test unset persistent resolve options
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.setPersistentResolveOptions(ResolveOption.registerBeforeResolving); container.setPersistentResolveOptions(ResolveOption.registerBeforeResolving);
container.unsetPersistentResolveOptions(); container.unsetPersistentResolveOptions();
@ -444,27 +509,32 @@ version(unittest) {
} }
// Test ResolveOption registerBeforeResolving fails for interfaces // Test ResolveOption registerBeforeResolving fails for interfaces
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
assertThrown!ResolveException(container.resolve!TestInterface(ResolveOption.registerBeforeResolving)); assertThrown!ResolveException(
container.resolve!TestInterface(ResolveOption.registerBeforeResolving));
} }
// Test ResolveOption noResolveException does not throw // Test ResolveOption noResolveException does not throw
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto instance = container.resolve!TestInterface(ResolveOption.noResolveException); auto instance = container.resolve!TestInterface(ResolveOption.noResolveException);
assert(instance is null); assert(instance is null);
} }
// ResolveOption noResolveException does not throw for resolveAll // ResolveOption noResolveException does not throw for resolveAll
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto instances = container.resolveAll!TestInterface(ResolveOption.noResolveException); auto instances = container.resolveAll!TestInterface(ResolveOption.noResolveException);
assert(instances.length == 0); assert(instances.length == 0);
} }
// Test autowired, constructor injected class // Test autowired, constructor injected class
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Red; container.register!Red;
container.register!Moolah; container.register!Moolah;
@ -478,7 +548,8 @@ version(unittest) {
} }
// Test injecting constructor with super-type parameter // Test injecting constructor with super-type parameter
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Wallpaper; container.register!Wallpaper;
container.register!(Color, Blue); container.register!(Color, Blue);
@ -489,7 +560,8 @@ version(unittest) {
} }
// Test prevention of circular dependencies during constructor injection // Test prevention of circular dependencies during constructor injection
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Pot; container.register!Pot;
container.register!Kettle; container.register!Kettle;
@ -498,7 +570,8 @@ version(unittest) {
} }
// Test prevention of transitive circular dependencies during constructor injection // Test prevention of transitive circular dependencies during constructor injection
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Rock; container.register!Rock;
container.register!Paper; container.register!Paper;
@ -508,7 +581,8 @@ version(unittest) {
} }
// Test injection of foreign dependency in constructor // Test injection of foreign dependency in constructor
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Ola; container.register!Ola;
container.register!Hello; container.register!Hello;
@ -516,7 +590,8 @@ version(unittest) {
} }
// Test PostConstruct method is called after resolving a dependency // Test PostConstruct method is called after resolving a dependency
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!PostConstructionDependency; container.register!PostConstructionDependency;
@ -525,7 +600,8 @@ version(unittest) {
} }
// Test PostConstruct of base type is called // Test PostConstruct of base type is called
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ChildOfPostConstruction; container.register!ChildOfPostConstruction;
@ -534,7 +610,8 @@ version(unittest) {
} }
// Test PostConstruct of class implementing interface is not called // Test PostConstruct of class implementing interface is not called
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ButThereWontBe; container.register!ButThereWontBe;
@ -543,7 +620,8 @@ version(unittest) {
} }
// Test postconstruction happens after autowiring and value injection // Test postconstruction happens after autowiring and value injection
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, PostConstructingIntInjector); container.register!(ValueInjector!int, PostConstructingIntInjector);
container.register!PostConstructionDependency; container.register!PostConstructionDependency;
@ -552,7 +630,8 @@ version(unittest) {
} }
// Test PreDestroy is called when removing a registration // Test PreDestroy is called when removing a registration
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!PreDestroyerOfFates; container.register!PreDestroyerOfFates;
auto instance = container.resolve!PreDestroyerOfFates; auto instance = container.resolve!PreDestroyerOfFates;
@ -561,7 +640,8 @@ version(unittest) {
} }
// Test PreDestroy is called when removing all registrations // Test PreDestroy is called when removing all registrations
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!PreDestroyerOfFates; container.register!PreDestroyerOfFates;
auto instance = container.resolve!PreDestroyerOfFates; auto instance = container.resolve!PreDestroyerOfFates;
@ -570,7 +650,8 @@ version(unittest) {
} }
// Test PreDestroy is called when the container is destroyed // Test PreDestroy is called when the container is destroyed
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!PreDestroyerOfFates; container.register!PreDestroyerOfFates;
auto instance = container.resolve!PreDestroyerOfFates; auto instance = container.resolve!PreDestroyerOfFates;

View file

@ -10,10 +10,12 @@ import poodinis.test.testClasses;
import std.exception; import std.exception;
version(unittest) { version (unittest)
{
//Test register component registrations from context //Test register component registrations from context
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto context = new TestContext(); auto context = new TestContext();
context.registerContextComponents(container); context.registerContextComponents(container);
@ -23,7 +25,8 @@ version(unittest) {
} }
//Test non-annotated methods are not registered //Test non-annotated methods are not registered
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto context = new TestContext(); auto context = new TestContext();
context.registerContextComponents(container); context.registerContextComponents(container);
@ -31,7 +34,8 @@ version(unittest) {
} }
//Test register component by base type //Test register component by base type
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto context = new TestContext(); auto context = new TestContext();
context.registerContextComponents(container); context.registerContextComponents(container);
@ -40,7 +44,8 @@ version(unittest) {
} }
//Test register components with multiple candidates //Test register components with multiple candidates
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto context = new TestContext(); auto context = new TestContext();
context.registerContextComponents(container); context.registerContextComponents(container);
@ -53,7 +58,8 @@ version(unittest) {
} }
//Test register component as prototype //Test register component as prototype
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto context = new TestContext(); auto context = new TestContext();
context.registerContextComponents(container); context.registerContextComponents(container);
@ -66,7 +72,8 @@ version(unittest) {
} }
// Test setting up simple dependencies through application context // Test setting up simple dependencies through application context
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.registerContext!SimpleContext; container.registerContext!SimpleContext;
auto instance = container.resolve!CakeChart; auto instance = container.resolve!CakeChart;
@ -75,7 +82,8 @@ version(unittest) {
} }
// Test resolving dependency from registered application context // Test resolving dependency from registered application context
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.registerContext!SimpleContext; container.registerContext!SimpleContext;
auto instance = container.resolve!Apple; auto instance = container.resolve!Apple;
@ -84,7 +92,8 @@ version(unittest) {
} }
// Test autowiring application context // Test autowiring application context
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!Apple; container.register!Apple;
container.registerContext!AutowiredTestContext; container.registerContext!AutowiredTestContext;
@ -95,7 +104,8 @@ version(unittest) {
} }
// Test autowiring application context with dependencies registered in same context // Test autowiring application context with dependencies registered in same context
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.registerContext!ComplexAutowiredTestContext; container.registerContext!ComplexAutowiredTestContext;
auto instance = container.resolve!ClassWrapperWrapper; auto instance = container.resolve!ClassWrapperWrapper;
@ -108,7 +118,8 @@ version(unittest) {
} }
// Test resolving registered context // Test resolving registered context
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.registerContext!TestContext; container.registerContext!TestContext;
container.resolve!ApplicationContext; container.resolve!ApplicationContext;

View file

@ -10,12 +10,15 @@ import poodinis.test.testClasses;
import std.exception; import std.exception;
version(unittest) { version (unittest)
{
// Test instance factory with singletons // Test instance factory with singletons
unittest { unittest
{
auto factory = new InstanceFactory(); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.yes); factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation),
CreatesSingleton.yes);
auto instanceOne = factory.getInstance(); auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance(); auto instanceTwo = factory.getInstance();
@ -24,9 +27,11 @@ version(unittest) {
} }
// Test instance factory with new instances // Test instance factory with new instances
unittest { unittest
{
auto factory = new InstanceFactory(); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.no); factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation),
CreatesSingleton.no);
auto instanceOne = factory.getInstance(); auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance(); auto instanceTwo = factory.getInstance();
@ -35,45 +40,57 @@ version(unittest) {
} }
// Test instance factory with existing instances // Test instance factory with existing instances
unittest { unittest
{
auto existingInstance = new TestImplementation(); auto existingInstance = new TestImplementation();
auto factory = new InstanceFactory(); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.yes, existingInstance); factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation),
CreatesSingleton.yes, existingInstance);
auto instanceOne = factory.getInstance(); auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance(); auto instanceTwo = factory.getInstance();
assert(instanceOne is existingInstance, "Created factory instance is not the existing instance"); assert(instanceOne is existingInstance,
assert(instanceTwo is existingInstance, "Created factory instance is not the existing instance when called again"); "Created factory instance is not the existing instance");
assert(instanceTwo is existingInstance,
"Created factory instance is not the existing instance when called again");
} }
// Test instance factory with existing instances when setting singleton flag to "no" // Test instance factory with existing instances when setting singleton flag to "no"
unittest { unittest
{
auto existingInstance = new TestImplementation(); auto existingInstance = new TestImplementation();
auto factory = new InstanceFactory(); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.no, existingInstance); factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation),
CreatesSingleton.no, existingInstance);
auto instance = factory.getInstance(); auto instance = factory.getInstance();
assert(instance is existingInstance, "Created factory instance is not the existing instance"); assert(instance is existingInstance,
"Created factory instance is not the existing instance");
} }
// Test creating instance using custom factory method // Test creating instance using custom factory method
unittest { unittest
Object factoryMethod() { {
Object factoryMethod()
{
auto instance = new TestImplementation(); auto instance = new TestImplementation();
instance.someContent = "Ducks!"; instance.someContent = "Ducks!";
return instance; return instance;
} }
auto factory = new InstanceFactory(); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(null, CreatesSingleton.yes, null, &factoryMethod); factory.factoryParameters = InstanceFactoryParameters(null,
CreatesSingleton.yes, null, &factoryMethod);
auto instance = cast(TestImplementation) factory.getInstance(); auto instance = cast(TestImplementation) factory.getInstance();
assert(instance !is null, "No instance was created by factory or could not be cast to expected type"); assert(instance !is null,
"No instance was created by factory or could not be cast to expected type");
assert(instance.someContent == "Ducks!"); assert(instance.someContent == "Ducks!");
} }
// Test injecting constructor of class // Test injecting constructor of class
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!TestImplementation; container.register!TestImplementation;
@ -85,12 +102,14 @@ version(unittest) {
} }
// Test injecting constructor of class with multiple constructors injects the first candidate // Test injecting constructor of class with multiple constructors injects the first candidate
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen; container.register!SomeOtherClassThen;
container.register!TestImplementation; container.register!TestImplementation;
auto factory = new ConstructorInjectingInstanceFactory!ClassWithMultipleConstructors(container); auto factory = new ConstructorInjectingInstanceFactory!ClassWithMultipleConstructors(
container);
auto instance = cast(ClassWithMultipleConstructors) factory.getInstance(); auto instance = cast(ClassWithMultipleConstructors) factory.getInstance();
assert(instance !is null); assert(instance !is null);
@ -99,12 +118,14 @@ version(unittest) {
} }
// Test injecting constructor of class with multiple constructor parameters // Test injecting constructor of class with multiple constructor parameters
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen; container.register!SomeOtherClassThen;
container.register!TestImplementation; container.register!TestImplementation;
auto factory = new ConstructorInjectingInstanceFactory!ClassWithConstructorWithMultipleParameters(container); auto factory = new ConstructorInjectingInstanceFactory!ClassWithConstructorWithMultipleParameters(
container);
auto instance = cast(ClassWithConstructorWithMultipleParameters) factory.getInstance(); auto instance = cast(ClassWithConstructorWithMultipleParameters) factory.getInstance();
assert(instance !is null); assert(instance !is null);
@ -113,11 +134,13 @@ version(unittest) {
} }
// Test injecting constructor of class with primitive constructor parameters // Test injecting constructor of class with primitive constructor parameters
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen; container.register!SomeOtherClassThen;
auto factory = new ConstructorInjectingInstanceFactory!ClassWithPrimitiveConstructor(container); auto factory = new ConstructorInjectingInstanceFactory!ClassWithPrimitiveConstructor(
container);
auto instance = cast(ClassWithPrimitiveConstructor) factory.getInstance(); auto instance = cast(ClassWithPrimitiveConstructor) factory.getInstance();
assert(instance !is null); assert(instance !is null);
@ -125,7 +148,8 @@ version(unittest) {
} }
// Test injecting constructor of class with struct constructor parameters // Test injecting constructor of class with struct constructor parameters
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen; container.register!SomeOtherClassThen;
@ -137,7 +161,8 @@ version(unittest) {
} }
// Test injecting constructor of class with empty constructor will skip injection // Test injecting constructor of class with empty constructor will skip injection
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto factory = new ConstructorInjectingInstanceFactory!ClassWithEmptyConstructor(container); auto factory = new ConstructorInjectingInstanceFactory!ClassWithEmptyConstructor(container);
@ -148,10 +173,12 @@ version(unittest) {
} }
// Test injecting constructor of class with no candidates fails // Test injecting constructor of class with no candidates fails
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto factory = new ConstructorInjectingInstanceFactory!ClassWithNonInjectableConstructor(container); auto factory = new ConstructorInjectingInstanceFactory!ClassWithNonInjectableConstructor(
container);
assertThrown!InstanceCreationException(factory.getInstance()); assertThrown!InstanceCreationException(factory.getInstance());
} }

View file

@ -7,6 +7,9 @@
module poodinis.test.foreignDependencies; module poodinis.test.foreignDependencies;
version(unittest) { version (unittest)
class Ola {} {
class Ola
{
}
} }

View file

@ -10,48 +10,65 @@ import poodinis.test.testClasses;
import std.exception; import std.exception;
version(unittest) { version (unittest)
{
// Test getting instance without scope defined throws exception // Test getting instance without scope defined throws exception
unittest { unittest
{
Registration registration = new Registration(typeid(TestType), null, null, null); Registration registration = new Registration(typeid(TestType), null, null, null);
assertThrown!(InstanceCreationException)(registration.getInstance(), null); assertThrown!(InstanceCreationException)(registration.getInstance(), null);
} }
// Test set single instance scope using scope setter // Test set single instance scope using scope setter
unittest { unittest
Registration registration = new Registration(null, typeid(TestType), new InstanceFactory(), null); {
Registration registration = new Registration(null, typeid(TestType),
new InstanceFactory(), null);
auto chainedRegistration = registration.singleInstance(); auto chainedRegistration = registration.singleInstance();
auto instance1 = registration.getInstance(); auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance(); auto instance2 = registration.getInstance();
assert(instance1 is instance2, "Registration with single instance scope did not return the same instance"); assert(instance1 is instance2,
assert(registration is chainedRegistration, "Registration returned by scope setting is not the same as the registration being set"); "Registration with single instance scope did not return the same instance");
assert(registration is chainedRegistration,
"Registration returned by scope setting is not the same as the registration being set");
} }
// Test set new instance scope using scope setter // Test set new instance scope using scope setter
unittest { unittest
Registration registration = new Registration(null, typeid(TestType), new InstanceFactory(), null); {
Registration registration = new Registration(null, typeid(TestType),
new InstanceFactory(), null);
auto chainedRegistration = registration.newInstance(); auto chainedRegistration = registration.newInstance();
auto instance1 = registration.getInstance(); auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance(); auto instance2 = registration.getInstance();
assert(instance1 !is instance2, "Registration with new instance scope did not return a different instance"); assert(instance1 !is instance2,
assert(registration is chainedRegistration, "Registration returned by scope setting is not the same as the registration being set"); "Registration with new instance scope did not return a different instance");
assert(registration is chainedRegistration,
"Registration returned by scope setting is not the same as the registration being set");
} }
// Test set existing instance scope using scope setter // Test set existing instance scope using scope setter
unittest { unittest
{
Registration registration = new Registration(null, null, new InstanceFactory(), null); Registration registration = new Registration(null, null, new InstanceFactory(), null);
auto expectedInstance = new TestType(); auto expectedInstance = new TestType();
auto chainedRegistration = registration.existingInstance(expectedInstance); auto chainedRegistration = registration.existingInstance(expectedInstance);
auto actualInstance = registration.getInstance(); auto actualInstance = registration.getInstance();
assert(expectedInstance is expectedInstance, "Registration with existing instance scope did not return the same instance"); assert(expectedInstance is expectedInstance,
assert(registration is chainedRegistration, "Registration returned by scope setting is not the same as the registration being set"); "Registration with existing instance scope did not return the same instance");
assert(registration is chainedRegistration,
"Registration returned by scope setting is not the same as the registration being set");
} }
// Test linking registrations // Test linking registrations
unittest { unittest
Registration firstRegistration = new Registration(typeid(TestInterface), typeid(TestImplementation), new InstanceFactory(), null).singleInstance(); {
Registration secondRegistration = new Registration(typeid(TestImplementation), typeid(TestImplementation), new InstanceFactory(), null).singleInstance().linkTo(firstRegistration); Registration firstRegistration = new Registration(typeid(TestInterface),
typeid(TestImplementation), new InstanceFactory(), null).singleInstance();
Registration secondRegistration = new Registration(typeid(TestImplementation),
typeid(TestImplementation), new InstanceFactory(), null).singleInstance()
.linkTo(firstRegistration);
auto firstInstance = firstRegistration.getInstance(); auto firstInstance = firstRegistration.getInstance();
auto secondInstance = secondRegistration.getInstance(); auto secondInstance = secondRegistration.getInstance();

File diff suppressed because it is too large Load diff

View file

@ -5,4 +5,6 @@
* The full terms of the license can be found in the LICENSE file. * The full terms of the license can be found in the LICENSE file.
*/ */
void main() {} void main()
{
}

View file

@ -10,26 +10,32 @@ import poodinis.test.testClasses;
import std.exception; import std.exception;
version(unittest) { version (unittest)
{
struct LocalStruct { struct LocalStruct
{
bool wasInjected = false; bool wasInjected = false;
} }
class LocalStructInjector : ValueInjector!LocalStruct { class LocalStructInjector : ValueInjector!LocalStruct
public override LocalStruct get(string key) { {
public override LocalStruct get(string key)
{
auto data = LocalStruct(true); auto data = LocalStruct(true);
return data; return data;
} }
} }
class LocalClassWithStruct { class LocalClassWithStruct
{
@Value("") @Value("")
public LocalStruct localStruct; public LocalStruct localStruct;
} }
// Test injection of values // Test injection of values
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!MyConfig; container.register!MyConfig;
container.register!(ValueInjector!int, IntInjector); container.register!(ValueInjector!int, IntInjector);
@ -43,7 +49,8 @@ version(unittest) {
} }
// Test injection of values throws exception when injector is not there // Test injection of values throws exception when injector is not there
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!MyConfig; container.register!MyConfig;
assertThrown!ResolveException(container.resolve!MyConfig); assertThrown!ResolveException(container.resolve!MyConfig);
@ -52,7 +59,8 @@ version(unittest) {
} }
// Test injection of values with defaults // Test injection of values with defaults
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ConfigWithDefaults; container.register!ConfigWithDefaults;
container.register!(ValueInjector!int, DefaultIntInjector); container.register!(ValueInjector!int, DefaultIntInjector);
@ -62,7 +70,8 @@ version(unittest) {
} }
// Test mandatory injection of values which are available // Test mandatory injection of values which are available
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ConfigWithMandatory; container.register!ConfigWithMandatory;
container.register!(ValueInjector!int, MandatoryAvailableIntInjector); container.register!(ValueInjector!int, MandatoryAvailableIntInjector);
@ -72,7 +81,8 @@ version(unittest) {
} }
// Test mandatory injection of values which are not available // Test mandatory injection of values which are not available
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ConfigWithMandatory; container.register!ConfigWithMandatory;
container.register!(ValueInjector!int, MandatoryUnavailableIntInjector); container.register!(ValueInjector!int, MandatoryUnavailableIntInjector);
@ -82,7 +92,8 @@ version(unittest) {
} }
// Test injecting dependencies within value injectors // Test injecting dependencies within value injectors
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
auto dependency = new Dependency(); auto dependency = new Dependency();
container.register!Dependency.existingInstance(dependency); container.register!Dependency.existingInstance(dependency);
@ -93,7 +104,8 @@ version(unittest) {
} }
// Test injecting circular dependencies within value injectors // Test injecting circular dependencies within value injectors
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, CircularIntInjector); container.register!(ValueInjector!int, CircularIntInjector);
auto injector = cast(CircularIntInjector) container.resolve!(ValueInjector!int); auto injector = cast(CircularIntInjector) container.resolve!(ValueInjector!int);
@ -103,7 +115,8 @@ version(unittest) {
} }
// Test value injection within value injectors // Test value injection within value injectors
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, ValueInjectedIntInjector); container.register!(ValueInjector!int, ValueInjectedIntInjector);
auto injector = cast(ValueInjectedIntInjector) container.resolve!(ValueInjector!int); auto injector = cast(ValueInjectedIntInjector) container.resolve!(ValueInjector!int);
@ -112,18 +125,21 @@ version(unittest) {
} }
// Test value injection within dependencies of value injectors // Test value injection within dependencies of value injectors
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!ConfigWithDefaults; container.register!ConfigWithDefaults;
container.register!(ValueInjector!int, DependencyValueInjectedIntInjector); container.register!(ValueInjector!int, DependencyValueInjectedIntInjector);
auto injector = cast(DependencyValueInjectedIntInjector) container.resolve!(ValueInjector!int); auto injector = cast(DependencyValueInjectedIntInjector) container.resolve!(
ValueInjector!int);
assert(injector.config.noms == 8899); assert(injector.config.noms == 8899);
} }
// Test resolving locally defined struct injector (github issue #20) // Test resolving locally defined struct injector (github issue #20)
unittest { unittest
{
auto container = new shared DependencyContainer(); auto container = new shared DependencyContainer();
container.register!(ValueInjector!LocalStruct, LocalStructInjector); container.register!(ValueInjector!LocalStruct, LocalStructInjector);
container.register!LocalClassWithStruct; container.register!LocalClassWithStruct;