mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Prevent registration, resolving and constructor injection selection of structs
Fixes #25
This commit is contained in:
parent
730d001d30
commit
dc48c87948
|
@ -164,7 +164,7 @@ synchronized class DependencyContainer {
|
|||
*
|
||||
* See_Also: singleInstance, newInstance, existingInstance, RegistrationOption
|
||||
*/
|
||||
public Registration register(SuperType, ConcreteType : SuperType)(RegistrationOption options = RegistrationOption.none) {
|
||||
public Registration register(SuperType, ConcreteType : SuperType)(RegistrationOption options = RegistrationOption.none) if (!is(ConcreteType == struct)) {
|
||||
TypeInfo registeredType = typeid(SuperType);
|
||||
TypeInfo_Class concreteType = typeid(ConcreteType);
|
||||
|
||||
|
@ -262,7 +262,7 @@ synchronized class DependencyContainer {
|
|||
* ---
|
||||
* You need to use the resolve method which allows you to specify a qualifier.
|
||||
*/
|
||||
public RegistrationType resolve(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) {
|
||||
public RegistrationType resolve(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) if (!is(RegistrationType == struct)) {
|
||||
return resolve!(RegistrationType, RegistrationType)(resolveOptions);
|
||||
}
|
||||
|
||||
|
@ -292,7 +292,7 @@ synchronized class DependencyContainer {
|
|||
* container.resolve!(Animal, Dog);
|
||||
* ---
|
||||
*/
|
||||
public QualifierType resolve(RegistrationType, QualifierType : RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) {
|
||||
public QualifierType resolve(RegistrationType, QualifierType : RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) if (!is(QualifierType == struct)) {
|
||||
TypeInfo resolveType = typeid(RegistrationType);
|
||||
TypeInfo qualifierType = typeid(QualifierType);
|
||||
|
||||
|
|
|
@ -131,7 +131,7 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory {
|
|||
private static bool parametersAreValid(Params...)() {
|
||||
bool isValid = true;
|
||||
foreach(param; Params) {
|
||||
if (isBuiltinType!param) {
|
||||
if (isBuiltinType!param || is(param == struct)) {
|
||||
isValid = false;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -124,6 +124,17 @@ version(unittest) {
|
|||
assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen);
|
||||
}
|
||||
|
||||
// Test injecting constructor of class with struct constructor parameters
|
||||
unittest {
|
||||
auto container = new shared DependencyContainer();
|
||||
container.register!SomeOtherClassThen;
|
||||
|
||||
auto factory = new ConstructorInjectingInstanceFactory!ClassWithStructConstructor(container);
|
||||
auto instance = cast(ClassWithStructConstructor) factory.getInstance();
|
||||
|
||||
assert(instance !is null);
|
||||
assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen);
|
||||
}
|
||||
|
||||
// Test injecting constructor of class with empty constructor will skip injection
|
||||
unittest {
|
||||
|
|
|
@ -513,6 +513,17 @@ version(unittest) {
|
|||
}
|
||||
}
|
||||
|
||||
class ClassWithStructConstructor {
|
||||
public SomeOtherClassThen someOtherClassThen;
|
||||
|
||||
this(Thing willNotBePicked) {
|
||||
}
|
||||
|
||||
this(SomeOtherClassThen someOtherClassThen) {
|
||||
this.someOtherClassThen = someOtherClassThen;
|
||||
}
|
||||
}
|
||||
|
||||
class TestType {}
|
||||
|
||||
class Dependency {}
|
||||
|
|
Loading…
Reference in a new issue