From 7fba33247ac345eb7c912a50bdaf1d2374616f49 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sat, 26 Sep 2015 22:40:34 +0200 Subject: [PATCH] Update tutorial to reflect new interface registration behaviour --- TUTORIAL.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/TUTORIAL.md b/TUTORIAL.md index 0217473..7a9d7de 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -27,20 +27,19 @@ To manually resolve a dependency, all you have to do is resolve the dependency's ```d auto exampleClassInstance = dependencies.resolve!ExampleClass; ``` -If the class is registered by interface and not by concrete type, you cannot resolve the class by concrete type. Registration of both a concrete type and interface type will resolve different registrations, returning different instances: +If the class is registered by interface and not by concrete type, you can still resolve the class by concrete type: ```d -auto exampleClassInstance = dependencies.resolve!ExampleClass; -auto exampleClassInstance2 = dependencies.resolve!ExampleInterface; -assert(exampleClassInstance !is exampleClassInstance2); -``` -You can solve this by adding the ADD_CONCRETE_TYPE_REGISTRATION option when registering: -```d -dependencies.register!(ExampleInterface, ExampleClass)(RegistrationOptions.ADD_CONCRETE_TYPE_REGISTRATION); -auto exampleClassInstance = dependencies.resolve!ExampleClass; -auto exampleClassInstance2 = dependencies.resolve!ExampleInterface; +auto exampleClassInstance = dependencies.resolve!ExampleInterface; +auto exampleClassInstance2 = dependencies.resolve!ExampleClass; assert(exampleClassInstance is exampleClassInstance2); ``` +If you want to prevent registrations from being both registered by interface and concrete type, use the DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION option when registering: +```d +dependencies.register!(ExampleInterface, ExampleClass)(RegistrationOptions.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); +auto exampleClassInstance = dependencies.resolve!ExampleInterface; +auto exampleClassInstance2 = dependencies.resolve!ExampleClass; // A ResolveException is thrown +``` Dependency scopes -----------------