diff --git a/.travis.yml b/.travis.yml index 8d2c704..9a44c1e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,4 +17,5 @@ script: - dub build --build=release --config=arrayCompletionExample - dub build --build=release --config=annotationsExample - dub build --build=release --config=applicationContextExample + - dub build --build=release --config=registerOnResolveExample # - dub build --build=ddox diff --git a/dub.json b/dub.json index a8b81fd..d663b56 100644 --- a/dub.json +++ b/dub.json @@ -90,6 +90,18 @@ "importPaths": [ "source" ] + }, + { + "name" : "registerOnResolveExample", + "description" : "Example where dependencies are registered at the moment they are resolved.", + "targetType": "executable", + "targetName": "registerOnResolveExample", + "sourcePaths": [ + "example/registeronresolve" + ], + "importPaths": [ + "source" + ] } ] } diff --git a/example/registeronresolve/app.d b/example/registeronresolve/app.d new file mode 100644 index 0000000..892d173 --- /dev/null +++ b/example/registeronresolve/app.d @@ -0,0 +1,45 @@ +/** + * Poodinis Dependency Injection Framework + * Copyright 2014-2016 Mike Bierlee + * This software is licensed under the terms of the MIT license. + * The full terms of the license can be found in the LICENSE file. + */ + +import poodinis; + +class Violin { +} + +interface InstrumentPlayer { +} + +class ViolinPlayer : InstrumentPlayer { + // Autowired concrete types can be registered on resolve + @Autowire + private Violin violin; +} + +class Orchestra { + // Autowired non-concrete types can be registered on resolved, given they have a qualifier. + @Autowire!ViolinPlayer + private InstrumentPlayer violinPlayer; +} + +void main() { + auto dependencies = DependencyContainer.getInstance(); + + /* + * By using the resolve option "registerBeforeResolving" you can register the resolved class + * immediately. Note that any autowired member will not automatically be registered as well. + */ + auto violinPlayer = dependencies.resolve!Violin([ResolveOption.registerBeforeResolving]); + + /* + * You can make the resolve option persistent by setting it on the container with setPersistentResolveOptions(). + * This will register all resolved types and their autowired members (recursively). + * Note that you will still get ResolveExceptions when a non-concrete type is autowired (without qualifier). + * In those cases you will still have to register those particular dependencies beforehand. + */ + dependencies.setPersistentResolveOptions(ResolveOption.registerBeforeResolving); + auto orchestra = dependencies.resolve!Orchestra; +}