mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Merge remote-tracking branch 'origin/master' into develop
This commit is contained in:
commit
a8da958bfb
|
@ -1,4 +1,4 @@
|
|||
Copyright (c) 2014-2016 Mike Bierlee
|
||||
Copyright (c) 2014-2017 Mike Bierlee
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
Poodinis Dependency Injection Framework
|
||||
=======================================
|
||||
Version 8.0.0
|
||||
Copyright 2014-2016 Mike Bierlee
|
||||
Copyright 2014-2017 Mike Bierlee
|
||||
Licensed under the terms of the MIT license - See [LICENSE.txt](LICENSE.txt)
|
||||
|
||||
Master: [![Build Status](https://api.travis-ci.org/mbierlee/poodinis.png?branch=master)](https://travis-ci.org/mbierlee/poodinis) - Dev: [![Build Status](https://api.travis-ci.org/mbierlee/poodinis.png?branch=develop)](https://travis-ci.org/mbierlee/poodinis)
|
||||
|
|
2
dub.json
2
dub.json
|
@ -3,7 +3,7 @@
|
|||
"description" : "A dependency injection framework with support for autowiring.",
|
||||
"homepage": "http://lostmoment.com/open-source/poodinis",
|
||||
"authors": ["Mike Bierlee"],
|
||||
"copyright": "Copyright 2014-2016 Mike Bierlee",
|
||||
"copyright": "Copyright 2014-2017 Mike Bierlee",
|
||||
"license": "MIT",
|
||||
"configurations": [
|
||||
{
|
||||
|
|
|
@ -1,72 +1,72 @@
|
|||
/**
|
||||
* 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.random;
|
||||
import std.digest.md;
|
||||
import std.stdio;
|
||||
import std.conv;
|
||||
|
||||
class SecurityAuditor {
|
||||
public void submitAudit() {
|
||||
writeln("Hmmmyes I have received your audit. It is.... adequate.");
|
||||
}
|
||||
}
|
||||
|
||||
class SuperSecurityDevice {
|
||||
private int seed;
|
||||
|
||||
public this() {
|
||||
auto randomGenerator = Random(unpredictableSeed);
|
||||
seed = uniform(0, 999, randomGenerator);
|
||||
}
|
||||
|
||||
public string getPassword() {
|
||||
return to!string(seed) ~ "t1m3sp13!!:";
|
||||
}
|
||||
}
|
||||
|
||||
class SecurityManager {
|
||||
@Autowire
|
||||
private SuperSecurityDevice levelOneSecurity;
|
||||
|
||||
@Autowire
|
||||
@AssignNewInstance
|
||||
private SuperSecurityDevice levelTwoSecurity;
|
||||
|
||||
@Autowire
|
||||
@OptionalDependency
|
||||
private SecurityAuditor auditor;
|
||||
|
||||
public void doAudit() {
|
||||
if (auditor !is null) {
|
||||
auditor.submitAudit();
|
||||
} else {
|
||||
writeln("I uh, will skip the audit for now...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto dependencies = new shared DependencyContainer();
|
||||
dependencies.register!SuperSecurityDevice; // Registered with the default "Single instance" scope
|
||||
dependencies.register!SecurityManager;
|
||||
|
||||
auto manager = dependencies.resolve!SecurityManager;
|
||||
|
||||
writeln("Password for user one: " ~ manager.levelOneSecurity.getPassword());
|
||||
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
|
||||
|
||||
if (manager.levelOneSecurity is manager.levelTwoSecurity) {
|
||||
writeln("SECURITY BREACH!!!!!"); // Should not be printed since levelTwoSecurity is a new instance.
|
||||
} else {
|
||||
writeln("Security okay!");
|
||||
}
|
||||
|
||||
manager.doAudit(); // Will not cause the SecurityAuditor to print, since we didn't register a SecurityAuditor.
|
||||
}
|
||||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2017 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.random;
|
||||
import std.digest.md;
|
||||
import std.stdio;
|
||||
import std.conv;
|
||||
|
||||
class SecurityAuditor {
|
||||
public void submitAudit() {
|
||||
writeln("Hmmmyes I have received your audit. It is.... adequate.");
|
||||
}
|
||||
}
|
||||
|
||||
class SuperSecurityDevice {
|
||||
private int seed;
|
||||
|
||||
public this() {
|
||||
auto randomGenerator = Random(unpredictableSeed);
|
||||
seed = uniform(0, 999, randomGenerator);
|
||||
}
|
||||
|
||||
public string getPassword() {
|
||||
return to!string(seed) ~ "t1m3sp13!!:";
|
||||
}
|
||||
}
|
||||
|
||||
class SecurityManager {
|
||||
@Autowire
|
||||
private SuperSecurityDevice levelOneSecurity;
|
||||
|
||||
@Autowire
|
||||
@AssignNewInstance
|
||||
private SuperSecurityDevice levelTwoSecurity;
|
||||
|
||||
@Autowire
|
||||
@OptionalDependency
|
||||
private SecurityAuditor auditor;
|
||||
|
||||
public void doAudit() {
|
||||
if (auditor !is null) {
|
||||
auditor.submitAudit();
|
||||
} else {
|
||||
writeln("I uh, will skip the audit for now...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto dependencies = new shared DependencyContainer();
|
||||
dependencies.register!SuperSecurityDevice; // Registered with the default "Single instance" scope
|
||||
dependencies.register!SecurityManager;
|
||||
|
||||
auto manager = dependencies.resolve!SecurityManager;
|
||||
|
||||
writeln("Password for user one: " ~ manager.levelOneSecurity.getPassword());
|
||||
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
|
||||
|
||||
if (manager.levelOneSecurity is manager.levelTwoSecurity) {
|
||||
writeln("SECURITY BREACH!!!!!"); // Should not be printed since levelTwoSecurity is a new instance.
|
||||
} else {
|
||||
writeln("Security okay!");
|
||||
}
|
||||
|
||||
manager.doAudit(); // Will not cause the SecurityAuditor to print, since we didn't register a SecurityAuditor.
|
||||
}
|
||||
|
|
|
@ -1,68 +1,68 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
class TownSquare {
|
||||
|
||||
@Autowire
|
||||
private 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
|
||||
private 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 = new shared DependencyContainer();
|
||||
container.registerContext!ExampleApplicationContext;
|
||||
|
||||
auto townSquare = container.resolve!TownSquare;
|
||||
townSquare.makeSound();
|
||||
}
|
||||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2017 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;
|
||||
|
||||
class TownSquare {
|
||||
|
||||
@Autowire
|
||||
private 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
|
||||
private 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 = new shared DependencyContainer();
|
||||
container.registerContext!ExampleApplicationContext;
|
||||
|
||||
auto townSquare = container.resolve!TownSquare;
|
||||
townSquare.makeSound();
|
||||
}
|
||||
|
|
|
@ -1,54 +1,54 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
interface Pie {
|
||||
public void eat();
|
||||
}
|
||||
|
||||
class BlueBerryPie : Pie {
|
||||
public override void eat() {
|
||||
writeln("Nom nom nom. I like this one!");
|
||||
}
|
||||
}
|
||||
|
||||
class ApplePie : Pie {
|
||||
public override void eat() {
|
||||
writeln("Nom nom nom. These aren't real apples...");
|
||||
}
|
||||
}
|
||||
|
||||
class CardboardBoxPie : Pie {
|
||||
public override void eat() {
|
||||
writeln("Nom nom nom. This... is not a pie.");
|
||||
}
|
||||
}
|
||||
|
||||
class PieEater {
|
||||
@Autowire
|
||||
private Pie[] pies;
|
||||
|
||||
public void eatThemAll() {
|
||||
foreach(pie ; pies) {
|
||||
pie.eat();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto dependencies = new shared DependencyContainer();
|
||||
dependencies.register!(Pie, BlueBerryPie);
|
||||
dependencies.register!(Pie, ApplePie);
|
||||
dependencies.register!(Pie, CardboardBoxPie);
|
||||
dependencies.register!(PieEater);
|
||||
|
||||
auto eater = dependencies.resolve!PieEater;
|
||||
eater.eatThemAll();
|
||||
}
|
||||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2017 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;
|
||||
|
||||
interface Pie {
|
||||
public void eat();
|
||||
}
|
||||
|
||||
class BlueBerryPie : Pie {
|
||||
public override void eat() {
|
||||
writeln("Nom nom nom. I like this one!");
|
||||
}
|
||||
}
|
||||
|
||||
class ApplePie : Pie {
|
||||
public override void eat() {
|
||||
writeln("Nom nom nom. These aren't real apples...");
|
||||
}
|
||||
}
|
||||
|
||||
class CardboardBoxPie : Pie {
|
||||
public override void eat() {
|
||||
writeln("Nom nom nom. This... is not a pie.");
|
||||
}
|
||||
}
|
||||
|
||||
class PieEater {
|
||||
@Autowire
|
||||
private Pie[] pies;
|
||||
|
||||
public void eatThemAll() {
|
||||
foreach(pie ; pies) {
|
||||
pie.eat();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto dependencies = new shared DependencyContainer();
|
||||
dependencies.register!(Pie, BlueBerryPie);
|
||||
dependencies.register!(Pie, ApplePie);
|
||||
dependencies.register!(Pie, CardboardBoxPie);
|
||||
dependencies.register!(PieEater);
|
||||
|
||||
auto eater = dependencies.resolve!PieEater;
|
||||
eater.eatThemAll();
|
||||
}
|
||||
|
|
|
@ -1,64 +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.
|
||||
*/
|
||||
|
||||
class Scheduler {
|
||||
private Calendar calendar;
|
||||
|
||||
// All parameters will autmatically be assigned when Scheduler is created.
|
||||
this(Calendar calendar) {
|
||||
this.calendar = calendar;
|
||||
}
|
||||
|
||||
public void scheduleJob() {
|
||||
calendar.findOpenDate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Calendar {
|
||||
private HardwareClock hardwareClock;
|
||||
|
||||
// This constructor contains built-in type "int" and thus will not be used.
|
||||
this(int initialDateTimeStamp, HardwareClock hardwareClock) {
|
||||
}
|
||||
|
||||
// This constructor is chosen instead as candidate for injection when Calendar is created.
|
||||
this(HardwareClock hardwareClock) {
|
||||
this.hardwareClock = hardwareClock;
|
||||
}
|
||||
|
||||
public void findOpenDate() {
|
||||
hardwareClock.doThings();
|
||||
}
|
||||
}
|
||||
|
||||
class HardwareClock {
|
||||
// Parameterless constructors will halt any further selection of constructors.
|
||||
this() {}
|
||||
|
||||
// As a result, this constructor will not be used when HardwareClock is created.
|
||||
this(Calendar calendar) {
|
||||
throw new Exception("This constructor should not be used by Poodinis");
|
||||
}
|
||||
|
||||
public void doThings() {
|
||||
import std.stdio;
|
||||
writeln("Things are being done!");
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
import poodinis; // Locally imported to emphasize that classes do not depend on Poodinis.
|
||||
|
||||
auto dependencies = new shared DependencyContainer();
|
||||
dependencies.register!Scheduler;
|
||||
dependencies.register!Calendar;
|
||||
dependencies.register!HardwareClock;
|
||||
|
||||
auto scheduler = dependencies.resolve!Scheduler;
|
||||
scheduler.scheduleJob();
|
||||
}
|
||||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
||||
class Scheduler {
|
||||
private Calendar calendar;
|
||||
|
||||
// All parameters will autmatically be assigned when Scheduler is created.
|
||||
this(Calendar calendar) {
|
||||
this.calendar = calendar;
|
||||
}
|
||||
|
||||
public void scheduleJob() {
|
||||
calendar.findOpenDate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Calendar {
|
||||
private HardwareClock hardwareClock;
|
||||
|
||||
// This constructor contains built-in type "int" and thus will not be used.
|
||||
this(int initialDateTimeStamp, HardwareClock hardwareClock) {
|
||||
}
|
||||
|
||||
// This constructor is chosen instead as candidate for injection when Calendar is created.
|
||||
this(HardwareClock hardwareClock) {
|
||||
this.hardwareClock = hardwareClock;
|
||||
}
|
||||
|
||||
public void findOpenDate() {
|
||||
hardwareClock.doThings();
|
||||
}
|
||||
}
|
||||
|
||||
class HardwareClock {
|
||||
// Parameterless constructors will halt any further selection of constructors.
|
||||
this() {}
|
||||
|
||||
// As a result, this constructor will not be used when HardwareClock is created.
|
||||
this(Calendar calendar) {
|
||||
throw new Exception("This constructor should not be used by Poodinis");
|
||||
}
|
||||
|
||||
public void doThings() {
|
||||
import std.stdio;
|
||||
writeln("Things are being done!");
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
import poodinis; // Locally imported to emphasize that classes do not depend on Poodinis.
|
||||
|
||||
auto dependencies = new shared DependencyContainer();
|
||||
dependencies.register!Scheduler;
|
||||
dependencies.register!Calendar;
|
||||
dependencies.register!HardwareClock;
|
||||
|
||||
auto scheduler = dependencies.resolve!Scheduler;
|
||||
scheduler.scheduleJob();
|
||||
}
|
||||
|
|
|
@ -1,53 +1,53 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
class ADependency {
|
||||
@PostConstruct
|
||||
public void postConstructor() {
|
||||
writeln("The dependency is created.");
|
||||
}
|
||||
|
||||
public void callMe() {
|
||||
writeln("The dependency was called.");
|
||||
}
|
||||
}
|
||||
|
||||
class AClass {
|
||||
@Autowire
|
||||
public ADependency dependency; // Dependencies are autowired before the post-constructor is called.
|
||||
|
||||
@PostConstruct
|
||||
public void postConstructor() {
|
||||
writeln("The class is created.");
|
||||
if (dependency !is null) {
|
||||
writeln("The dependency is autowired.");
|
||||
} else {
|
||||
writeln("The dependency was NOT autowired.");
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestructor() {
|
||||
writeln("The class is no longer registered with the container.");
|
||||
}
|
||||
}
|
||||
|
||||
public void main() {
|
||||
auto container = new shared DependencyContainer();
|
||||
container.register!ADependency;
|
||||
container.register!AClass;
|
||||
auto instance = container.resolve!AClass; // Will cause the post constructor to be called.
|
||||
container.removeRegistration!AClass; // Will cause the pre destructor to be called.
|
||||
|
||||
// The instance won't be destroyed by the container and as long as there are references to it,
|
||||
// it will not be collected by the garbage collector either.
|
||||
instance.dependency.callMe();
|
||||
}
|
||||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2017 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;
|
||||
|
||||
class ADependency {
|
||||
@PostConstruct
|
||||
public void postConstructor() {
|
||||
writeln("The dependency is created.");
|
||||
}
|
||||
|
||||
public void callMe() {
|
||||
writeln("The dependency was called.");
|
||||
}
|
||||
}
|
||||
|
||||
class AClass {
|
||||
@Autowire
|
||||
public ADependency dependency; // Dependencies are autowired before the post-constructor is called.
|
||||
|
||||
@PostConstruct
|
||||
public void postConstructor() {
|
||||
writeln("The class is created.");
|
||||
if (dependency !is null) {
|
||||
writeln("The dependency is autowired.");
|
||||
} else {
|
||||
writeln("The dependency was NOT autowired.");
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestructor() {
|
||||
writeln("The class is no longer registered with the container.");
|
||||
}
|
||||
}
|
||||
|
||||
public void main() {
|
||||
auto container = new shared DependencyContainer();
|
||||
container.register!ADependency;
|
||||
container.register!AClass;
|
||||
auto instance = container.resolve!AClass; // Will cause the post constructor to be called.
|
||||
container.removeRegistration!AClass; // Will cause the pre destructor to be called.
|
||||
|
||||
// The instance won't be destroyed by the container and as long as there are references to it,
|
||||
// it will not be collected by the garbage collector either.
|
||||
instance.dependency.callMe();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,45 +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 = new shared DependencyContainer();
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2017 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 = new shared DependencyContainer();
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
|
|
@ -1,64 +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"
|
||||
}
|
||||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2017 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"
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
*
|
||||
* Authors:
|
||||
* Mike Bierlee, m.bierlee@lostmoment.com
|
||||
* Copyright: 2014-2016 Mike Bierlee
|
||||
* Copyright: 2014-2017 Mike Bierlee
|
||||
* License:
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
*
|
||||
* Authors:
|
||||
* Mike Bierlee, m.bierlee@lostmoment.com
|
||||
* Copyright: 2014-2016 Mike Bierlee
|
||||
* Copyright: 2014-2017 Mike Bierlee
|
||||
* License:
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
*
|
||||
* Authors:
|
||||
* Mike Bierlee, m.bierlee@lostmoment.com
|
||||
* Copyright: 2014-2016 Mike Bierlee
|
||||
* Copyright: 2014-2017 Mike Bierlee
|
||||
* License:
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
*
|
||||
* Authors:
|
||||
* Mike Bierlee, m.bierlee@lostmoment.com
|
||||
* Copyright: 2014-2016 Mike Bierlee
|
||||
* Copyright: 2014-2017 Mike Bierlee
|
||||
* License:
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
*
|
||||
* Authors:
|
||||
* Mike Bierlee, m.bierlee@lostmoment.com
|
||||
* Copyright: 2014-2016 Mike Bierlee
|
||||
* Copyright: 2014-2017 Mike Bierlee
|
||||
* License:
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
* Kenji Hara,
|
||||
* Shoichi Kato,
|
||||
* Mike Bierlee (m.bierlee@lostmoment.com)
|
||||
* Copyright: Copyright Digital Mars 2005 - 2009., Copyright Andrei Alexandrescu 2008-, Jonathan M Davis 2011-., 2014-2016 Mike Bierlee
|
||||
* Copyright: Copyright Digital Mars 2005 - 2009., Copyright Andrei Alexandrescu 2008-, Jonathan M Davis 2011-., 2014-2017 Mike Bierlee
|
||||
* License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0)
|
||||
*/
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
*
|
||||
* Authors:
|
||||
* Mike Bierlee, m.bierlee@lostmoment.com
|
||||
* Copyright: 2014-2016 Mike Bierlee
|
||||
* Copyright: 2014-2017 Mike Bierlee
|
||||
* License:
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* Authors:
|
||||
* Mike Bierlee, m.bierlee@lostmoment.com
|
||||
* Copyright: 2014-2016 Mike Bierlee
|
||||
* Copyright: 2014-2017 Mike Bierlee
|
||||
* License:
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014-2016 Mike Bierlee
|
||||
* Copyright 2014-2017 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.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue