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
|
||||
==================
|
||||
**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**
|
||||
* ADD setting persistent registration and resolve options
|
||||
* 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.
|
||||
|
||||
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
|
||||
---------------------
|
||||
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.conv;
|
||||
|
||||
class SecurityAuditor {
|
||||
public void submitAudit() {
|
||||
writeln("Hmmmyes I have received your audit. It is.... adequate.");
|
||||
}
|
||||
}
|
||||
|
||||
class SuperSecurityDevice {
|
||||
private int seed;
|
||||
|
||||
|
@ -32,6 +38,18 @@ class SecurityManager {
|
|||
@Autowire
|
||||
@AssignNewInstance
|
||||
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() {
|
||||
|
@ -45,6 +63,10 @@ void main() {
|
|||
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
@ -127,10 +135,15 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
|
|||
alias MemberType = typeof(Type.tupleof[memberIndex]);
|
||||
|
||||
enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance);
|
||||
enum isOptional = hasUDA!(Type.tupleof[memberIndex], OptionalDependency);
|
||||
|
||||
static if (isDynamicArray!MemberType) {
|
||||
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);
|
||||
|
@ -143,12 +156,12 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
|
|||
MemberType qualifiedInstance;
|
||||
static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) {
|
||||
alias QualifierType = typeof(autowireAttribute.qualifier);
|
||||
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance)(container);
|
||||
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance, isOptional)(container);
|
||||
debug(poodinisVerbose) {
|
||||
qualifiedInstanceType = typeid(QualifierType);
|
||||
}
|
||||
} else {
|
||||
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance)(container);
|
||||
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance, isOptional)(container);
|
||||
}
|
||||
|
||||
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) {
|
||||
auto instanceFactory = new InstanceFactory(typeid(MemberType), CreatesSingleton.no, null);
|
||||
return cast(MemberType) instanceFactory.getInstance();
|
||||
} else {
|
||||
static if (isOptional) {
|
||||
return container.resolve!(MemberType, QualifierType)([ResolveOption.noResolveException]);
|
||||
} else {
|
||||
return container.resolve!(MemberType, QualifierType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Autowire the given instance using the globally available dependency container.
|
||||
|
|
|
@ -73,6 +73,20 @@ version(unittest) {
|
|||
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
|
||||
unittest {
|
||||
shared(DependencyContainer) container = new DependencyContainer();
|
||||
|
@ -214,4 +228,16 @@ version(unittest) {
|
|||
|
||||
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