mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add ability to mark autowire dependencies as optional
This commit is contained in:
parent
6746fd64a0
commit
7308702dfe
|
@ -1,5 +1,9 @@
|
||||||
Poodinis Changelog
|
Poodinis Changelog
|
||||||
==================
|
==================
|
||||||
|
**Version NEXT**
|
||||||
|
* ADD ability to mark autowire dependencies as optional. When you use UDA @OptionalDependency, a type which fails to autowire will remain null
|
||||||
|
(or an empty array). No ResolveException is thrown.
|
||||||
|
|
||||||
**Version 6.1.0**
|
**Version 6.1.0**
|
||||||
* ADD setting persistent registration and resolve options
|
* ADD setting persistent registration and resolve options
|
||||||
* DEPRECATE DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION, use doNotAddConcreteTypeRegistration instead
|
* DEPRECATE DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION, use doNotAddConcreteTypeRegistration instead
|
||||||
|
|
11
TUTORIAL.md
11
TUTORIAL.md
|
@ -94,6 +94,17 @@ assert(instance.dependency !is null);
|
||||||
```
|
```
|
||||||
If an interface is to be autowired, you must register a concrete class by interface. Any class registered by concrete type can only be injected when a dependency on a concrete type is autowired.
|
If an interface is to be autowired, you must register a concrete class by interface. Any class registered by concrete type can only be injected when a dependency on a concrete type is autowired.
|
||||||
|
|
||||||
|
Using the UDA `OptionalDependency` you can mark an autowired member as being optional. When a member is optional, no ResolveException will be thrown when
|
||||||
|
the type of the member is not registered and `ResolveOption.registerBeforeResolving` is not set on the container. The member will remain null or an empty array in
|
||||||
|
case of array dependencies.
|
||||||
|
```d
|
||||||
|
class ExampleClass {
|
||||||
|
@Autowire
|
||||||
|
@OptionalDependency
|
||||||
|
private AnotherExampleClass dependency;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Circular dependencies
|
Circular dependencies
|
||||||
---------------------
|
---------------------
|
||||||
Poodinis can autowire circular dependencies when they are registered with `singleInstance` or `existingInstance` registration scopes. Circular dependencies in registrations with `newInstance` scopes will not be autowired, as this would cause an endless loop.
|
Poodinis can autowire circular dependencies when they are registered with `singleInstance` or `existingInstance` registration scopes. Circular dependencies in registrations with `newInstance` scopes will not be autowired, as this would cause an endless loop.
|
||||||
|
|
|
@ -12,6 +12,12 @@ import std.digest.md;
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import std.conv;
|
import std.conv;
|
||||||
|
|
||||||
|
class SecurityAuditor {
|
||||||
|
public void submitAudit() {
|
||||||
|
writeln("Hmmmyes I have received your audit. It is.... adequate.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class SuperSecurityDevice {
|
class SuperSecurityDevice {
|
||||||
private int seed;
|
private int seed;
|
||||||
|
|
||||||
|
@ -32,6 +38,18 @@ class SecurityManager {
|
||||||
@Autowire
|
@Autowire
|
||||||
@AssignNewInstance
|
@AssignNewInstance
|
||||||
private SuperSecurityDevice levelTwoSecurity;
|
private SuperSecurityDevice levelTwoSecurity;
|
||||||
|
|
||||||
|
@Autowire
|
||||||
|
@OptionalDependency
|
||||||
|
private SecurityAuditor auditor;
|
||||||
|
|
||||||
|
public void doAudit() {
|
||||||
|
if (auditor !is null) {
|
||||||
|
auditor.submitAudit();
|
||||||
|
} else {
|
||||||
|
writeln("I uh, will skip the audit for now...");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
@ -45,6 +63,10 @@ void main() {
|
||||||
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
|
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
|
||||||
|
|
||||||
if (manager.levelOneSecurity is manager.levelTwoSecurity) {
|
if (manager.levelOneSecurity is manager.levelTwoSecurity) {
|
||||||
writeln("SECURITY BREACH!!!!!");
|
writeln("SECURITY BREACH!!!!!"); // Should not be printed since levelTwoSecurity is a new instance.
|
||||||
|
} else {
|
||||||
|
writeln("Security okay!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
manager.doAudit(); // Will not cause the SecurityAuditor to print, since we didn't register a SecurityAuditor.
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,14 @@ struct Autowire(QualifierType = UseMemberType) {
|
||||||
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 {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UDA for annotating class members to be autowired with a new instance regardless of their registration scope.
|
* UDA for annotating class members to be autowired with a new instance regardless of their registration scope.
|
||||||
*
|
*
|
||||||
|
@ -127,10 +135,15 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
|
||||||
alias MemberType = typeof(Type.tupleof[memberIndex]);
|
alias MemberType = typeof(Type.tupleof[memberIndex]);
|
||||||
|
|
||||||
enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance);
|
enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance);
|
||||||
|
enum isOptional = hasUDA!(Type.tupleof[memberIndex], OptionalDependency);
|
||||||
|
|
||||||
static if (isDynamicArray!MemberType) {
|
static if (isDynamicArray!MemberType) {
|
||||||
alias MemberElementType = ElementType!MemberType;
|
alias MemberElementType = ElementType!MemberType;
|
||||||
|
static if (isOptional) {
|
||||||
|
auto instances = container.resolveAll!MemberElementType([ResolveOption.noResolveException]);
|
||||||
|
} else {
|
||||||
auto instances = container.resolveAll!MemberElementType;
|
auto instances = container.resolveAll!MemberElementType;
|
||||||
|
}
|
||||||
instance.tupleof[memberIndex] = instances;
|
instance.tupleof[memberIndex] = instances;
|
||||||
debug(poodinisVerbose) {
|
debug(poodinisVerbose) {
|
||||||
printDebugAutowiringArray(typeid(MemberElementType), typeid(Type), &instance, member);
|
printDebugAutowiringArray(typeid(MemberElementType), typeid(Type), &instance, member);
|
||||||
|
@ -143,12 +156,12 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
|
||||||
MemberType qualifiedInstance;
|
MemberType qualifiedInstance;
|
||||||
static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) {
|
static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) {
|
||||||
alias QualifierType = typeof(autowireAttribute.qualifier);
|
alias QualifierType = typeof(autowireAttribute.qualifier);
|
||||||
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance)(container);
|
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance, isOptional)(container);
|
||||||
debug(poodinisVerbose) {
|
debug(poodinisVerbose) {
|
||||||
qualifiedInstanceType = typeid(QualifierType);
|
qualifiedInstanceType = typeid(QualifierType);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance)(container);
|
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance, isOptional)(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.tupleof[memberIndex] = qualifiedInstance;
|
instance.tupleof[memberIndex] = qualifiedInstance;
|
||||||
|
@ -164,14 +177,18 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private QualifierType createOrResolveInstance(MemberType, QualifierType, bool createNew)(shared(DependencyContainer) container) {
|
private QualifierType createOrResolveInstance(MemberType, QualifierType, bool createNew, bool isOptional)(shared(DependencyContainer) container) {
|
||||||
static if (createNew) {
|
static if (createNew) {
|
||||||
auto instanceFactory = new InstanceFactory(typeid(MemberType), CreatesSingleton.no, null);
|
auto instanceFactory = new InstanceFactory(typeid(MemberType), CreatesSingleton.no, null);
|
||||||
return cast(MemberType) instanceFactory.getInstance();
|
return cast(MemberType) instanceFactory.getInstance();
|
||||||
|
} else {
|
||||||
|
static if (isOptional) {
|
||||||
|
return container.resolve!(MemberType, QualifierType)([ResolveOption.noResolveException]);
|
||||||
} else {
|
} else {
|
||||||
return container.resolve!(MemberType, QualifierType);
|
return container.resolve!(MemberType, QualifierType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Autowire the given instance using the globally available dependency container.
|
* Autowire the given instance using the globally available dependency container.
|
||||||
|
|
|
@ -73,6 +73,20 @@ version(unittest) {
|
||||||
public ComponentA componentA;
|
public ComponentA componentA;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class OuttaTime {
|
||||||
|
@Autowire
|
||||||
|
@OptionalDependency
|
||||||
|
public InterfaceA interfaceA;
|
||||||
|
|
||||||
|
@Autowire
|
||||||
|
@OptionalDependency
|
||||||
|
public ComponentA componentA;
|
||||||
|
|
||||||
|
@Autowire
|
||||||
|
@OptionalDependency
|
||||||
|
public ComponentC[] componentCs;
|
||||||
|
}
|
||||||
|
|
||||||
// Test autowiring concrete type to existing instance
|
// Test autowiring concrete type to existing instance
|
||||||
unittest {
|
unittest {
|
||||||
shared(DependencyContainer) container = new DependencyContainer();
|
shared(DependencyContainer) container = new DependencyContainer();
|
||||||
|
@ -214,4 +228,16 @@ version(unittest) {
|
||||||
|
|
||||||
assert(instance.componentA !is null);
|
assert(instance.componentA !is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test autowiring optional depenencies
|
||||||
|
unittest {
|
||||||
|
shared(DependencyContainer) container = new DependencyContainer();
|
||||||
|
auto instance = new OuttaTime();
|
||||||
|
|
||||||
|
container.autowire(instance);
|
||||||
|
|
||||||
|
assert(instance.interfaceA is null);
|
||||||
|
assert(instance.componentA is null);
|
||||||
|
assert(instance.componentCs is null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue