Add setting persistent registration options

This commit is contained in:
Mike Bierlee 2016-02-03 21:25:33 +01:00
parent 947dd4823e
commit def5ec2e88
4 changed files with 56 additions and 2 deletions

View file

@ -1,5 +1,8 @@
Poodinis Changelog
==================
**Version NEXT**
* ADD setting persistent registration options
**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
should not be affected by this change.

View file

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

View file

@ -69,6 +69,8 @@ synchronized class DependencyContainer {
private Registration[] autowireStack;
private RegistrationOption[] persistentRegistrationOptions;
/**
* Register a dependency by concrete class type.
*
@ -126,7 +128,7 @@ synchronized class DependencyContainer {
auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this);
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)) {
auto concreteTypeRegistration = register!ConcreteType;
concreteTypeRegistration.linkTo(newRegistration);
@ -137,7 +139,13 @@ synchronized class DependencyContainer {
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) {
if (presentOption == option) {
return true;
@ -358,4 +366,22 @@ synchronized class DependencyContainer {
}
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 = [];
}
}

View file

@ -585,4 +585,21 @@ version(unittest) {
container.registerContext!TestContext;
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;
}
}