Add application context example

This commit is contained in:
Mike Bierlee 2015-12-28 22:38:58 +01:00
parent 78e8acd7a3
commit 435dc37c3c
4 changed files with 76 additions and 5 deletions

7
.gitignore vendored
View file

@ -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

View file

@ -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

View file

@ -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"
]
}
]
}

View file

@ -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();
}