diff --git a/.gitignore b/.gitignore index d06ef68..7131463 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,6 @@ -/poodinis.exe +/*.exe /poodinis.lib /poodinis.obj -/quickstartExample.exe -/qualifiersExample.exe -/arrayCompletionExample.exe -/annotationsExample.exe /.settings /.dub /dub.selections.json @@ -18,3 +14,4 @@ /qualifiersExample /arrayCompletionExample /annotationsExample +/applicationContextExample diff --git a/.travis.yml b/.travis.yml index 92b1483..c66367b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,4 +11,5 @@ script: - dub build --build=release --config=qualifiersExample - dub build --build=release --config=arrayCompletionExample - dub build --build=release --config=annotationsExample + - dub build --build=release --config=applicationContextExample # - dub build --build=ddox diff --git a/dub.json b/dub.json index e8d7e30..e76b1bf 100644 --- a/dub.json +++ b/dub.json @@ -78,6 +78,18 @@ "importPaths": [ "source" ] + }, + { + "name" : "applicationContextExample", + "description" : "Example where an application context is used to set-up dependencies.", + "targetType": "executable", + "targetName": "applicationContextExample", + "sourcePaths": [ + "example/applicationcontext" + ], + "importPaths": [ + "source" + ] } ] } diff --git a/example/applicationcontext/app.d b/example/applicationcontext/app.d new file mode 100644 index 0000000..fe95c04 --- /dev/null +++ b/example/applicationcontext/app.d @@ -0,0 +1,61 @@ +import poodinis; + +import std.stdio; + +class TownSquare { + + @Autowire + public MarketStall marketStall; + + public void makeSound() { + marketStall.announceGoodsForSale(); + } + +} + +interface Goods { + public string getGoodsName(); +} + +class Fish : Goods { + public override string getGoodsName() { + return "Fish"; + } +} + +class MarketStall { + private Goods goods; + + this(Goods goods) { + this.goods = goods; + } + + public void announceGoodsForSale() { + writeln(goods.getGoodsName() ~ " for sale!"); + } +} + +class ExampleApplicationContext : ApplicationContext { + + @Autowire + public Goods goods; + + public override void registerDependencies(shared(DependencyContainer) container) { + container.register!(Goods, Fish); + container.register!TownSquare; + } + + @Component + public MarketStall marketStall() { + return new MarketStall(goods); + } + +} + +void main() { + auto container = DependencyContainer.getInstance(); + container.registerContext!ExampleApplicationContext; + + auto townSquare = container.resolve!TownSquare; + townSquare.makeSound(); +}