Update copyrigths

And convert tabs to spaces
This commit is contained in:
Mike Bierlee 2018-01-01 15:54:32 +01:00
parent 9d0bbfd6cf
commit 4410206ec4
74 changed files with 2396 additions and 2384 deletions

1
.gitignore vendored
View file

@ -19,3 +19,4 @@
/.idea
/*.iml
/out
/classes

View file

@ -1,4 +1,4 @@
Copyright (c) 2014-2017 Mike Bierlee
Copyright (c) 2014-2018 Mike Bierlee
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -1,7 +1,7 @@
Poodinis Dependency Injection Framework
=======================================
Version 8.0.1
Copyright 2014-2017 Mike Bierlee
Copyright 2014-2018 Mike Bierlee
Licensed under the terms of the MIT license - See [LICENSE.txt](LICENSE.txt)
Master: [![Build Status](https://api.travis-ci.org/mbierlee/poodinis.png?branch=master)](https://travis-ci.org/mbierlee/poodinis) - Dev: [![Build Status](https://api.travis-ci.org/mbierlee/poodinis.png?branch=develop)](https://travis-ci.org/mbierlee/poodinis)

View file

@ -83,7 +83,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -155,7 +155,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -101,7 +101,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -93,7 +93,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -106,7 +106,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -104,7 +104,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -177,7 +177,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -91,7 +91,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -207,7 +207,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -162,7 +162,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -97,7 +97,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -189,7 +189,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -109,7 +109,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -95,7 +95,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -95,7 +95,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -91,7 +91,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -91,7 +91,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -96,7 +96,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -96,7 +96,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -92,7 +92,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -107,7 +107,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -92,7 +92,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -116,7 +116,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -142,7 +142,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -92,7 +92,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -92,7 +92,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -92,7 +92,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -100,7 +100,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -83,7 +83,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -102,7 +102,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
Mike Bierlee (m.bierlee@lostmoment.com)
</p>
<p>Copyright Digital Mars 2005 - 2009., Copyright Andrei Alexandrescu 2008-, Jonathan M Davis 2011-., 2014-2017 Mike Bierlee
<p>Copyright Digital Mars 2005 - 2009., Copyright Andrei Alexandrescu 2008-, Jonathan M Davis 2011-., 2014-2018 Mike Bierlee
</p>
<p>boost.org/LICENSE_1_0.txt, Boost License 1.0

View file

@ -121,7 +121,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -97,7 +97,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -95,7 +95,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -98,7 +98,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -156,7 +156,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -120,7 +120,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -106,7 +106,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -120,7 +120,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -106,7 +106,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -92,7 +92,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -103,7 +103,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -128,7 +128,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -92,7 +92,7 @@ document.getElementById('symbolSearchPane').style.display = 'block';</script>
<p>Mike Bierlee, m.bierlee@lostmoment.com
</p>
<p>2014-2017 Mike Bierlee
<p>2014-2018 Mike Bierlee
</p>
<p>This software is licensed under the terms of the MIT license.

View file

@ -3,7 +3,7 @@
"description" : "A dependency injection framework with support for autowiring.",
"homepage": "http://lostmoment.com/open-source/poodinis",
"authors": ["Mike Bierlee"],
"copyright": "Copyright 2014-2017 Mike Bierlee",
"copyright": "Copyright 2014-2018 Mike Bierlee",
"license": "MIT",
"-ddoxTool": "scod",
"configurations": [

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -9,7 +9,7 @@
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2017 Mike Bierlee
* Copyright: 2014-2018 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
@ -30,7 +30,7 @@ import std.string;
import std.traits;
import std.range;
private struct UseMemberType {};
private struct UseMemberType {}
/**
* UDA for annotating class members as candidates for autowiring.
@ -66,15 +66,15 @@ private struct UseMemberType {};
* will also be autowired because the autowire mechanism knows that member "fuelEngine" is an instance of "FuelEngine"
*/
struct Autowire(QualifierType) {
QualifierType qualifier;
};
QualifierType qualifier;
}
/**
* UDA for marking autowired dependencies optional.
* Optional dependencies will not lead to a resolveException when there is no type registered for them.
* 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.
@ -92,7 +92,7 @@ struct OptionalDependency {};
struct AssignNewInstance {}
private void printDebugAutowiredInstance(TypeInfo instanceType, void* instanceAddress) {
writeln(format("DEBUG: Autowiring members of [%s@%s]", instanceType, instanceAddress));
writeln(format("DEBUG: Autowiring members of [%s@%s]", instanceType, instanceAddress));
}
/**
@ -105,129 +105,129 @@ private void printDebugAutowiredInstance(TypeInfo instanceType, void* instanceAd
* See_Also: Autowire
*/
public void autowire(Type)(shared(DependencyContainer) container, Type instance) {
debug(poodinisVerbose) {
printDebugAutowiredInstance(typeid(Type), &instance);
}
debug(poodinisVerbose) {
printDebugAutowiredInstance(typeid(Type), &instance);
}
// Recurse into base class if there are more between Type and Object in the hierarchy
static if(BaseClassesTuple!Type.length > 1)
{
autowire!(BaseClassesTuple!Type[0])(container, instance);
}
// Recurse into base class if there are more between Type and Object in the hierarchy
static if(BaseClassesTuple!Type.length > 1)
{
autowire!(BaseClassesTuple!Type[0])(container, instance);
}
foreach(index, name; FieldNameTuple!Type) {
autowireMember!(name, index, Type)(container, instance);
}
foreach(index, name; FieldNameTuple!Type) {
autowireMember!(name, index, Type)(container, instance);
}
}
private void printDebugAutowiringCandidate(TypeInfo candidateInstanceType, void* candidateInstanceAddress, TypeInfo instanceType, void* instanceAddress, string member) {
writeln(format("DEBUG: Autowired instance [%s@%s] to [%s@%s].%s", candidateInstanceType, candidateInstanceAddress, instanceType, instanceAddress, member));
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) {
writeln(format("DEBUG: Autowired all registered instances of super type %s to [%s@%s].%s", superTypeInfo, instanceType, instanceAddress, member));
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) {
foreach(attribute; __traits(getAttributes, Type.tupleof[memberIndex])) {
static if (is(attribute == Autowire!T, T)) {
injectInstance!(member, memberIndex, typeof(attribute.qualifier))(container, instance);
} else static if (__traits(isSame, attribute, Autowire)) {
injectInstance!(member, memberIndex, UseMemberType)(container, instance);
} else static if (is(typeof(attribute) == Value)) {
enum key = attribute.key;
injectValue!(member, memberIndex, key, false)(container, instance);
} else static if (is(typeof(attribute) == MandatoryValue)) {
enum key = attribute.key;
injectValue!(member, memberIndex, key, true)(container, instance);
}
}
foreach(attribute; __traits(getAttributes, Type.tupleof[memberIndex])) {
static if (is(attribute == Autowire!T, T)) {
injectInstance!(member, memberIndex, typeof(attribute.qualifier))(container, instance);
} else static if (__traits(isSame, attribute, Autowire)) {
injectInstance!(member, memberIndex, UseMemberType)(container, instance);
} else static if (is(typeof(attribute) == Value)) {
enum key = attribute.key;
injectValue!(member, memberIndex, key, false)(container, instance);
} else static if (is(typeof(attribute) == MandatoryValue)) {
enum key = attribute.key;
injectValue!(member, memberIndex, key, true)(container, instance);
}
}
}
private void injectInstance(string member, size_t memberIndex, QualifierType, Type)(shared(DependencyContainer) container, Type instance) {
if (instance.tupleof[memberIndex] is null) {
alias MemberType = typeof(Type.tupleof[memberIndex]);
enum isOptional = hasUDA!(Type.tupleof[memberIndex], OptionalDependency);
if (instance.tupleof[memberIndex] is null) {
alias MemberType = typeof(Type.tupleof[memberIndex]);
enum isOptional = hasUDA!(Type.tupleof[memberIndex], OptionalDependency);
static if (isDynamicArray!MemberType) {
injectMultipleInstances!(member, memberIndex, isOptional, MemberType)(container, instance);
} else {
injectSingleInstance!(member, memberIndex, isOptional, MemberType, QualifierType)(container, instance);
}
}
static if (isDynamicArray!MemberType) {
injectMultipleInstances!(member, memberIndex, isOptional, MemberType)(container, 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) {
alias MemberElementType = ElementType!MemberType;
static if (isOptional) {
auto instances = container.resolveAll!MemberElementType(ResolveOption.noResolveException);
} else {
auto instances = container.resolveAll!MemberElementType;
}
alias MemberElementType = ElementType!MemberType;
static if (isOptional) {
auto instances = container.resolveAll!MemberElementType(ResolveOption.noResolveException);
} else {
auto instances = container.resolveAll!MemberElementType;
}
instance.tupleof[memberIndex] = instances;
debug(poodinisVerbose) {
printDebugAutowiringArray(typeid(MemberElementType), typeid(Type), &instance, member);
}
instance.tupleof[memberIndex] = instances;
debug(poodinisVerbose) {
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) {
debug(poodinisVerbose) {
TypeInfo qualifiedInstanceType = typeid(MemberType);
}
debug(poodinisVerbose) {
TypeInfo qualifiedInstanceType = typeid(MemberType);
}
enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance);
enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance);
MemberType qualifiedInstance;
static if (!is(QualifierType == UseMemberType)) {
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance, isOptional)(container);
debug(poodinisVerbose) {
qualifiedInstanceType = typeid(QualifierType);
}
} else {
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance, isOptional)(container);
}
MemberType qualifiedInstance;
static if (!is(QualifierType == UseMemberType)) {
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance, isOptional)(container);
debug(poodinisVerbose) {
qualifiedInstanceType = typeid(QualifierType);
}
} else {
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance, isOptional)(container);
}
instance.tupleof[memberIndex] = qualifiedInstance;
instance.tupleof[memberIndex] = qualifiedInstance;
debug(poodinisVerbose) {
printDebugAutowiringCandidate(qualifiedInstanceType, &qualifiedInstance, typeid(Type), &instance, member);
}
debug(poodinisVerbose) {
printDebugAutowiringCandidate(qualifiedInstanceType, &qualifiedInstance, typeid(Type), &instance, member);
}
}
private QualifierType createOrResolveInstance(MemberType, QualifierType, bool createNew, bool isOptional)(shared(DependencyContainer) container) {
static if (createNew) {
auto instanceFactory = new InstanceFactory();
instanceFactory.factoryParameters = InstanceFactoryParameters(typeid(MemberType), CreatesSingleton.no);
return cast(MemberType) instanceFactory.getInstance();
} else {
static if (isOptional) {
return container.resolve!(MemberType, QualifierType)(ResolveOption.noResolveException);
} else {
return container.resolve!(MemberType, QualifierType);
}
}
static if (createNew) {
auto instanceFactory = new InstanceFactory();
instanceFactory.factoryParameters = InstanceFactoryParameters(typeid(MemberType), CreatesSingleton.no);
return cast(MemberType) instanceFactory.getInstance();
} else {
static if (isOptional) {
return container.resolve!(MemberType, QualifierType)(ResolveOption.noResolveException);
} else {
return container.resolve!(MemberType, QualifierType);
}
}
}
private void injectValue(string member, size_t memberIndex, string key, bool mandatory, Type)(shared(DependencyContainer) container, Type instance) {
alias MemberType = typeof(Type.tupleof[memberIndex]);
try {
auto injector = container.resolve!(ValueInjector!MemberType);
instance.tupleof[memberIndex] = injector.get(key);
debug(poodinisVerbose) {
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 (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);
}
}
alias MemberType = typeof(Type.tupleof[memberIndex]);
try {
auto injector = container.resolve!(ValueInjector!MemberType);
instance.tupleof[memberIndex] = injector.get(key);
debug(poodinisVerbose) {
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 (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) {
writeln(format("DEBUG: Injected value with key '%s' of type %s into [%s@%s].%s", key, valueType, instanceType, instanceAddress, member));
writeln(format("DEBUG: Injected value with key '%s' of type %s into [%s@%s].%s", key, valueType, instanceType, instanceAddress, member));
}
/**
@ -237,48 +237,48 @@ private void printDebugValueInjection(TypeInfo instanceType, void* instanceAddre
* Deprecated: Using the global container is undesired. See DependencyContainer.getInstance().
*/
public deprecated void globalAutowire(Type)(Type instance) {
DependencyContainer.getInstance().autowire(instance);
DependencyContainer.getInstance().autowire(instance);
}
class AutowiredRegistration(RegistrationType : Object) : Registration {
private shared(DependencyContainer) container;
private shared(DependencyContainer) container;
public this(TypeInfo registeredType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) {
super(registeredType, typeid(RegistrationType), instanceFactory, originatingContainer);
}
public this(TypeInfo registeredType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) {
super(registeredType, typeid(RegistrationType), instanceFactory, originatingContainer);
}
public override Object getInstance(InstantiationContext context = new AutowireInstantiationContext()) {
enforce(!(originatingContainer is null), "The registration's originating container is null. There is no way to resolve autowire dependencies.");
public override Object getInstance(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;
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) {
originatingContainer.autowire(instance);
}
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!");
if (autowireContext.autowireInstance) {
originatingContainer.autowire(instance);
}
this.preDestructor = getPreDestructor(instance);
this.preDestructor = getPreDestructor(instance);
return instance;
}
return instance;
}
private void delegate() getPreDestructor(RegistrationType instance) {
void delegate() preDestructor = null;
foreach (memberName; __traits(allMembers, RegistrationType)) {
mixin(createImportsString!RegistrationType);
static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName)))
&& __traits(getProtection, __traits(getMember, instance, memberName)) == "public"
&& isFunction!(mixin(fullyQualifiedName!RegistrationType ~ `.` ~ memberName))
&& hasUDA!(__traits(getMember, instance, memberName), PreDestroy)) {
preDestructor = &__traits(getMember, instance, memberName);
}
}
private void delegate() getPreDestructor(RegistrationType instance) {
void delegate() preDestructor = null;
foreach (memberName; __traits(allMembers, RegistrationType)) {
mixin(createImportsString!RegistrationType);
static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName)))
&& __traits(getProtection, __traits(getMember, instance, memberName)) == "public"
&& isFunction!(mixin(fullyQualifiedName!RegistrationType ~ `.` ~ memberName))
&& hasUDA!(__traits(getMember, instance, memberName), PreDestroy)) {
preDestructor = &__traits(getMember, instance, memberName);
}
}
return preDestructor;
}
return preDestructor;
}
}
class AutowireInstantiationContext : InstantiationContext {
public bool autowireInstance = true;
public bool autowireInstance = true;
}

View file

@ -5,7 +5,7 @@
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2017 Mike Bierlee
* Copyright: 2014-2018 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
@ -27,60 +27,60 @@ import std.concurrency;
import std.traits;
debug {
import std.stdio;
import std.stdio;
}
/**
* Exception thrown when errors occur while resolving a type in a dependency container.
*/
class ResolveException : Exception {
this(string message, TypeInfo resolveType) {
super(format("Exception while resolving type %s: %s", resolveType.toString(), message));
}
this(string message, TypeInfo resolveType) {
super(format("Exception while resolving type %s: %s", resolveType.toString(), message));
}
this(Throwable cause, TypeInfo resolveType) {
super(format("Exception while resolving type %s", resolveType.toString()), cause);
}
this(Throwable cause, TypeInfo resolveType) {
super(format("Exception while resolving type %s", resolveType.toString()), cause);
}
}
/**
* Exception thrown when errors occur while registering a type in a dependency container.
*/
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
*/
public enum RegistrationOption {
none = 0,
/**
* Prevent a concrete type being registered on itself. With this option you will always need
* to use the supertype as the type of the dependency.
*/
doNotAddConcreteTypeRegistration = 1 << 0,
none = 0,
/**
* Prevent a concrete type being registered on itself. With this option you will always need
* to use the supertype as the type of the dependency.
*/
doNotAddConcreteTypeRegistration = 1 << 0,
}
/**
* Options which influence the process of resolving dependencies
*/
public enum ResolveOption {
none = 0,
/**
* Registers the type you're trying to resolve before returning it.
* This essentially makes registration optional for resolving by concerete types.
* Resolinvg will still fail when trying to resolve a dependency by supertype.
*/
registerBeforeResolving = 1 << 0,
none = 0,
/**
* Registers the type you're trying to resolve before returning it.
* This essentially makes registration optional for resolving by concerete types.
* Resolinvg will still fail when trying to resolve a dependency by supertype.
*/
registerBeforeResolving = 1 << 0,
/**
* Does not throw a resolve exception when a type is not registered but will
* return null instead. If the type is an array, an empty array is returned instead.
*/
noResolveException = 1 << 1
/**
* Does not throw a resolve exception when a type is not registered but will
* return null instead. If the type is an array, an empty array is returned instead.
*/
noResolveException = 1 << 1
}
/**
@ -112,358 +112,358 @@ struct PreDestroy {}
* You can still create new instances of this class for exceptional situations.
*/
synchronized class DependencyContainer {
private Registration[][TypeInfo] registrations;
private Registration[][TypeInfo] registrations;
private Registration[] autowireStack;
private Registration[] autowireStack;
private RegistrationOption persistentRegistrationOptions;
private ResolveOption persistentResolveOptions;
private RegistrationOption persistentRegistrationOptions;
private ResolveOption persistentResolveOptions;
~this() {
clearAllRegistrations();
}
~this() {
clearAllRegistrations();
}
/**
* Register a dependency by concrete class type.
*
* A dependency registered by concrete class type can only be resolved by concrete class type.
* No qualifiers can be used when resolving dependencies which are registered by concrete type.
*
* The default registration scope is "single instance" scope.
*
* Returns:
* A registration is returned which can be used to change the registration scope.
*
* Examples:
* Register and resolve a class by concrete type:
* ---
* class Cat : Animal { ... }
* container.register!Cat;
* ---
*
* See_Also: singleInstance, newInstance, existingInstance
*/
public Registration register(ConcreteType)(RegistrationOption options = RegistrationOption.none) {
return register!(ConcreteType, ConcreteType)(options);
}
/**
* Register a dependency by concrete class type.
*
* A dependency registered by concrete class type can only be resolved by concrete class type.
* No qualifiers can be used when resolving dependencies which are registered by concrete type.
*
* The default registration scope is "single instance" scope.
*
* Returns:
* A registration is returned which can be used to change the registration scope.
*
* Examples:
* Register and resolve a class by concrete type:
* ---
* class Cat : Animal { ... }
* container.register!Cat;
* ---
*
* See_Also: singleInstance, newInstance, existingInstance
*/
public Registration register(ConcreteType)(RegistrationOption options = RegistrationOption.none) {
return register!(ConcreteType, ConcreteType)(options);
}
/**
* Register a dependency by super type.
*
* A dependency registered by super type can only be resolved by super type. A qualifier is typically
* used to resolve dependencies registered by super type.
*
* The default registration scope is "single instance" scope.
*
* Examples:
* Register and resolve by super type
* ---
* class Cat : Animal { ... }
* container.register!(Animal, Cat);
* ---
*
* See_Also: singleInstance, newInstance, existingInstance, RegistrationOption
*/
public Registration register(SuperType, ConcreteType : SuperType)(RegistrationOption options = RegistrationOption.none) {
TypeInfo registeredType = typeid(SuperType);
TypeInfo_Class concreteType = typeid(ConcreteType);
/**
* Register a dependency by super type.
*
* A dependency registered by super type can only be resolved by super type. A qualifier is typically
* used to resolve dependencies registered by super type.
*
* The default registration scope is "single instance" scope.
*
* Examples:
* Register and resolve by super type
* ---
* class Cat : Animal { ... }
* container.register!(Animal, Cat);
* ---
*
* See_Also: singleInstance, newInstance, existingInstance, RegistrationOption
*/
public Registration register(SuperType, ConcreteType : SuperType)(RegistrationOption options = RegistrationOption.none) {
TypeInfo registeredType = typeid(SuperType);
TypeInfo_Class concreteType = typeid(ConcreteType);
debug(poodinisVerbose) {
writeln(format("DEBUG: Register type %s (as %s)", concreteType.toString(), registeredType.toString()));
}
debug(poodinisVerbose) {
writeln(format("DEBUG: Register type %s (as %s)", concreteType.toString(), registeredType.toString()));
}
auto existingRegistration = getExistingRegistration(registeredType, concreteType);
if (existingRegistration) {
return existingRegistration;
}
auto existingRegistration = getExistingRegistration(registeredType, concreteType);
if (existingRegistration) {
return existingRegistration;
}
auto instanceFactory = new ConstructorInjectingInstanceFactory!ConcreteType(this);
auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, instanceFactory, this);
newRegistration.singleInstance();
auto instanceFactory = new ConstructorInjectingInstanceFactory!ConcreteType(this);
auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, instanceFactory, this);
newRegistration.singleInstance();
static if (!is(SuperType == ConcreteType)) {
if (!hasOption(options, persistentRegistrationOptions, RegistrationOption.doNotAddConcreteTypeRegistration)) {
auto concreteTypeRegistration = register!ConcreteType;
concreteTypeRegistration.linkTo(newRegistration);
}
}
static if (!is(SuperType == ConcreteType)) {
if (!hasOption(options, persistentRegistrationOptions, RegistrationOption.doNotAddConcreteTypeRegistration)) {
auto concreteTypeRegistration = register!ConcreteType;
concreteTypeRegistration.linkTo(newRegistration);
}
}
registrations[registeredType] ~= cast(shared(Registration)) newRegistration;
return newRegistration;
}
registrations[registeredType] ~= cast(shared(Registration)) newRegistration;
return newRegistration;
}
private bool hasOption(OptionType)(OptionType options, OptionType persistentOptions, OptionType option) {
return ((options | persistentOptions) & option) != 0;
}
private bool hasOption(OptionType)(OptionType options, OptionType persistentOptions, OptionType option) {
return ((options | persistentOptions) & option) != 0;
}
private OptionType buildFlags(OptionType)(OptionType[] options) {
OptionType flags;
foreach (option; options) {
flags |= option;
}
return flags;
}
private OptionType buildFlags(OptionType)(OptionType[] options) {
OptionType flags;
foreach (option; options) {
flags |= option;
}
return flags;
}
private Registration getExistingRegistration(TypeInfo registrationType, TypeInfo qualifierType) {
auto existingCandidates = registrationType in registrations;
if (existingCandidates) {
return getRegistration(cast(Registration[]) *existingCandidates, qualifierType);
}
private Registration getExistingRegistration(TypeInfo registrationType, TypeInfo qualifierType) {
auto existingCandidates = registrationType in registrations;
if (existingCandidates) {
return getRegistration(cast(Registration[]) *existingCandidates, qualifierType);
}
return null;
}
return null;
}
private Registration getRegistration(Registration[] candidates, TypeInfo concreteType) {
foreach(existingRegistration ; candidates) {
if (existingRegistration.instanceType == concreteType) {
return existingRegistration;
}
}
private Registration getRegistration(Registration[] candidates, TypeInfo concreteType) {
foreach(existingRegistration ; candidates) {
if (existingRegistration.instanceType == concreteType) {
return existingRegistration;
}
}
return null;
}
return null;
}
/**
* Resolve dependencies.
*
* Dependencies can only resolved using this method if they are registered by concrete type or the only
* concrete type registered by super type.
*
* Resolved dependencies are automatically autowired before being returned.
*
* Returns:
* An instance is returned which is created according to the registration scope with which they are registered.
*
* Throws:
* ResolveException when type is not registered.
*
* Examples:
* Resolve dependencies registered by super type and concrete type:
* ---
* class Cat : Animal { ... }
* class Dog : Animal { ... }
*
* container.register!(Animal, Cat);
* container.register!Dog;
*
* container.resolve!Animal;
* container.resolve!Dog;
* ---
* You cannot resolve a dependency when it is registered by multiple super types:
* ---
* class Cat : Animal { ... }
* class Dog : Animal { ... }
*
* container.register!(Animal, Cat);
* container.register!(Animal, Dog);
*
* container.resolve!Animal; // Error: multiple candidates for type "Animal"
* container.resolve!Dog; // Error: No type is registered by concrete type "Dog", only by super type "Animal"
* ---
* You need to use the resolve method which allows you to specify a qualifier.
*/
public RegistrationType resolve(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) {
return resolve!(RegistrationType, RegistrationType)(resolveOptions);
}
/**
* Resolve dependencies.
*
* Dependencies can only resolved using this method if they are registered by concrete type or the only
* concrete type registered by super type.
*
* Resolved dependencies are automatically autowired before being returned.
*
* Returns:
* An instance is returned which is created according to the registration scope with which they are registered.
*
* Throws:
* ResolveException when type is not registered.
*
* Examples:
* Resolve dependencies registered by super type and concrete type:
* ---
* class Cat : Animal { ... }
* class Dog : Animal { ... }
*
* container.register!(Animal, Cat);
* container.register!Dog;
*
* container.resolve!Animal;
* container.resolve!Dog;
* ---
* You cannot resolve a dependency when it is registered by multiple super types:
* ---
* class Cat : Animal { ... }
* class Dog : Animal { ... }
*
* container.register!(Animal, Cat);
* container.register!(Animal, Dog);
*
* container.resolve!Animal; // Error: multiple candidates for type "Animal"
* container.resolve!Dog; // Error: No type is registered by concrete type "Dog", only by super type "Animal"
* ---
* You need to use the resolve method which allows you to specify a qualifier.
*/
public RegistrationType resolve(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) {
return resolve!(RegistrationType, RegistrationType)(resolveOptions);
}
/**
* Resolve dependencies using a qualifier.
*
* Dependencies can only resolved using this method if they are registered by super type.
*
* Resolved dependencies are automatically autowired before being returned.
*
* Returns:
* An instance is returned which is created according to the registration scope with which they are registered.
*
* Throws:
* ResolveException when type is not registered or there are multiple candidates available for type.
*
* Examples:
* Resolve dependencies registered by super type:
* ---
* class Cat : Animal { ... }
* class Dog : Animal { ... }
*
* container.register!(Animal, Cat);
* container.register!(Animal, Dog);
*
* container.resolve!(Animal, Cat);
* container.resolve!(Animal, Dog);
* ---
*/
public QualifierType resolve(RegistrationType, QualifierType : RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) {
TypeInfo resolveType = typeid(RegistrationType);
TypeInfo qualifierType = typeid(QualifierType);
/**
* Resolve dependencies using a qualifier.
*
* Dependencies can only resolved using this method if they are registered by super type.
*
* Resolved dependencies are automatically autowired before being returned.
*
* Returns:
* An instance is returned which is created according to the registration scope with which they are registered.
*
* Throws:
* ResolveException when type is not registered or there are multiple candidates available for type.
*
* Examples:
* Resolve dependencies registered by super type:
* ---
* class Cat : Animal { ... }
* class Dog : Animal { ... }
*
* container.register!(Animal, Cat);
* container.register!(Animal, Dog);
*
* container.resolve!(Animal, Cat);
* container.resolve!(Animal, Dog);
* ---
*/
public QualifierType resolve(RegistrationType, QualifierType : RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) {
TypeInfo resolveType = typeid(RegistrationType);
TypeInfo qualifierType = typeid(QualifierType);
debug(poodinisVerbose) {
writeln("DEBUG: Resolving type " ~ resolveType.toString() ~ " with qualifier " ~ qualifierType.toString());
}
debug(poodinisVerbose) {
writeln("DEBUG: Resolving type " ~ resolveType.toString() ~ " with qualifier " ~ qualifierType.toString());
}
static if (__traits(compiles, new QualifierType())) {
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.registerBeforeResolving)) {
register!(RegistrationType, QualifierType)();
}
}
static if (__traits(compiles, new QualifierType())) {
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.registerBeforeResolving)) {
register!(RegistrationType, QualifierType)();
}
}
auto candidates = resolveType in registrations;
if (!candidates) {
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) {
return null;
}
auto candidates = resolveType in registrations;
if (!candidates) {
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) {
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 {
QualifierType newInstance = resolveAutowiredInstance!QualifierType(registration);
callPostConstructors(newInstance);
return newInstance;
} catch (ValueInjectionException e) {
throw new ResolveException(e, resolveType);
}
}
try {
QualifierType newInstance = resolveAutowiredInstance!QualifierType(registration);
callPostConstructors(newInstance);
return newInstance;
} catch (ValueInjectionException e) {
throw new ResolveException(e, resolveType);
}
}
private QualifierType resolveAutowiredInstance(QualifierType)(Registration registration) {
QualifierType instance;
if (!(cast(Registration[]) autowireStack).canFind(registration)) {
autowireStack ~= cast(shared(Registration)) registration;
instance = cast(QualifierType) registration.getInstance(new AutowireInstantiationContext());
autowireStack = autowireStack[0 .. $-1];
} else {
auto autowireContext = new AutowireInstantiationContext();
autowireContext.autowireInstance = false;
instance = cast(QualifierType) registration.getInstance(autowireContext);
}
return instance;
}
private QualifierType resolveAutowiredInstance(QualifierType)(Registration registration) {
QualifierType instance;
if (!(cast(Registration[]) autowireStack).canFind(registration)) {
autowireStack ~= cast(shared(Registration)) registration;
instance = cast(QualifierType) registration.getInstance(new AutowireInstantiationContext());
autowireStack = autowireStack[0 .. $-1];
} else {
auto autowireContext = new AutowireInstantiationContext();
autowireContext.autowireInstance = false;
instance = cast(QualifierType) registration.getInstance(autowireContext);
}
return instance;
}
/**
* Resolve all dependencies registered to a super type.
*
* Returns:
* An array of autowired instances is returned. The order is undetermined.
*
* Examples:
* ---
* class Cat : Animal { ... }
* class Dog : Animal { ... }
*
* container.register!(Animal, Cat);
* container.register!(Animal, Dog);
*
* Animal[] animals = container.resolveAll!Animal;
* ---
*/
public RegistrationType[] resolveAll(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) {
RegistrationType[] instances;
TypeInfo resolveType = typeid(RegistrationType);
/**
* Resolve all dependencies registered to a super type.
*
* Returns:
* An array of autowired instances is returned. The order is undetermined.
*
* Examples:
* ---
* class Cat : Animal { ... }
* class Dog : Animal { ... }
*
* container.register!(Animal, Cat);
* container.register!(Animal, Dog);
*
* Animal[] animals = container.resolveAll!Animal;
* ---
*/
public RegistrationType[] resolveAll(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) {
RegistrationType[] instances;
TypeInfo resolveType = typeid(RegistrationType);
auto qualifiedRegistrations = resolveType in registrations;
if (!qualifiedRegistrations) {
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) {
return [];
}
auto qualifiedRegistrations = resolveType in registrations;
if (!qualifiedRegistrations) {
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) {
return [];
}
throw new ResolveException("Type not registered.", resolveType);
}
throw new ResolveException("Type not registered.", resolveType);
}
foreach(registration ; cast(Registration[]) *qualifiedRegistrations) {
instances ~= resolveAutowiredInstance!RegistrationType(registration);
}
foreach(registration ; cast(Registration[]) *qualifiedRegistrations) {
instances ~= resolveAutowiredInstance!RegistrationType(registration);
}
return instances;
}
return instances;
}
private Registration getQualifiedRegistration(TypeInfo resolveType, TypeInfo qualifierType, Registration[] candidates) {
if (resolveType == qualifierType) {
if (candidates.length > 1) {
string candidateList = candidates.toConcreteTypeListString();
throw new ResolveException("Multiple qualified candidates available: " ~ candidateList ~ ". Please use a qualifier.", resolveType);
}
private Registration getQualifiedRegistration(TypeInfo resolveType, TypeInfo qualifierType, Registration[] candidates) {
if (resolveType == qualifierType) {
if (candidates.length > 1) {
string candidateList = candidates.toConcreteTypeListString();
throw new ResolveException("Multiple qualified candidates available: " ~ candidateList ~ ". Please use a qualifier.", resolveType);
}
return candidates[0];
}
return candidates[0];
}
return getRegistration(candidates, qualifierType);
}
return getRegistration(candidates, qualifierType);
}
private void callPostConstructors(Type)(Type instance) {
foreach (memberName; __traits(allMembers, Type)) {
mixin(createImportsString!Type);
private void callPostConstructors(Type)(Type instance) {
foreach (memberName; __traits(allMembers, Type)) {
mixin(createImportsString!Type);
static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName)))
&& __traits(getProtection, __traits(getMember, instance, memberName)) == "public"
&& isFunction!(mixin(fullyQualifiedName!Type ~ `.` ~ memberName))
&& hasUDA!(__traits(getMember, instance, memberName), PostConstruct)) {
__traits(getMember, instance, memberName)();
}
}
}
static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName)))
&& __traits(getProtection, __traits(getMember, instance, memberName)) == "public"
&& isFunction!(mixin(fullyQualifiedName!Type ~ `.` ~ memberName))
&& hasUDA!(__traits(getMember, instance, memberName), PostConstruct)) {
__traits(getMember, instance, memberName)();
}
}
}
/**
* Clears all dependency registrations managed by this container.
*/
public void clearAllRegistrations() {
foreach(registrationsOfType; registrations) {
callPreDestructorsOfRegistrations(registrationsOfType);
}
registrations.destroy();
}
/**
* Clears all dependency registrations managed by this container.
*/
public void clearAllRegistrations() {
foreach(registrationsOfType; registrations) {
callPreDestructorsOfRegistrations(registrationsOfType);
}
registrations.destroy();
}
/**
* Removes a registered dependency by type.
*
* A dependency can be removed either by super type or concrete type, depending on how they are registered.
*
* Examples:
* ---
* container.removeRegistration!Animal;
* ---
*/
public void removeRegistration(RegistrationType)() {
auto registrationsOfType = *(typeid(RegistrationType) in registrations);
callPreDestructorsOfRegistrations(registrationsOfType);
registrations.remove(typeid(RegistrationType));
}
/**
* Removes a registered dependency by type.
*
* A dependency can be removed either by super type or concrete type, depending on how they are registered.
*
* Examples:
* ---
* container.removeRegistration!Animal;
* ---
*/
public void removeRegistration(RegistrationType)() {
auto registrationsOfType = *(typeid(RegistrationType) in registrations);
callPreDestructorsOfRegistrations(registrationsOfType);
registrations.remove(typeid(RegistrationType));
}
private void callPreDestructorsOfRegistrations(shared(Registration[]) registrations) {
foreach(registration; registrations) {
Registration unsharedRegistration = cast(Registration) registration;
if (unsharedRegistration.preDestructor !is null) {
unsharedRegistration.preDestructor()();
}
}
}
private void callPreDestructorsOfRegistrations(shared(Registration[]) registrations) {
foreach(registration; registrations) {
Registration unsharedRegistration = cast(Registration) registration;
if (unsharedRegistration.preDestructor !is null) {
unsharedRegistration.preDestructor()();
}
}
}
/**
* Apply persistent registration options which will be used everytime register() is called.
*/
public void setPersistentRegistrationOptions(RegistrationOption options) {
persistentRegistrationOptions = options;
}
/**
* Apply persistent registration options which will be used everytime register() is called.
*/
public void setPersistentRegistrationOptions(RegistrationOption options) {
persistentRegistrationOptions = options;
}
/**
* Unsets all applied persistent registration options
*/
public void unsetPersistentRegistrationOptions() {
persistentRegistrationOptions = RegistrationOption.none;
}
/**
* Unsets all applied persistent registration options
*/
public void unsetPersistentRegistrationOptions() {
persistentRegistrationOptions = RegistrationOption.none;
}
/**
* Apply persistent resolve options which will be used everytime resolve() is called.
*/
public void setPersistentResolveOptions(ResolveOption options) {
persistentResolveOptions = options;
}
/**
* Apply persistent resolve options which will be used everytime resolve() is called.
*/
public void setPersistentResolveOptions(ResolveOption options) {
persistentResolveOptions = options;
}
/**
* Unsets all applied persistent resolve options
*/
public void unsetPersistentResolveOptions() {
persistentResolveOptions = ResolveOption.none;
}
/**
* Unsets all applied persistent resolve options
*/
public void unsetPersistentResolveOptions() {
persistentResolveOptions = ResolveOption.none;
}
}

View file

@ -5,7 +5,7 @@
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2017 Mike Bierlee
* Copyright: 2014-2018 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
@ -21,7 +21,7 @@ import poodinis.autowire;
import std.traits;
class ApplicationContext {
public void registerDependencies(shared(DependencyContainer) container) {}
public void registerDependencies(shared(DependencyContainer) container) {}
}
/**
@ -35,7 +35,7 @@ struct Component {}
* enables you to use type-qualified alternatives for dependencies.
*/
struct RegisterByType(Type) {
Type type;
Type type;
}
/**
@ -52,33 +52,33 @@ struct Prototype {}
* want to use annotations to set-up dependencies in your classes.
*/
public void registerContext(Context : ApplicationContext)(shared(DependencyContainer) container) {
auto context = new Context();
context.registerDependencies(container);
context.registerContextComponents(container);
container.register!(ApplicationContext, Context)().existingInstance(context);
autowire(container, context);
auto context = new Context();
context.registerDependencies(container);
context.registerContextComponents(container);
container.register!(ApplicationContext, Context)().existingInstance(context);
autowire(container, context);
}
public void registerContextComponents(ApplicationContextType : ApplicationContext)(ApplicationContextType context, shared(DependencyContainer) container) {
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);
Registration registration = null;
auto createsSingleton = CreatesSingleton.yes;
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);
Registration registration = null;
auto createsSingleton = CreatesSingleton.yes;
foreach(attribute; __traits(getAttributes, __traits(getMember, context, member))) {
static if (is(attribute == RegisterByType!T, T)) {
registration = container.register!(typeof(attribute.type), ReturnType!factoryMethod);
} else static if (__traits(isSame, attribute, Prototype)) {
createsSingleton = CreatesSingleton.no;
}
}
foreach(attribute; __traits(getAttributes, __traits(getMember, context, member))) {
static if (is(attribute == RegisterByType!T, T)) {
registration = container.register!(typeof(attribute.type), ReturnType!factoryMethod);
} else static if (__traits(isSame, attribute, Prototype)) {
createsSingleton = CreatesSingleton.no;
}
}
if (registration is null) {
registration = container.register!(ReturnType!factoryMethod);
}
if (registration is null) {
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

@ -3,7 +3,7 @@
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2017 Mike Bierlee
* Copyright: 2014-2018 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
@ -24,147 +24,147 @@ alias CreatesSingleton = Flag!"CreatesSingleton";
alias InstanceFactoryMethod = Object delegate();
class InstanceCreationException : Exception {
this(string message, string file = __FILE__, size_t line = __LINE__) {
super(message, file, line);
}
this(string message, string file = __FILE__, size_t line = __LINE__) {
super(message, file, line);
}
}
struct InstanceFactoryParameters {
TypeInfo_Class instanceType;
CreatesSingleton createsSingleton = CreatesSingleton.yes;
Object existingInstance;
InstanceFactoryMethod factoryMethod;
TypeInfo_Class instanceType;
CreatesSingleton createsSingleton = CreatesSingleton.yes;
Object existingInstance;
InstanceFactoryMethod factoryMethod;
}
class InstanceFactory {
private Object instance = null;
private InstanceFactoryParameters _factoryParameters;
private Object instance = null;
private InstanceFactoryParameters _factoryParameters;
this() {
factoryParameters = InstanceFactoryParameters();
}
this() {
factoryParameters = InstanceFactoryParameters();
}
public @property void factoryParameters(InstanceFactoryParameters factoryParameters) {
if (factoryParameters.factoryMethod is null) {
factoryParameters.factoryMethod = &this.createInstance;
}
public @property void factoryParameters(InstanceFactoryParameters factoryParameters) {
if (factoryParameters.factoryMethod is null) {
factoryParameters.factoryMethod = &this.createInstance;
}
if (factoryParameters.existingInstance !is null) {
factoryParameters.createsSingleton = CreatesSingleton.yes;
this.instance = factoryParameters.existingInstance;
}
if (factoryParameters.existingInstance !is null) {
factoryParameters.createsSingleton = CreatesSingleton.yes;
this.instance = factoryParameters.existingInstance;
}
_factoryParameters = factoryParameters;
}
_factoryParameters = factoryParameters;
}
public @property InstanceFactoryParameters factoryParameters() {
return _factoryParameters;
}
public @property InstanceFactoryParameters factoryParameters() {
return _factoryParameters;
}
public Object getInstance() {
if (_factoryParameters.createsSingleton && instance !is null) {
debug(poodinisVerbose) {
printDebugUseExistingInstance();
}
public Object getInstance() {
if (_factoryParameters.createsSingleton && instance !is null) {
debug(poodinisVerbose) {
printDebugUseExistingInstance();
}
return instance;
}
return instance;
}
debug(poodinisVerbose) {
printDebugCreateNewInstance();
}
debug(poodinisVerbose) {
printDebugCreateNewInstance();
}
instance = _factoryParameters.factoryMethod();
return instance;
}
instance = _factoryParameters.factoryMethod();
return instance;
}
private void printDebugUseExistingInstance() {
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");
}
}
private void printDebugUseExistingInstance() {
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");
}
}
private void printDebugCreateNewInstance() {
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");
}
}
private void printDebugCreateNewInstance() {
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");
}
}
protected Object createInstance() {
enforce!InstanceCreationException(_factoryParameters.instanceType, "Instance type is not defined, cannot create instance without knowing its type.");
return _factoryParameters.instanceType.create();
}
protected Object createInstance() {
enforce!InstanceCreationException(_factoryParameters.instanceType, "Instance type is not defined, cannot create instance without knowing its type.");
return _factoryParameters.instanceType.create();
}
}
class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory {
private shared DependencyContainer container;
private bool isBeingInjected = false;
private shared DependencyContainer container;
private bool isBeingInjected = false;
this(shared DependencyContainer container) {
this.container = container;
}
this(shared DependencyContainer container) {
this.container = container;
}
private static string createArgumentList(Params...)() {
string argumentList = "";
foreach(param; Params) {
if (argumentList.length > 0) {
argumentList ~= ",";
}
private static string createArgumentList(Params...)() {
string argumentList = "";
foreach(param; Params) {
if (argumentList.length > 0) {
argumentList ~= ",";
}
argumentList ~= "container.resolve!" ~ param.stringof;
}
return argumentList;
}
argumentList ~= "container.resolve!" ~ param.stringof;
}
return argumentList;
}
private static string createImportList(Params...)() {
string importList = "";
foreach(param; Params) {
importList ~= createImportsString!param;
}
return importList;
}
private static string createImportList(Params...)() {
string importList = "";
foreach(param; Params) {
importList ~= createImportsString!param;
}
return importList;
}
private static bool parametersAreValid(Params...)() {
bool isValid = true;
foreach(param; Params) {
if (isBuiltinType!param) {
isValid = false;
break;
}
}
private static bool parametersAreValid(Params...)() {
bool isValid = true;
foreach(param; Params) {
if (isBuiltinType!param) {
isValid = false;
break;
}
}
return isValid;
}
return isValid;
}
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));
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));
Object instance = null;
static if (__traits(compiles, __traits(getOverloads, InstanceType, `__ctor`))) {
foreach(ctor ; __traits(getOverloads, InstanceType, `__ctor`)) {
static if (parametersAreValid!(Parameters!ctor)) {
isBeingInjected = true;
mixin(createImportsString!InstanceType
~ createImportList!(Parameters!ctor) ~ `
instance = new ` ~ fullyQualifiedName!InstanceType ~ `(` ~ createArgumentList!(Parameters!ctor) ~ `);
`);
isBeingInjected = false;
break;
}
}
}
Object instance = null;
static if (__traits(compiles, __traits(getOverloads, InstanceType, `__ctor`))) {
foreach(ctor ; __traits(getOverloads, InstanceType, `__ctor`)) {
static if (parametersAreValid!(Parameters!ctor)) {
isBeingInjected = true;
mixin(createImportsString!InstanceType
~ createImportList!(Parameters!ctor) ~ `
instance = new ` ~ fullyQualifiedName!InstanceType ~ `(` ~ createArgumentList!(Parameters!ctor) ~ `);
`);
isBeingInjected = false;
break;
}
}
}
if (instance is null) {
instance = typeid(InstanceType).create();
}
if (instance is null) {
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

@ -1,16 +1,27 @@
/**
* This module contains instance factory facilities
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2018 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
module poodinis.imports;
import std.traits;
public static string createImportsString(Type)() {
string imports = `import ` ~ moduleName!Type ~ `;`;
static if (__traits(compiles, TemplateArgsOf!Type)) {
foreach(TemplateArgType; TemplateArgsOf!Type) {
static if (!isBuiltinType!TemplateArgType) {
imports ~= createImportsString!TemplateArgType;
}
}
}
string imports = `import ` ~ moduleName!Type ~ `;`;
static if (__traits(compiles, TemplateArgsOf!Type)) {
foreach(TemplateArgType; TemplateArgsOf!Type) {
static if (!isBuiltinType!TemplateArgType) {
imports ~= createImportsString!TemplateArgType;
}
}
}
return imports;
return imports;
}

View file

@ -3,7 +3,7 @@
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2017 Mike Bierlee
* Copyright: 2014-2018 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.

View file

@ -19,7 +19,7 @@
* Kenji Hara,
* Shoichi Kato,
* Mike Bierlee (m.bierlee@lostmoment.com)
* Copyright: Copyright Digital Mars 2005 - 2009., Copyright Andrei Alexandrescu 2008-, Jonathan M Davis 2011-., 2014-2017 Mike Bierlee
* Copyright: Copyright Digital Mars 2005 - 2009., Copyright Andrei Alexandrescu 2008-, Jonathan M Davis 2011-., 2014-2018 Mike Bierlee
* License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0)
*/
@ -29,35 +29,35 @@ import std.exception;
import std.traits;
static if (!__traits(compiles, basicExceptionCtors)) {
mixin template basicExceptionCtors()
{
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @safe pure nothrow
{
super(msg, file, line, next);
}
mixin template basicExceptionCtors()
{
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @safe pure nothrow
{
super(msg, file, line, next);
}
this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @safe pure nothrow
{
super(msg, file, line, next);
}
}
this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @safe pure nothrow
{
super(msg, file, line, next);
}
}
}
static if (!__traits(compiles, isFunction)) {
template isFunction(X...) if (X.length == 1)
{
static if (is(typeof(&X[0]) U : U*) && is(U == function) ||
is(typeof(&X[0]) U == delegate))
{
// x is a (nested) function symbol.
enum isFunction = true;
}
else static if (is(X[0] T))
{
// x is a type. Take the type of it and examine.
enum isFunction = is(T == function);
}
else
enum isFunction = false;
}
template isFunction(X...) if (X.length == 1)
{
static if (is(typeof(&X[0]) U : U*) && is(U == function) ||
is(typeof(&X[0]) U == delegate))
{
// x is a (nested) function symbol.
enum isFunction = true;
}
else static if (is(X[0] T))
{
// x is a type. Take the type of it and examine.
enum isFunction = is(T == function);
}
else
enum isFunction = false;
}
}

View file

@ -5,7 +5,7 @@
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2017 Mike Bierlee
* Copyright: 2014-2018 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
@ -17,61 +17,61 @@ import poodinis.container;
import poodinis.factory;
class Registration {
private TypeInfo _registeredType = null;
private TypeInfo_Class _instanceType = null;
private Registration linkedRegistration;
private shared(DependencyContainer) _originatingContainer;
private InstanceFactory _instanceFactory;
private void delegate() _preDestructor;
private TypeInfo _registeredType = null;
private TypeInfo_Class _instanceType = null;
private Registration linkedRegistration;
private shared(DependencyContainer) _originatingContainer;
private InstanceFactory _instanceFactory;
private void delegate() _preDestructor;
public @property registeredType() {
return _registeredType;
}
public @property registeredType() {
return _registeredType;
}
public @property instanceType() {
return _instanceType;
}
public @property instanceType() {
return _instanceType;
}
public @property originatingContainer() {
return _originatingContainer;
}
public @property originatingContainer() {
return _originatingContainer;
}
public @property instanceFactory() {
return _instanceFactory;
}
public @property instanceFactory() {
return _instanceFactory;
}
public @property preDestructor() {
return _preDestructor;
}
public @property preDestructor() {
return _preDestructor;
}
protected @property preDestructor(void delegate() preDestructor) {
_preDestructor = preDestructor;
}
protected @property preDestructor(void delegate() preDestructor) {
_preDestructor = preDestructor;
}
this(TypeInfo registeredType, TypeInfo_Class instanceType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) {
this._registeredType = registeredType;
this._instanceType = instanceType;
this._originatingContainer = originatingContainer;
this._instanceFactory = instanceFactory;
}
this(TypeInfo registeredType, TypeInfo_Class instanceType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) {
this._registeredType = registeredType;
this._instanceType = instanceType;
this._originatingContainer = originatingContainer;
this._instanceFactory = instanceFactory;
}
public Object getInstance(InstantiationContext context = new InstantiationContext()) {
if (linkedRegistration !is null) {
return linkedRegistration.getInstance(context);
}
public Object getInstance(InstantiationContext context = new InstantiationContext()) {
if (linkedRegistration !is null) {
return linkedRegistration.getInstance(context);
}
if (instanceFactory is null) {
throw new InstanceCreationException("No instance factory defined for registration of type " ~ registeredType.toString());
}
if (instanceFactory is null) {
throw new InstanceCreationException("No instance factory defined for registration of type " ~ registeredType.toString());
}
return instanceFactory.getInstance();
}
return instanceFactory.getInstance();
}
public Registration linkTo(Registration registration) {
this.linkedRegistration = registration;
return this;
}
public Registration linkTo(Registration registration) {
this.linkedRegistration = registration;
return this;
}
}
/**
@ -80,35 +80,35 @@ class Registration {
* Effectively makes the given registration a singleton.
*/
public Registration singleInstance(Registration registration) {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes);
return registration;
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes);
return registration;
}
/**
* Scopes registrations to return a new instance every time the given registration is resolved.
*/
public Registration newInstance(Registration registration) {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.no);
return registration;
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.no);
return registration;
}
/**
* Scopes registrations to return the given instance every time the given registration is resolved.
*/
public Registration existingInstance(Registration registration, Object instance) {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes, instance);
return registration;
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes, instance);
return registration;
}
public string toConcreteTypeListString(Registration[] registrations) {
auto concreteTypeListString = "";
foreach (registration ; registrations) {
if (concreteTypeListString.length > 0) {
concreteTypeListString ~= ", ";
}
concreteTypeListString ~= registration.instanceType.toString();
}
return concreteTypeListString;
auto concreteTypeListString = "";
foreach (registration ; registrations) {
if (concreteTypeListString.length > 0) {
concreteTypeListString ~= ", ";
}
concreteTypeListString ~= registration.instanceType.toString();
}
return concreteTypeListString;
}
class InstantiationContext {}

View file

@ -4,7 +4,7 @@
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2014-2017 Mike Bierlee
* Copyright: 2014-2018 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
@ -19,20 +19,20 @@ import poodinis.polyfill;
* Thrown when something goes wrong during value injection.
*/
class ValueInjectionException : Exception {
mixin basicExceptionCtors;
mixin basicExceptionCtors;
}
/**
* Thrown by injectors when the value with the given key cannot be found.
*/
class ValueNotAvailableException : Exception {
this(string key) {
super(format("Value for key %s is not available", key));
}
this(string key) {
super(format("Value for key %s is not available", key));
}
this(string key, Throwable cause) {
super(format("Value for key %s is not available", key), cause);
}
this(string key, Throwable cause) {
super(format("Value for key %s is not available", key), cause);
}
}
/**
@ -53,12 +53,12 @@ class ValueNotAvailableException : Exception {
* ---
*/
struct Value {
/**
* The textual key used to find the value by injectors.
*
* The format is injector-specific.
*/
string key;
/**
* The textual key used to find the value by injectors.
*
* The format is injector-specific.
*/
string key;
}
/**
@ -79,12 +79,12 @@ struct Value {
* ---
*/
struct MandatoryValue {
/**
* The textual key used to find the value by injectors.
*
* The format is injector-specific.
*/
string key;
/**
* The textual key used to find the value by injectors.
*
* The format is injector-specific.
*/
string key;
}
/**
@ -110,14 +110,14 @@ struct MandatoryValue {
* ---
*/
interface ValueInjector(Type) {
/**
* Get a value from the injector by key.
*
* The key can have any format. Generally you are encouraged
* to accept a dot separated path, for example: server.http.port
*
* Throws: ValueNotAvailableException when the value for the given key is not available for any reason
*/
Type get(string key);
/**
* Get a value from the injector by key.
*
* The key can have any format. Generally you are encouraged
* to accept a dot separated path, for example: server.http.port
*
* Throws: ValueNotAvailableException when the value for the given key is not available for any reason
*/
Type get(string key);
}

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
@ -12,171 +12,171 @@ import std.exception;
version(unittest) {
// Test autowiring concrete type to existing instance
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
auto componentB = new ComponentB();
container.autowire(componentB);
assert(componentB !is null, "Autowirable dependency failed to autowire");
}
// Test autowiring concrete type to existing instance
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
auto componentB = new ComponentB();
container.autowire(componentB);
assert(componentB !is null, "Autowirable dependency failed to autowire");
}
// Test autowiring interface type to existing instance
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
auto componentD = new ComponentD();
container.autowire(componentD);
assert(componentD.componentC !is null, "Autowirable dependency failed to autowire");
}
// Test autowiring interface type to existing instance
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
auto componentD = new ComponentD();
container.autowire(componentD);
assert(componentD.componentC !is null, "Autowirable dependency failed to autowire");
}
// Test autowiring private members
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
auto componentD = new ComponentD();
container.autowire(componentD);
assert(componentD.privateComponentC is componentD.componentC, "Autowire private dependency failed");
}
// Test autowiring private members
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
auto componentD = new ComponentD();
container.autowire(componentD);
assert(componentD.privateComponentC is componentD.componentC, "Autowire private dependency failed");
}
// Test autowiring will only happen once
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC).newInstance();
auto componentD = new ComponentD();
container.autowire(componentD);
auto expectedComponent = componentD.componentC;
container.autowire(componentD);
auto actualComponent = componentD.componentC;
assert(expectedComponent is actualComponent, "Autowiring the second time wired a different instance");
}
// Test autowiring will only happen once
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC).newInstance();
auto componentD = new ComponentD();
container.autowire(componentD);
auto expectedComponent = componentD.componentC;
container.autowire(componentD);
auto actualComponent = componentD.componentC;
assert(expectedComponent is actualComponent, "Autowiring the second time wired a different instance");
}
// Test autowiring unregistered type
unittest {
auto container = new shared DependencyContainer();
auto componentD = new ComponentD();
assertThrown!(ResolveException)(container.autowire(componentD), "Autowiring unregistered type should throw ResolveException");
}
// Test autowiring unregistered type
unittest {
auto container = new shared DependencyContainer();
auto componentD = new ComponentD();
assertThrown!(ResolveException)(container.autowire(componentD), "Autowiring unregistered type should throw ResolveException");
}
// Test autowiring member with non-autowire attribute does not autowire
unittest {
auto container = new shared DependencyContainer();
auto componentE = new ComponentE();
container.autowire(componentE);
assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire");
}
// Test autowiring member with non-autowire attribute does not autowire
unittest {
auto container = new shared DependencyContainer();
auto componentE = new ComponentE();
container.autowire(componentE);
assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire");
}
// Test autowire class with alias declaration
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
auto componentDeclarationCocktail = new ComponentDeclarationCocktail();
// Test autowire class with alias declaration
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
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
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX);
auto componentX = container.resolve!(InterfaceA, ComponentX);
// Test autowire class with qualifier
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX);
auto componentX = container.resolve!(InterfaceA, ComponentX);
auto monkeyShine = new MonkeyShine();
container.autowire(monkeyShine);
auto monkeyShine = new MonkeyShine();
container.autowire(monkeyShine);
assert(monkeyShine.component is componentX, "Autowiring class with qualifier failed");
}
assert(monkeyShine.component is componentX, "Autowiring class with qualifier failed");
}
// Test autowire class with multiple qualifiers
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX);
auto componentC = container.resolve!(InterfaceA, ComponentC);
auto componentX = container.resolve!(InterfaceA, ComponentX);
// Test autowire class with multiple qualifiers
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX);
auto componentC = container.resolve!(InterfaceA, ComponentC);
auto componentX = container.resolve!(InterfaceA, ComponentX);
auto bootstrapBootstrap = new BootstrapBootstrap();
container.autowire(bootstrapBootstrap);
auto bootstrapBootstrap = new BootstrapBootstrap();
container.autowire(bootstrapBootstrap);
assert(bootstrapBootstrap.componentX is componentX, "Autowiring class with multiple qualifiers failed");
assert(bootstrapBootstrap.componentC is componentC, "Autowiring class with multiple qualifiers failed");
}
assert(bootstrapBootstrap.componentX is componentX, "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
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
// Test getting instance from autowired registration will autowire instance
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), new InstanceFactory(), container).singleInstance();
auto instance = cast(ComponentB) registration.getInstance(new AutowireInstantiationContext());
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), 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
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX);
// Test autowiring a dynamic array with all qualified types
unittest {
auto container = new shared DependencyContainer();
container.register!(InterfaceA, ComponentC);
container.register!(InterfaceA, ComponentX);
auto lord = new LordOfTheComponents();
container.autowire(lord);
auto lord = new LordOfTheComponents();
container.autowire(lord);
assert(lord.components.length == 2, "Dynamic array was not autowired");
}
assert(lord.components.length == 2, "Dynamic array was not autowired");
}
// Test autowiring new instance of singleinstance registration with newInstance UDA
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
// Test autowiring new instance of singleinstance registration with newInstance UDA
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
auto regularComponentA = container.resolve!ComponentA;
auto charlie = new ComponentCharlie();
auto regularComponentA = container.resolve!ComponentA;
auto charlie = new ComponentCharlie();
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
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
container.register!ComponentB;
container.register!ComponentZ;
// Test autowiring members from base class
unittest {
auto container = new shared DependencyContainer();
container.register!ComponentA;
container.register!ComponentB;
container.register!ComponentZ;
auto instance = new ComponentZ();
container.autowire(instance);
auto instance = new ComponentZ();
container.autowire(instance);
assert(instance.componentA !is null);
}
assert(instance.componentA !is null);
}
// Test autowiring optional dependencies
unittest {
auto container = new shared DependencyContainer();
auto instance = new OuttaTime();
// Test autowiring optional dependencies
unittest {
auto container = new shared DependencyContainer();
auto instance = new OuttaTime();
container.autowire(instance);
container.autowire(instance);
assert(instance.interfaceA is null);
assert(instance.componentA is null);
assert(instance.componentCs is null);
}
assert(instance.interfaceA is null);
assert(instance.componentA is null);
assert(instance.componentCs is null);
}
// Test autowiring class using value injection
unittest {
auto container = new shared DependencyContainer();
// Test autowiring class using value injection
unittest {
auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, TestInjector);
container.register!ComponentA;
auto instance = new ValuedClass();
container.register!(ValueInjector!int, TestInjector);
container.register!ComponentA;
auto instance = new ValuedClass();
container.autowire(instance);
container.autowire(instance);
assert(instance.intValue == 8);
assert(instance.unrelated !is null);
}
assert(instance.intValue == 8);
assert(instance.unrelated !is null);
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
@ -12,105 +12,105 @@ import std.exception;
version(unittest) {
//Test register component registrations from context
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
auto bananaInstance = container.resolve!Banana;
//Test register component registrations from context
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
auto bananaInstance = container.resolve!Banana;
assert(bananaInstance.color == "Yellow");
}
assert(bananaInstance.color == "Yellow");
}
//Test non-annotated methods are not registered
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
assertThrown!ResolveException(container.resolve!Apple);
}
//Test non-annotated methods are not registered
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
assertThrown!ResolveException(container.resolve!Apple);
}
//Test register component by base type
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
auto instance = container.resolve!Fruit;
assert(instance.getShape() == "Pear shaped");
}
//Test register component by base type
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
auto instance = container.resolve!Fruit;
assert(instance.getShape() == "Pear shaped");
}
//Test register components with multiple candidates
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
//Test register components with multiple candidates
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
auto rabbit = container.resolve!(Animal, Rabbit);
assert(rabbit.getYell() == "Squeeeeeel");
auto rabbit = container.resolve!(Animal, Rabbit);
assert(rabbit.getYell() == "Squeeeeeel");
auto wolf = container.resolve!(Animal, Wolf);
assert(wolf.getYell() == "Wooooooooooo");
}
auto wolf = container.resolve!(Animal, Wolf);
assert(wolf.getYell() == "Wooooooooooo");
}
//Test register component as prototype
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
//Test register component as prototype
unittest {
auto container = new shared DependencyContainer();
auto context = new TestContext();
context.registerContextComponents(container);
auto firstInstance = container.resolve!PieChart;
auto secondInstance = container.resolve!PieChart;
auto firstInstance = container.resolve!PieChart;
auto secondInstance = container.resolve!PieChart;
assert(firstInstance !is null && secondInstance !is null);
assert(firstInstance !is secondInstance);
}
assert(firstInstance !is null && secondInstance !is null);
assert(firstInstance !is secondInstance);
}
// Test setting up simple dependencies through application context
unittest {
auto container = new shared DependencyContainer();
container.registerContext!SimpleContext;
auto instance = container.resolve!CakeChart;
// Test setting up simple dependencies through application context
unittest {
auto container = new shared DependencyContainer();
container.registerContext!SimpleContext;
auto instance = container.resolve!CakeChart;
assert(instance !is null);
}
assert(instance !is null);
}
// Test resolving dependency from registered application context
unittest {
auto container = new shared DependencyContainer();
container.registerContext!SimpleContext;
auto instance = container.resolve!Apple;
// Test resolving dependency from registered application context
unittest {
auto container = new shared DependencyContainer();
container.registerContext!SimpleContext;
auto instance = container.resolve!Apple;
assert(instance !is null);
}
assert(instance !is null);
}
// Test autowiring application context
unittest {
auto container = new shared DependencyContainer();
container.register!Apple;
container.registerContext!AutowiredTestContext;
auto instance = container.resolve!ClassWrapper;
// Test autowiring application context
unittest {
auto container = new shared DependencyContainer();
container.register!Apple;
container.registerContext!AutowiredTestContext;
auto instance = container.resolve!ClassWrapper;
assert(instance !is null);
assert(instance.someClass !is null);
}
assert(instance !is null);
assert(instance.someClass !is null);
}
// Test autowiring application context with dependencies registered in same context
unittest {
auto container = new shared DependencyContainer();
container.registerContext!ComplexAutowiredTestContext;
auto instance = container.resolve!ClassWrapperWrapper;
auto wrapper = container.resolve!ClassWrapper;
auto someClass = container.resolve!Apple;
// Test autowiring application context with dependencies registered in same context
unittest {
auto container = new shared DependencyContainer();
container.registerContext!ComplexAutowiredTestContext;
auto instance = container.resolve!ClassWrapperWrapper;
auto wrapper = container.resolve!ClassWrapper;
auto someClass = container.resolve!Apple;
assert(instance !is null);
assert(instance.wrapper is wrapper);
assert(instance.wrapper.someClass is someClass);
}
assert(instance !is null);
assert(instance.wrapper is wrapper);
assert(instance.wrapper.someClass is someClass);
}
// Test resolving registered context
unittest {
auto container = new shared DependencyContainer();
container.registerContext!TestContext;
container.resolve!ApplicationContext;
}
// Test resolving registered context
unittest {
auto container = new shared DependencyContainer();
container.registerContext!TestContext;
container.resolve!ApplicationContext;
}
}

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
@ -12,137 +12,137 @@ import std.exception;
version(unittest) {
// Test instance factory with singletons
unittest {
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.yes);
auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance();
// Test instance factory with singletons
unittest {
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.yes);
auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance();
assert(instanceOne !is null, "Created factory instance is null");
assert(instanceOne is instanceTwo, "Created factory instance is not the same");
}
assert(instanceOne !is null, "Created factory instance is null");
assert(instanceOne is instanceTwo, "Created factory instance is not the same");
}
// Test instance factory with new instances
unittest {
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.no);
auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance();
// Test instance factory with new instances
unittest {
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.no);
auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance();
assert(instanceOne !is null, "Created factory instance is null");
assert(instanceOne !is instanceTwo, "Created factory instance is the same");
}
assert(instanceOne !is null, "Created factory instance is null");
assert(instanceOne !is instanceTwo, "Created factory instance is the same");
}
// Test instance factory with existing instances
unittest {
auto existingInstance = new TestImplementation();
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.yes, existingInstance);
auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance();
// Test instance factory with existing instances
unittest {
auto existingInstance = new TestImplementation();
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.yes, existingInstance);
auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance();
assert(instanceOne is existingInstance, "Created factory instance is not the existing instance");
assert(instanceTwo is existingInstance, "Created factory instance is not the existing instance when called again");
}
assert(instanceOne is existingInstance, "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"
unittest {
auto existingInstance = new TestImplementation();
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.no, existingInstance);
auto instance = factory.getInstance();
// Test instance factory with existing instances when setting singleton flag to "no"
unittest {
auto existingInstance = new TestImplementation();
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.no, existingInstance);
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
unittest {
Object factoryMethod() {
auto instance = new TestImplementation();
instance.someContent = "Ducks!";
return instance;
}
// Test creating instance using custom factory method
unittest {
Object factoryMethod() {
auto instance = new TestImplementation();
instance.someContent = "Ducks!";
return instance;
}
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(null, CreatesSingleton.yes, null, &factoryMethod);
auto instance = cast(TestImplementation) factory.getInstance();
auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(null, CreatesSingleton.yes, null, &factoryMethod);
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.someContent == "Ducks!");
}
assert(instance !is null, "No instance was created by factory or could not be cast to expected type");
assert(instance.someContent == "Ducks!");
}
// Test injecting constructor of class
unittest {
auto container = new shared DependencyContainer();
container.register!TestImplementation;
// Test injecting constructor of class
unittest {
auto container = new shared DependencyContainer();
container.register!TestImplementation;
auto factory = new ConstructorInjectingInstanceFactory!ClassWithConstructor(container);
auto instance = cast(ClassWithConstructor) factory.getInstance();
auto factory = new ConstructorInjectingInstanceFactory!ClassWithConstructor(container);
auto instance = cast(ClassWithConstructor) factory.getInstance();
assert(instance !is null);
assert(instance.testImplementation is container.resolve!TestImplementation);
}
assert(instance !is null);
assert(instance.testImplementation is container.resolve!TestImplementation);
}
// Test injecting constructor of class with multiple constructors injects the first candidate
unittest {
auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen;
container.register!TestImplementation;
// Test injecting constructor of class with multiple constructors injects the first candidate
unittest {
auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen;
container.register!TestImplementation;
auto factory = new ConstructorInjectingInstanceFactory!ClassWithMultipleConstructors(container);
auto instance = cast(ClassWithMultipleConstructors) factory.getInstance();
auto factory = new ConstructorInjectingInstanceFactory!ClassWithMultipleConstructors(container);
auto instance = cast(ClassWithMultipleConstructors) factory.getInstance();
assert(instance !is null);
assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen);
assert(instance.testImplementation is null);
}
assert(instance !is null);
assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen);
assert(instance.testImplementation is null);
}
// Test injecting constructor of class with multiple constructor parameters
unittest {
auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen;
container.register!TestImplementation;
// Test injecting constructor of class with multiple constructor parameters
unittest {
auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen;
container.register!TestImplementation;
auto factory = new ConstructorInjectingInstanceFactory!ClassWithConstructorWithMultipleParameters(container);
auto instance = cast(ClassWithConstructorWithMultipleParameters) factory.getInstance();
auto factory = new ConstructorInjectingInstanceFactory!ClassWithConstructorWithMultipleParameters(container);
auto instance = cast(ClassWithConstructorWithMultipleParameters) factory.getInstance();
assert(instance !is null);
assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen);
assert(instance.testImplementation is container.resolve!TestImplementation);
}
assert(instance !is null);
assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen);
assert(instance.testImplementation is container.resolve!TestImplementation);
}
// Test injecting constructor of class with primitive constructor parameters
unittest {
auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen;
// Test injecting constructor of class with primitive constructor parameters
unittest {
auto container = new shared DependencyContainer();
container.register!SomeOtherClassThen;
auto factory = new ConstructorInjectingInstanceFactory!ClassWithPrimitiveConstructor(container);
auto instance = cast(ClassWithPrimitiveConstructor) factory.getInstance();
auto factory = new ConstructorInjectingInstanceFactory!ClassWithPrimitiveConstructor(container);
auto instance = cast(ClassWithPrimitiveConstructor) factory.getInstance();
assert(instance !is null);
assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen);
}
assert(instance !is null);
assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen);
}
// Test injecting constructor of class with empty constructor will skip injection
unittest {
auto container = new shared DependencyContainer();
// Test injecting constructor of class with empty constructor will skip injection
unittest {
auto container = new shared DependencyContainer();
auto factory = new ConstructorInjectingInstanceFactory!ClassWithEmptyConstructor(container);
auto instance = cast(ClassWithEmptyConstructor) factory.getInstance();
auto factory = new ConstructorInjectingInstanceFactory!ClassWithEmptyConstructor(container);
auto instance = cast(ClassWithEmptyConstructor) factory.getInstance();
assert(instance !is null);
assert(instance.someOtherClassThen is null);
}
assert(instance !is null);
assert(instance.someOtherClassThen is null);
}
// Test injecting constructor of class with no candidates fails
unittest {
auto container = new shared DependencyContainer();
// Test injecting constructor of class with no candidates fails
unittest {
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

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
@ -8,5 +8,5 @@
module poodinis.test.foreignDependencies;
version(unittest) {
class Ola {}
class Ola {}
}

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
@ -12,51 +12,51 @@ import std.exception;
version(unittest) {
// Test getting instance without scope defined throws exception
unittest {
Registration registration = new Registration(typeid(TestType), null, null, null);
assertThrown!(InstanceCreationException)(registration.getInstance(), null);
}
// Test getting instance without scope defined throws exception
unittest {
Registration registration = new Registration(typeid(TestType), null, null, null);
assertThrown!(InstanceCreationException)(registration.getInstance(), null);
}
// Test set single instance scope using scope setter
unittest {
Registration registration = new Registration(null, typeid(TestType), new InstanceFactory(), null);
auto chainedRegistration = registration.singleInstance();
auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance();
assert(instance1 is instance2, "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 single instance scope using scope setter
unittest {
Registration registration = new Registration(null, typeid(TestType), new InstanceFactory(), null);
auto chainedRegistration = registration.singleInstance();
auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance();
assert(instance1 is instance2, "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
unittest {
Registration registration = new Registration(null, typeid(TestType), new InstanceFactory(), null);
auto chainedRegistration = registration.newInstance();
auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance();
assert(instance1 !is instance2, "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 new instance scope using scope setter
unittest {
Registration registration = new Registration(null, typeid(TestType), new InstanceFactory(), null);
auto chainedRegistration = registration.newInstance();
auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance();
assert(instance1 !is instance2, "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
unittest {
Registration registration = new Registration(null, null, new InstanceFactory(), null);
auto expectedInstance = new TestType();
auto chainedRegistration = registration.existingInstance(expectedInstance);
auto actualInstance = registration.getInstance();
assert(expectedInstance is expectedInstance, "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 set existing instance scope using scope setter
unittest {
Registration registration = new Registration(null, null, new InstanceFactory(), null);
auto expectedInstance = new TestType();
auto chainedRegistration = registration.existingInstance(expectedInstance);
auto actualInstance = registration.getInstance();
assert(expectedInstance is expectedInstance, "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
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);
// Test linking registrations
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);
auto firstInstance = firstRegistration.getInstance();
auto secondInstance = secondRegistration.getInstance();
auto firstInstance = firstRegistration.getInstance();
auto secondInstance = secondRegistration.getInstance();
assert(firstInstance is secondInstance);
}
assert(firstInstance is secondInstance);
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/

View file

@ -1,6 +1,6 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2017 Mike Bierlee
* Copyright 2014-2018 Mike Bierlee
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
@ -12,126 +12,126 @@ import std.exception;
version(unittest) {
struct LocalStruct {
bool wasInjected = false;
}
struct LocalStruct {
bool wasInjected = false;
}
class LocalStructInjector : ValueInjector!LocalStruct {
public override LocalStruct get(string key) {
auto data = LocalStruct(true);
return data;
}
}
class LocalStructInjector : ValueInjector!LocalStruct {
public override LocalStruct get(string key) {
auto data = LocalStruct(true);
return data;
}
}
class LocalClassWithStruct {
@Value("")
public LocalStruct localStruct;
}
class LocalClassWithStruct {
@Value("")
public LocalStruct localStruct;
}
// Test injection of values
unittest {
auto container = new shared DependencyContainer();
container.register!MyConfig;
container.register!(ValueInjector!int, IntInjector);
container.register!(ValueInjector!string, StringInjector);
container.register!(ValueInjector!Thing, ThingInjector);
// Test injection of values
unittest {
auto container = new shared DependencyContainer();
container.register!MyConfig;
container.register!(ValueInjector!int, IntInjector);
container.register!(ValueInjector!string, StringInjector);
container.register!(ValueInjector!Thing, ThingInjector);
auto instance = container.resolve!MyConfig;
assert(instance.stuffs == 364);
assert(instance.name == "Le Chef");
assert(instance.thing.x == 8899);
}
auto instance = container.resolve!MyConfig;
assert(instance.stuffs == 364);
assert(instance.name == "Le Chef");
assert(instance.thing.x == 8899);
}
// Test injection of values throws exception when injector is not there
unittest {
auto container = new shared DependencyContainer();
container.register!MyConfig;
assertThrown!ResolveException(container.resolve!MyConfig);
// Test injection of values throws exception when injector is not there
unittest {
auto container = new shared DependencyContainer();
container.register!MyConfig;
assertThrown!ResolveException(container.resolve!MyConfig);
assertThrown!ValueInjectionException(autowire(container, new MyConfig()));
}
assertThrown!ValueInjectionException(autowire(container, new MyConfig()));
}
// Test injection of values with defaults
unittest {
auto container = new shared DependencyContainer();
container.register!ConfigWithDefaults;
container.register!(ValueInjector!int, DefaultIntInjector);
// Test injection of values with defaults
unittest {
auto container = new shared DependencyContainer();
container.register!ConfigWithDefaults;
container.register!(ValueInjector!int, DefaultIntInjector);
auto instance = container.resolve!ConfigWithDefaults;
assert(instance.noms == 9);
}
auto instance = container.resolve!ConfigWithDefaults;
assert(instance.noms == 9);
}
// Test mandatory injection of values which are available
unittest {
auto container = new shared DependencyContainer();
container.register!ConfigWithMandatory;
container.register!(ValueInjector!int, MandatoryAvailableIntInjector);
// Test mandatory injection of values which are available
unittest {
auto container = new shared DependencyContainer();
container.register!ConfigWithMandatory;
container.register!(ValueInjector!int, MandatoryAvailableIntInjector);
auto instance = container.resolve!ConfigWithMandatory;
assert(instance.nums == 7466);
}
auto instance = container.resolve!ConfigWithMandatory;
assert(instance.nums == 7466);
}
// Test mandatory injection of values which are not available
unittest {
auto container = new shared DependencyContainer();
container.register!ConfigWithMandatory;
container.register!(ValueInjector!int, MandatoryUnavailableIntInjector);
// Test mandatory injection of values which are not available
unittest {
auto container = new shared DependencyContainer();
container.register!ConfigWithMandatory;
container.register!(ValueInjector!int, MandatoryUnavailableIntInjector);
assertThrown!ResolveException(container.resolve!ConfigWithMandatory);
assertThrown!ValueInjectionException(autowire(container, new ConfigWithMandatory()));
}
assertThrown!ResolveException(container.resolve!ConfigWithMandatory);
assertThrown!ValueInjectionException(autowire(container, new ConfigWithMandatory()));
}
// Test injecting dependencies within value injectors
unittest {
auto container = new shared DependencyContainer();
auto dependency = new Dependency();
container.register!Dependency.existingInstance(dependency);
container.register!(ValueInjector!int, DependencyInjectedIntInjector);
auto injector = cast(DependencyInjectedIntInjector) container.resolve!(ValueInjector!int);
// Test injecting dependencies within value injectors
unittest {
auto container = new shared DependencyContainer();
auto dependency = new Dependency();
container.register!Dependency.existingInstance(dependency);
container.register!(ValueInjector!int, DependencyInjectedIntInjector);
auto injector = cast(DependencyInjectedIntInjector) container.resolve!(ValueInjector!int);
assert(injector.dependency is dependency);
}
assert(injector.dependency is dependency);
}
// Test injecting circular dependencies within value injectors
unittest {
auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, CircularIntInjector);
auto injector = cast(CircularIntInjector) container.resolve!(ValueInjector!int);
// Test injecting circular dependencies within value injectors
unittest {
auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, CircularIntInjector);
auto injector = cast(CircularIntInjector) container.resolve!(ValueInjector!int);
assert(injector.dependency is injector);
assert(injector.get("whatever") == 3);
}
assert(injector.dependency is injector);
assert(injector.get("whatever") == 3);
}
// Test value injection within value injectors
unittest {
auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, ValueInjectedIntInjector);
auto injector = cast(ValueInjectedIntInjector) container.resolve!(ValueInjector!int);
// Test value injection within value injectors
unittest {
auto container = new shared DependencyContainer();
container.register!(ValueInjector!int, ValueInjectedIntInjector);
auto injector = cast(ValueInjectedIntInjector) container.resolve!(ValueInjector!int);
assert(injector.count == 5);
}
assert(injector.count == 5);
}
// Test value injection within dependencies of value injectors
unittest {
auto container = new shared DependencyContainer();
container.register!ConfigWithDefaults;
// Test value injection within dependencies of value injectors
unittest {
auto container = new shared DependencyContainer();
container.register!ConfigWithDefaults;
container.register!(ValueInjector!int, DependencyValueInjectedIntInjector);
auto injector = cast(DependencyValueInjectedIntInjector) container.resolve!(ValueInjector!int);
container.register!(ValueInjector!int, DependencyValueInjectedIntInjector);
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)
unittest {
auto container = new shared DependencyContainer();
container.register!(ValueInjector!LocalStruct, LocalStructInjector);
container.register!LocalClassWithStruct;
// Test resolving locally defined struct injector (github issue #20)
unittest {
auto container = new shared DependencyContainer();
container.register!(ValueInjector!LocalStruct, LocalStructInjector);
container.register!LocalClassWithStruct;
auto injector = container.resolve!(ValueInjector!LocalStruct);
assert(injector !is null);
auto injector = container.resolve!(ValueInjector!LocalStruct);
assert(injector !is null);
auto localClass = container.resolve!LocalClassWithStruct;
assert(localClass.localStruct.wasInjected);
}
auto localClass = container.resolve!LocalClassWithStruct;
assert(localClass.localStruct.wasInjected);
}
}