mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Rename "Container" -> "DependencyContainer". Deprecate use of "Container"
This commit is contained in:
parent
088f5e851d
commit
b2bb6f15c4
|
@ -20,7 +20,7 @@ class Autowire{};
|
||||||
|
|
||||||
alias Autowired = Autowire;
|
alias Autowired = Autowire;
|
||||||
|
|
||||||
public void autowire(Type)(Container container, Type instance) {
|
public void autowire(Type)(DependencyContainer container, Type instance) {
|
||||||
debug {
|
debug {
|
||||||
auto memberType = typeid(Type);
|
auto memberType = typeid(Type);
|
||||||
auto instanceAddress = &instance;
|
auto instanceAddress = &instance;
|
||||||
|
@ -53,5 +53,5 @@ mixin template AutowireConstructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void globalAutowire(Type)(Type instance) {
|
public void globalAutowire(Type)(Type instance) {
|
||||||
Container.getInstance().autowire(instance);
|
DependencyContainer.getInstance().autowire(instance);
|
||||||
}
|
}
|
|
@ -30,9 +30,12 @@ class ResolveException : Exception {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Container {
|
deprecated("Container has been renamed to DependencyContainer")
|
||||||
|
alias Container = DependencyContainer;
|
||||||
|
|
||||||
private static Container instance;
|
class DependencyContainer {
|
||||||
|
|
||||||
|
private static DependencyContainer instance;
|
||||||
|
|
||||||
private Registration[TypeInfo] registrations;
|
private Registration[TypeInfo] registrations;
|
||||||
|
|
||||||
|
@ -86,9 +89,9 @@ class Container {
|
||||||
registrations.remove(typeid(RegistrationType));
|
registrations.remove(typeid(RegistrationType));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Container getInstance() {
|
public static DependencyContainer getInstance() {
|
||||||
if (instance is null) {
|
if (instance is null) {
|
||||||
instance = new Container();
|
instance = new DependencyContainer();
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring concrete type to existing instance
|
// Test autowiring concrete type to existing instance
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!ComponentA;
|
container.register!ComponentA;
|
||||||
auto componentB = new ComponentB();
|
auto componentB = new ComponentB();
|
||||||
container.autowire!(ComponentB)(componentB);
|
container.autowire!(ComponentB)(componentB);
|
||||||
|
@ -73,7 +73,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring interface type to existing instance
|
// Test autowiring interface type to existing instance
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!(InterfaceA, ComponentC);
|
container.register!(InterfaceA, ComponentC);
|
||||||
auto componentD = new ComponentD();
|
auto componentD = new ComponentD();
|
||||||
container.autowire!(ComponentD)(componentD);
|
container.autowire!(ComponentD)(componentD);
|
||||||
|
@ -82,7 +82,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring will only happen once
|
// Test autowiring will only happen once
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!(InterfaceA, ComponentC).newInstance();
|
container.register!(InterfaceA, ComponentC).newInstance();
|
||||||
auto componentD = new ComponentD();
|
auto componentD = new ComponentD();
|
||||||
container.autowire!(ComponentD)(componentD);
|
container.autowire!(ComponentD)(componentD);
|
||||||
|
@ -94,14 +94,14 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring unregistered type
|
// Test autowiring unregistered type
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
auto componentD = new ComponentD();
|
auto componentD = new ComponentD();
|
||||||
assertThrown!(ResolveException)(container.autowire!(ComponentD)(componentD), "Autowiring unregistered type should throw ResolveException");
|
assertThrown!(ResolveException)(container.autowire!(ComponentD)(componentD), "Autowiring unregistered type should throw ResolveException");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test autowiring member with non-autowire attribute does not autowire
|
// Test autowiring member with non-autowire attribute does not autowire
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
auto componentE = new ComponentE();
|
auto componentE = new ComponentE();
|
||||||
container.autowire!ComponentE(componentE);
|
container.autowire!ComponentE(componentE);
|
||||||
assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire");
|
assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire");
|
||||||
|
@ -109,7 +109,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowire in constructor
|
// Test autowire in constructor
|
||||||
unittest {
|
unittest {
|
||||||
auto container = Container.getInstance();
|
auto container = DependencyContainer.getInstance();
|
||||||
container.register!ComponentA;
|
container.register!ComponentA;
|
||||||
auto componentF = new ComponentF();
|
auto componentF = new ComponentF();
|
||||||
auto autowiredComponentA = componentF.componentA;
|
auto autowiredComponentA = componentF.componentA;
|
||||||
|
@ -124,7 +124,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowire class with alias declaration
|
// Test autowire class with alias declaration
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!ComponentA;
|
container.register!ComponentA;
|
||||||
auto componentDeclarationCocktail = new ComponentDeclarationCocktail();
|
auto componentDeclarationCocktail = new ComponentDeclarationCocktail();
|
||||||
|
|
||||||
|
|
|
@ -83,14 +83,14 @@ version(unittest) {
|
||||||
|
|
||||||
// Test register concrete type
|
// Test register concrete type
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
auto registration = container.register!(TestClass)();
|
auto registration = container.register!(TestClass)();
|
||||||
assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same");
|
assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test resolve registered type
|
// Test resolve registered type
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!(TestClass)();
|
container.register!(TestClass)();
|
||||||
TestClass actualInstance = container.resolve!(TestClass)();
|
TestClass actualInstance = container.resolve!(TestClass)();
|
||||||
assert(actualInstance !is null, "Resolved type is null");
|
assert(actualInstance !is null, "Resolved type is null");
|
||||||
|
@ -99,7 +99,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test register interface
|
// Test register interface
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!(TestInterface, TestClass)();
|
container.register!(TestInterface, TestClass)();
|
||||||
TestInterface actualInstance = container.resolve!(TestInterface)();
|
TestInterface actualInstance = container.resolve!(TestInterface)();
|
||||||
assert(actualInstance !is null, "Resolved type is null");
|
assert(actualInstance !is null, "Resolved type is null");
|
||||||
|
@ -108,13 +108,13 @@ version(unittest) {
|
||||||
|
|
||||||
// Test resolve non-registered type
|
// Test resolve non-registered type
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving non-registered type does not fail");
|
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving non-registered type does not fail");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test clear registrations
|
// Test clear registrations
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!(TestClass)();
|
container.register!(TestClass)();
|
||||||
container.clearAllRegistrations();
|
container.clearAllRegistrations();
|
||||||
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving cleared type does not fail");
|
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving cleared type does not fail");
|
||||||
|
@ -122,14 +122,14 @@ version(unittest) {
|
||||||
|
|
||||||
// Test get singleton of container
|
// Test get singleton of container
|
||||||
unittest {
|
unittest {
|
||||||
auto instance1 = Container.getInstance();
|
auto instance1 = DependencyContainer.getInstance();
|
||||||
auto instance2 = Container.getInstance();
|
auto instance2 = DependencyContainer.getInstance();
|
||||||
assert(instance1 is instance2, "getInstance does not return the same instance");
|
assert(instance1 is instance2, "getInstance does not return the same instance");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test resolve single instance for type
|
// Test resolve single instance for type
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!(TestClass)().singleInstance();
|
container.register!(TestClass)().singleInstance();
|
||||||
auto instance1 = container.resolve!(TestClass);
|
auto instance1 = container.resolve!(TestClass);
|
||||||
auto instance2 = container.resolve!(TestClass);
|
auto instance2 = container.resolve!(TestClass);
|
||||||
|
@ -138,7 +138,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test resolve new instance for type
|
// Test resolve new instance for type
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!(TestClass)().newInstance();
|
container.register!(TestClass)().newInstance();
|
||||||
auto instance1 = container.resolve!(TestClass);
|
auto instance1 = container.resolve!(TestClass);
|
||||||
auto instance2 = container.resolve!(TestClass);
|
auto instance2 = container.resolve!(TestClass);
|
||||||
|
@ -147,7 +147,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test resolve existing instance for type
|
// Test resolve existing instance for type
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
auto expectedInstance = new TestClass();
|
auto expectedInstance = new TestClass();
|
||||||
container.register!(TestClass)().existingInstance(expectedInstance);
|
container.register!(TestClass)().existingInstance(expectedInstance);
|
||||||
auto actualInstance = container.resolve!(TestClass);
|
auto actualInstance = container.resolve!(TestClass);
|
||||||
|
@ -156,7 +156,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowire resolved instances
|
// Test autowire resolved instances
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!AutowiredClass;
|
container.register!AutowiredClass;
|
||||||
container.register!ComponentClass;
|
container.register!ComponentClass;
|
||||||
auto componentInstance = container.resolve!ComponentClass;
|
auto componentInstance = container.resolve!ComponentClass;
|
||||||
|
@ -166,7 +166,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test circular autowiring
|
// Test circular autowiring
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!ComponentMouse;
|
container.register!ComponentMouse;
|
||||||
container.register!ComponentCat;
|
container.register!ComponentCat;
|
||||||
auto mouse = container.resolve!ComponentMouse;
|
auto mouse = container.resolve!ComponentMouse;
|
||||||
|
@ -176,7 +176,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test remove registration
|
// Test remove registration
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!TestClass;
|
container.register!TestClass;
|
||||||
container.removeRegistration!TestClass;
|
container.removeRegistration!TestClass;
|
||||||
assertThrown!ResolveException(container.resolve!TestClass);
|
assertThrown!ResolveException(container.resolve!TestClass);
|
||||||
|
@ -184,7 +184,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring does not autowire member where instance is non-null
|
// Test autowiring does not autowire member where instance is non-null
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
auto existingA = new AutowiredClass();
|
auto existingA = new AutowiredClass();
|
||||||
auto existingB = new ComponentClass();
|
auto existingB = new ComponentClass();
|
||||||
existingB.autowiredClass = existingA;
|
existingB.autowiredClass = existingA;
|
||||||
|
@ -199,7 +199,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring circular dependency by third-degree
|
// Test autowiring circular dependency by third-degree
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!Eenie;
|
container.register!Eenie;
|
||||||
container.register!Meenie;
|
container.register!Meenie;
|
||||||
container.register!Moe;
|
container.register!Moe;
|
||||||
|
@ -211,7 +211,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring deep circular dependencies
|
// Test autowiring deep circular dependencies
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!Ittie;
|
container.register!Ittie;
|
||||||
container.register!Bittie;
|
container.register!Bittie;
|
||||||
container.register!Banana;
|
container.register!Banana;
|
||||||
|
@ -223,7 +223,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring deep circular dependencies with newInstance scope does not autowire new instance second time
|
// Test autowiring deep circular dependencies with newInstance scope does not autowire new instance second time
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!(Ittie).newInstance();
|
container.register!(Ittie).newInstance();
|
||||||
container.register!(Bittie).newInstance();
|
container.register!(Bittie).newInstance();
|
||||||
container.register!(Banana).newInstance();
|
container.register!(Banana).newInstance();
|
||||||
|
@ -235,7 +235,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test autowiring type registered by interface fails (BUG test case)
|
// Test autowiring type registered by interface fails (BUG test case)
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!Banana;
|
container.register!Banana;
|
||||||
container.register!(SuperInterface, SuperImplementation);
|
container.register!(SuperInterface, SuperImplementation);
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ version(unittest) {
|
||||||
|
|
||||||
// Test reusing a container after clearing all registrations
|
// Test reusing a container after clearing all registrations
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new Container();
|
auto container = new DependencyContainer();
|
||||||
container.register!Banana;
|
container.register!Banana;
|
||||||
container.clearAllRegistrations();
|
container.clearAllRegistrations();
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue