mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add setting persistent registration options
This commit is contained in:
parent
947dd4823e
commit
def5ec2e88
|
@ -1,5 +1,8 @@
|
||||||
Poodinis Changelog
|
Poodinis Changelog
|
||||||
==================
|
==================
|
||||||
|
**Version NEXT**
|
||||||
|
* ADD setting persistent registration options
|
||||||
|
|
||||||
**Version 6.0.0**
|
**Version 6.0.0**
|
||||||
* CHANGE registration scopes are replaced by a single factory implementation. If you were not doing anything with the internal scope mechanism, you
|
* CHANGE registration scopes are replaced by a single factory implementation. If you were not doing anything with the internal scope mechanism, you
|
||||||
should not be affected by this change.
|
should not be affected by this change.
|
||||||
|
|
|
@ -197,3 +197,11 @@ class Context : ApplicationContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Persistent Registration Options
|
||||||
|
-------------------------------
|
||||||
|
If you want registration options to be persistent (applicable for every call to register()), you can use the container method setPersistentRegistrationOptions():
|
||||||
|
```d
|
||||||
|
dependencies.setPersistentRegistrationOptions(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); // Sets the option
|
||||||
|
dependencies.unsetPersistentRegistrationOptions(); // Clears the persistentent options
|
||||||
|
```
|
|
@ -69,6 +69,8 @@ synchronized class DependencyContainer {
|
||||||
|
|
||||||
private Registration[] autowireStack;
|
private Registration[] autowireStack;
|
||||||
|
|
||||||
|
private RegistrationOption[] persistentRegistrationOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a dependency by concrete class type.
|
* Register a dependency by concrete class type.
|
||||||
*
|
*
|
||||||
|
@ -126,7 +128,7 @@ synchronized class DependencyContainer {
|
||||||
auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this);
|
auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this);
|
||||||
newRegistration.singleInstance();
|
newRegistration.singleInstance();
|
||||||
|
|
||||||
if (!hasOption(options, RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION)) {
|
if (!hasOption(options, persistentRegistrationOptions, RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION)) {
|
||||||
static if (!is(SuperType == ConcreteType)) {
|
static if (!is(SuperType == ConcreteType)) {
|
||||||
auto concreteTypeRegistration = register!ConcreteType;
|
auto concreteTypeRegistration = register!ConcreteType;
|
||||||
concreteTypeRegistration.linkTo(newRegistration);
|
concreteTypeRegistration.linkTo(newRegistration);
|
||||||
|
@ -137,7 +139,13 @@ synchronized class DependencyContainer {
|
||||||
return newRegistration;
|
return newRegistration;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool hasOption(RegistrationOptionsTuple...)(RegistrationOptionsTuple options, RegistrationOption option) {
|
private bool hasOption(OptionType, OptionsTuple...)(OptionsTuple options, OptionType[] persistentOptions, OptionType option) {
|
||||||
|
foreach (presentOption; persistentOptions) {
|
||||||
|
if (presentOption == option) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach(presentOption ; options) {
|
foreach(presentOption ; options) {
|
||||||
if (presentOption == option) {
|
if (presentOption == option) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -358,4 +366,22 @@ synchronized class DependencyContainer {
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply persistent registration options which will be used everytime register() is called.
|
||||||
|
*/
|
||||||
|
public void setPersistentRegistrationOptions(RegistrationOptionsTuple...)(RegistrationOptionsTuple registrationOptions) {
|
||||||
|
unsetPersistentRegistrationOptions();
|
||||||
|
foreach (option; registrationOptions) {
|
||||||
|
persistentRegistrationOptions ~= option;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unsets all applied registration options
|
||||||
|
*/
|
||||||
|
public void unsetPersistentRegistrationOptions() {
|
||||||
|
persistentRegistrationOptions = [];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -585,4 +585,21 @@ version(unittest) {
|
||||||
container.registerContext!TestContext;
|
container.registerContext!TestContext;
|
||||||
container.resolve!ApplicationContext;
|
container.resolve!ApplicationContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test set persistent registration options
|
||||||
|
unittest {
|
||||||
|
shared(DependencyContainer) container = new DependencyContainer();
|
||||||
|
container.setPersistentRegistrationOptions(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION);
|
||||||
|
container.register!(TestInterface, TestClass);
|
||||||
|
assertThrown!ResolveException(container.resolve!TestClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test unset persistent registration options
|
||||||
|
unittest {
|
||||||
|
shared(DependencyContainer) container = new DependencyContainer();
|
||||||
|
container.setPersistentRegistrationOptions(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION);
|
||||||
|
container.unsetPersistentRegistrationOptions();
|
||||||
|
container.register!(TestInterface, TestClass);
|
||||||
|
container.resolve!TestClass;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue