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;
|
||||
|
||||
public void autowire(Type)(Container container, Type instance) {
|
||||
public void autowire(Type)(DependencyContainer container, Type instance) {
|
||||
debug {
|
||||
auto memberType = typeid(Type);
|
||||
auto instanceAddress = &instance;
|
||||
|
@ -53,5 +53,5 @@ mixin template AutowireConstructor() {
|
|||
}
|
||||
|
||||
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;
|
||||
|
||||
|
@ -86,9 +89,9 @@ class Container {
|
|||
registrations.remove(typeid(RegistrationType));
|
||||
}
|
||||
|
||||
public static Container getInstance() {
|
||||
public static DependencyContainer getInstance() {
|
||||
if (instance is null) {
|
||||
instance = new Container();
|
||||
instance = new DependencyContainer();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ version(unittest) {
|
|||
|
||||
// Test autowiring concrete type to existing instance
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!ComponentA;
|
||||
auto componentB = new ComponentB();
|
||||
container.autowire!(ComponentB)(componentB);
|
||||
|
@ -73,7 +73,7 @@ version(unittest) {
|
|||
|
||||
// Test autowiring interface type to existing instance
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!(InterfaceA, ComponentC);
|
||||
auto componentD = new ComponentD();
|
||||
container.autowire!(ComponentD)(componentD);
|
||||
|
@ -82,7 +82,7 @@ version(unittest) {
|
|||
|
||||
// Test autowiring will only happen once
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!(InterfaceA, ComponentC).newInstance();
|
||||
auto componentD = new ComponentD();
|
||||
container.autowire!(ComponentD)(componentD);
|
||||
|
@ -94,14 +94,14 @@ version(unittest) {
|
|||
|
||||
// Test autowiring unregistered type
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
auto componentD = new ComponentD();
|
||||
assertThrown!(ResolveException)(container.autowire!(ComponentD)(componentD), "Autowiring unregistered type should throw ResolveException");
|
||||
}
|
||||
|
||||
// Test autowiring member with non-autowire attribute does not autowire
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
auto componentE = new ComponentE();
|
||||
container.autowire!ComponentE(componentE);
|
||||
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
|
||||
unittest {
|
||||
auto container = Container.getInstance();
|
||||
auto container = DependencyContainer.getInstance();
|
||||
container.register!ComponentA;
|
||||
auto componentF = new ComponentF();
|
||||
auto autowiredComponentA = componentF.componentA;
|
||||
|
@ -124,7 +124,7 @@ version(unittest) {
|
|||
|
||||
// Test autowire class with alias declaration
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!ComponentA;
|
||||
auto componentDeclarationCocktail = new ComponentDeclarationCocktail();
|
||||
|
||||
|
|
|
@ -83,14 +83,14 @@ version(unittest) {
|
|||
|
||||
// Test register concrete type
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
auto registration = container.register!(TestClass)();
|
||||
assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same");
|
||||
}
|
||||
|
||||
// Test resolve registered type
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!(TestClass)();
|
||||
TestClass actualInstance = container.resolve!(TestClass)();
|
||||
assert(actualInstance !is null, "Resolved type is null");
|
||||
|
@ -99,7 +99,7 @@ version(unittest) {
|
|||
|
||||
// Test register interface
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!(TestInterface, TestClass)();
|
||||
TestInterface actualInstance = container.resolve!(TestInterface)();
|
||||
assert(actualInstance !is null, "Resolved type is null");
|
||||
|
@ -108,13 +108,13 @@ version(unittest) {
|
|||
|
||||
// Test resolve non-registered type
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving non-registered type does not fail");
|
||||
}
|
||||
|
||||
// Test clear registrations
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!(TestClass)();
|
||||
container.clearAllRegistrations();
|
||||
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving cleared type does not fail");
|
||||
|
@ -122,14 +122,14 @@ version(unittest) {
|
|||
|
||||
// Test get singleton of container
|
||||
unittest {
|
||||
auto instance1 = Container.getInstance();
|
||||
auto instance2 = Container.getInstance();
|
||||
auto instance1 = DependencyContainer.getInstance();
|
||||
auto instance2 = DependencyContainer.getInstance();
|
||||
assert(instance1 is instance2, "getInstance does not return the same instance");
|
||||
}
|
||||
|
||||
// Test resolve single instance for type
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!(TestClass)().singleInstance();
|
||||
auto instance1 = container.resolve!(TestClass);
|
||||
auto instance2 = container.resolve!(TestClass);
|
||||
|
@ -138,7 +138,7 @@ version(unittest) {
|
|||
|
||||
// Test resolve new instance for type
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!(TestClass)().newInstance();
|
||||
auto instance1 = container.resolve!(TestClass);
|
||||
auto instance2 = container.resolve!(TestClass);
|
||||
|
@ -147,7 +147,7 @@ version(unittest) {
|
|||
|
||||
// Test resolve existing instance for type
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
auto expectedInstance = new TestClass();
|
||||
container.register!(TestClass)().existingInstance(expectedInstance);
|
||||
auto actualInstance = container.resolve!(TestClass);
|
||||
|
@ -156,7 +156,7 @@ version(unittest) {
|
|||
|
||||
// Test autowire resolved instances
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!AutowiredClass;
|
||||
container.register!ComponentClass;
|
||||
auto componentInstance = container.resolve!ComponentClass;
|
||||
|
@ -166,7 +166,7 @@ version(unittest) {
|
|||
|
||||
// Test circular autowiring
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!ComponentMouse;
|
||||
container.register!ComponentCat;
|
||||
auto mouse = container.resolve!ComponentMouse;
|
||||
|
@ -176,7 +176,7 @@ version(unittest) {
|
|||
|
||||
// Test remove registration
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!TestClass;
|
||||
container.removeRegistration!TestClass;
|
||||
assertThrown!ResolveException(container.resolve!TestClass);
|
||||
|
@ -184,7 +184,7 @@ version(unittest) {
|
|||
|
||||
// Test autowiring does not autowire member where instance is non-null
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
auto existingA = new AutowiredClass();
|
||||
auto existingB = new ComponentClass();
|
||||
existingB.autowiredClass = existingA;
|
||||
|
@ -199,7 +199,7 @@ version(unittest) {
|
|||
|
||||
// Test autowiring circular dependency by third-degree
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!Eenie;
|
||||
container.register!Meenie;
|
||||
container.register!Moe;
|
||||
|
@ -211,7 +211,7 @@ version(unittest) {
|
|||
|
||||
// Test autowiring deep circular dependencies
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!Ittie;
|
||||
container.register!Bittie;
|
||||
container.register!Banana;
|
||||
|
@ -223,7 +223,7 @@ version(unittest) {
|
|||
|
||||
// Test autowiring deep circular dependencies with newInstance scope does not autowire new instance second time
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!(Ittie).newInstance();
|
||||
container.register!(Bittie).newInstance();
|
||||
container.register!(Banana).newInstance();
|
||||
|
@ -235,7 +235,7 @@ version(unittest) {
|
|||
|
||||
// Test autowiring type registered by interface fails (BUG test case)
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!Banana;
|
||||
container.register!(SuperInterface, SuperImplementation);
|
||||
|
||||
|
@ -246,7 +246,7 @@ version(unittest) {
|
|||
|
||||
// Test reusing a container after clearing all registrations
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
auto container = new DependencyContainer();
|
||||
container.register!Banana;
|
||||
container.clearAllRegistrations();
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue