From 932e3dab6b511cb634e9c190b984e7f285f97df0 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Mon, 12 Dec 2016 22:48:23 +0100 Subject: [PATCH] Add value injection example --- .travis.yml | 1 + dub.json | 12 +++++++ example/valueinjection/app.d | 64 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 example/valueinjection/app.d diff --git a/.travis.yml b/.travis.yml index ed8f50f..855a72a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,4 +23,5 @@ script: - dub build --build=release --config=applicationContextExample - dub build --build=release --config=registerOnResolveExample - dub build --build=release --config=constructorInjectionExample + - dub build --build=release --config=valueInjectionExample # - dub build --build=ddox diff --git a/dub.json b/dub.json index 12c9000..bfbd711 100644 --- a/dub.json +++ b/dub.json @@ -114,6 +114,18 @@ "importPaths": [ "source" ] + }, + { + "name" : "valueInjectionExample", + "description" : "Example where values are injected into dependencies.", + "targetType": "executable", + "targetName": "valueInjectionExample", + "sourcePaths": [ + "example/valueinjection" + ], + "importPaths": [ + "source" + ] } ] } diff --git a/example/valueinjection/app.d b/example/valueinjection/app.d new file mode 100644 index 0000000..c960563 --- /dev/null +++ b/example/valueinjection/app.d @@ -0,0 +1,64 @@ +/** + * 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; + +import std.stdio; +import std.string; + +class IntValueInjector : ValueInjector!int { + int get(string key) { + switch(key) { + case "http.port": + return 8080; + case "http.keep_alive": + return 60; + default: + throw new ValueNotAvailableException(key); + } + } +} + +class StringValueInjector : ValueInjector!string { + string get(string key) { + switch(key) { + case "http.hostname": + return "acme.org"; + default: + throw new ValueNotAvailableException(key); + } + } +} + +class HttpServer { + + @Value("http.port") + private int port = 80; + + @Value("http.hostname") + private string hostName = "localhost"; + + @Value("http.max_connections") + private int maxConnections = 1000; // Default assignment is kept because max_connections is not available within the injector + + @MandatoryValue("http.keep_alive") + private int keepAliveTime; // A ResolveException is thrown when the value is not available, default assignments are not used. + + public void serve() { + writeln(format("Serving pages for %s:%s with max connection count of %s", hostName, port, maxConnections)); + } +} + +void main() { + auto dependencies = new shared DependencyContainer(); + dependencies.register!(ValueInjector!int, IntValueInjector); + dependencies.register!(ValueInjector!string, StringValueInjector); + dependencies.register!HttpServer; + + auto server = dependencies.resolve!HttpServer; + server.serve(); // Prints "Serving pages for acme.org:8080 with max connection count of 1000" +}