Always register concrete type when registering type by supertype by default.

This deprecates ADD_CONCRETE_TYPE_REGISTRATION (see DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION for reverse configuration). This also means you do not need to use qualifiers when registering single type of super type by super type.
This commit is contained in:
Mike Bierlee 2015-08-17 22:25:57 +02:00
parent 8c103df7e5
commit da16513a8d
3 changed files with 31 additions and 6 deletions

View file

@ -1,5 +1,9 @@
Poodinis Changelog
==================
** Next version**
* DEPRECATE ADD_CONCRETE_TYPE_REGISTRATION registration option. It basically does nothing anymore. See next point.
* CHANGE adding registrations by super type always registers them by concrete type as well now. (Previously done with ADD_CONCRETE_TYPE_REGISTRATION). See DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION for the reverse behaviour.
**Version 4.0.0**
* REMOVE deprecated module "dependency.d"

View file

@ -64,8 +64,17 @@ public enum RegistrationOptions {
*
* assert(firstCat is secondCat);
* ---
*
* Deprecated: ADD_CONCRETE_TYPE_REGISTRATION behaviour is default now. If you want to reverse to happen, use DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION.
*/
ADD_CONCRETE_TYPE_REGISTRATION
ADD_CONCRETE_TYPE_REGISTRATION,
/**
* Prevent a concrete type being registered on itself. With this option you will always need
* to use the supertype as the type of the dependency.
*/
DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION
}
/**
@ -140,12 +149,10 @@ synchronized class DependencyContainer {
auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this);
newRegistration.singleInstance();
if (hasOption(options, RegistrationOptions.ADD_CONCRETE_TYPE_REGISTRATION)) {
if (!hasOption(options, RegistrationOptions.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION)) {
static if (!is(SuperType == ConcreteType)) {
auto concreteTypeRegistration = register!ConcreteType;
concreteTypeRegistration.linkTo(newRegistration);
} else {
throw new RegistrationException("Option ADD_CONCRETE_TYPE_REGISTRATION cannot be used when registering a concrete type registration", concreteType);
}
}

View file

@ -443,10 +443,24 @@ version(unittest) {
assert(firstInstance is secondInstance);
}
// Test registering concrete type with option ADD_CONCRETE_TYPE_REGISTRATION
// Test registering type with option DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION
unittest {
shared(DependencyContainer) container = new DependencyContainer();
assertThrown!RegistrationException(container.register!(TestClass, TestClass)(RegistrationOptions.ADD_CONCRETE_TYPE_REGISTRATION));
container.register!(TestInterface, TestClass)(RegistrationOptions.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION);
auto firstInstance = container.resolve!TestInterface;
assertThrown!ResolveException(container.resolve!TestClass);
}
// Test registering type will register by contrete type by default
unittest {
shared(DependencyContainer) container = new DependencyContainer();
container.register!(TestInterface, TestClass);
auto firstInstance = container.resolve!TestInterface;
auto secondInstance = container.resolve!TestClass;
assert(firstInstance is secondInstance);
}
// Test resolving all registrations to an interface