Prevent registration, resolving and constructor injection selection of structs

Fixes #25
This commit is contained in:
Mike Bierlee 2018-06-11 18:45:27 +03:00
parent 730d001d30
commit dc48c87948
4 changed files with 26 additions and 4 deletions

View file

@ -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);

View file

@ -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;
}

View file

@ -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 {

View file

@ -513,6 +513,17 @@ version(unittest) {
}
}
class ClassWithStructConstructor {
public SomeOtherClassThen someOtherClassThen;
this(Thing willNotBePicked) {
}
this(SomeOtherClassThen someOtherClassThen) {
this.someOtherClassThen = someOtherClassThen;
}
}
class TestType {}
class Dependency {}