mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Rename dependency container instance in examples more aptly
This commit is contained in:
parent
3da9393a17
commit
a919402981
44
README.md
44
README.md
|
@ -37,11 +37,11 @@ class DataWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
auto container = DependencyContainer.getInstance();
|
auto dependencies = DependencyContainer.getInstance();
|
||||||
container.register!DataWriter;
|
dependencies.register!DataWriter;
|
||||||
container.register!(Database, RelationalDatabase);
|
dependencies.register!(Database, RelationalDatabase);
|
||||||
|
|
||||||
auto writer = container.resolve!DataWriter;
|
auto writer = dependencies.resolve!DataWriter;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
For more examples, see the [examples](example) directory.
|
For more examples, see the [examples](example) directory.
|
||||||
|
@ -50,30 +50,30 @@ For more examples, see the [examples](example) directory.
|
||||||
To register a class, a new dependency container must be instantiated:
|
To register a class, a new dependency container must be instantiated:
|
||||||
```d
|
```d
|
||||||
// Register a private container
|
// Register a private container
|
||||||
auto container = new DependencyContainer();
|
auto dependencies = new DependencyContainer();
|
||||||
// Or use the singleton container
|
// Or use the singleton container
|
||||||
container = DependencyContainer.getInstance();
|
dependencies = DependencyContainer.getInstance();
|
||||||
```
|
```
|
||||||
###Registering dependencies
|
###Registering dependencies
|
||||||
To make dependencies available, they have to be registered:
|
To make dependencies available, they have to be registered:
|
||||||
```d
|
```d
|
||||||
// Register concrete class
|
// Register concrete class
|
||||||
container.register!ExampleClass;
|
dependencies.register!ExampleClass;
|
||||||
// Register by interface
|
// Register by interface
|
||||||
container.register!(ExampleInterface, ExampleClass);
|
dependencies.register!(ExampleInterface, ExampleClass);
|
||||||
```
|
```
|
||||||
In the above example, dependencies on the concrete class and interface will resolve an instance of class ExampleClass. Registering a class by interface does not automatically register by concrete type.
|
In the above example, dependencies on the concrete class and interface will resolve an instance of class ExampleClass. Registering a class by interface does not automatically register by concrete type.
|
||||||
|
|
||||||
###Resolving dependencies
|
###Resolving dependencies
|
||||||
To manually resolve a dependency, all you have to do is resolve the dependency's type using the container in which it is registered:
|
To manually resolve a dependency, all you have to do is resolve the dependency's type using the container in which it is registered:
|
||||||
```d
|
```d
|
||||||
auto exampleClassInstance = container.resolve!ExampleClass;
|
auto exampleClassInstance = dependencies.resolve!ExampleClass;
|
||||||
```
|
```
|
||||||
If the class is registered by interface and not by concrete type, you cannot resolve the class by concrete type. Registration of both a concrete type and interface type will resolve different registrations, returning different instances:
|
If the class is registered by interface and not by concrete type, you cannot resolve the class by concrete type. Registration of both a concrete type and interface type will resolve different registrations, returning different instances:
|
||||||
|
|
||||||
```d
|
```d
|
||||||
auto exampleClassInstance = container.resolve!ExampleClass;
|
auto exampleClassInstance = dependencies.resolve!ExampleClass;
|
||||||
auto exampleClassInstance2 = container.resolve!ExampleInterface;
|
auto exampleClassInstance2 = dependencies.resolve!ExampleInterface;
|
||||||
assert(exampleClassInstance !is exampleClassInstance2);
|
assert(exampleClassInstance !is exampleClassInstance2);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -83,18 +83,18 @@ With dependency scopes, you can control how a dependency is resolved. The scope
|
||||||
* Resolve a dependency using a single instance (default):
|
* Resolve a dependency using a single instance (default):
|
||||||
|
|
||||||
```d
|
```d
|
||||||
container.register!(ExampleClass).singleInstance();
|
dependencies.register!(ExampleClass).singleInstance();
|
||||||
```
|
```
|
||||||
* Resolve a dependency with a new instance each time it is resolved:
|
* Resolve a dependency with a new instance each time it is resolved:
|
||||||
|
|
||||||
```d
|
```d
|
||||||
container.register!(ExampleClass).newInstance();
|
dependencies.register!(ExampleClass).newInstance();
|
||||||
```
|
```
|
||||||
* Resolve a dependency using a pre-existing instance
|
* Resolve a dependency using a pre-existing instance
|
||||||
|
|
||||||
```d
|
```d
|
||||||
auto preExistingInstance = new ExampleClass();
|
auto preExistingInstance = new ExampleClass();
|
||||||
container.register!(ExampleClass).existingInstance(preExistingInstance);
|
dependencies.register!(ExampleClass).existingInstance(preExistingInstance);
|
||||||
```
|
```
|
||||||
|
|
||||||
###Autowiring
|
###Autowiring
|
||||||
|
@ -107,18 +107,18 @@ class ExampleClassB {
|
||||||
public ExampleClassA dependency;
|
public ExampleClassA dependency;
|
||||||
}
|
}
|
||||||
|
|
||||||
container.register!ExampleClassA;
|
dependencies.register!ExampleClassA;
|
||||||
auto exampleInstance = new ExampleClassB();
|
auto exampleInstance = new ExampleClassB();
|
||||||
container.autowire(exampleInstance);
|
dependencies.autowire(exampleInstance);
|
||||||
assert(exampleInstance.dependency !is null);
|
assert(exampleInstance.dependency !is null);
|
||||||
```
|
```
|
||||||
At the moment, it is only possible to autowire public members or properties.
|
At the moment, it is only possible to autowire public members or properties.
|
||||||
|
|
||||||
Dependencies are automatically autowired when a class is resolved. So when you register ExampleClassB, its member, *dependency*, is automatically autowired:
|
Dependencies are automatically autowired when a class is resolved. So when you register ExampleClassB, its member, *dependency*, is automatically autowired:
|
||||||
```d
|
```d
|
||||||
container.register!ExampleClassA;
|
dependencies.register!ExampleClassA;
|
||||||
container.register!ExampleClassB;
|
dependencies.register!ExampleClassB;
|
||||||
auto instance = container.resolve!ExampleClassB;
|
auto instance = dependencies.resolve!ExampleClassB;
|
||||||
assert(instance.dependency !is null);
|
assert(instance.dependency !is null);
|
||||||
```
|
```
|
||||||
If an interface is to be autowired, you must register a concrete class by interface. Any class registered by concrete type can only be injected when a dependency on a concrete type is autowired.
|
If an interface is to be autowired, you must register a concrete class by interface. Any class registered by concrete type can only be injected when a dependency on a concrete type is autowired.
|
||||||
|
@ -130,9 +130,9 @@ Poodinis can autowire circular dependencies when they are registered with single
|
||||||
You can register multiple concrete types to a super type. When doing so, you will need to specify a qualifier when resolving that type:
|
You can register multiple concrete types to a super type. When doing so, you will need to specify a qualifier when resolving that type:
|
||||||
```d
|
```d
|
||||||
// Color is an interface, Blue and Red are classes implementing that interface
|
// Color is an interface, Blue and Red are classes implementing that interface
|
||||||
container.register!(Color, Blue);
|
dependencies.register!(Color, Blue);
|
||||||
container.register!(Color, Red);
|
dependencies.register!(Color, Red);
|
||||||
auto blueInstance = container.resolve!(Color, Blue);
|
auto blueInstance = dependencies.resolve!(Color, Blue);
|
||||||
```
|
```
|
||||||
If you want to autowire a type registered to multiple concrete types, specify a qualified type as template argument:
|
If you want to autowire a type registered to multiple concrete types, specify a qualified type as template argument:
|
||||||
```d
|
```d
|
||||||
|
|
|
@ -1,50 +1,50 @@
|
||||||
import poodinis.dependency;
|
import poodinis.dependency;
|
||||||
|
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
|
|
||||||
interface Engine {
|
interface Engine {
|
||||||
public void engage();
|
public void engage();
|
||||||
}
|
}
|
||||||
|
|
||||||
class FuelEngine : Engine {
|
class FuelEngine : Engine {
|
||||||
public void engage() {
|
public void engage() {
|
||||||
writeln("VROOOOOOM!");
|
writeln("VROOOOOOM!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ElectricEngine : Engine {
|
class ElectricEngine : Engine {
|
||||||
public void engage() {
|
public void engage() {
|
||||||
writeln("hummmmmmmm....");
|
writeln("hummmmmmmm....");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HybridCar {
|
class HybridCar {
|
||||||
alias KilometersPerHour = int;
|
alias KilometersPerHour = int;
|
||||||
|
|
||||||
@Autowire!FuelEngine
|
@Autowire!FuelEngine
|
||||||
public Engine fuelEngine;
|
public Engine fuelEngine;
|
||||||
|
|
||||||
@Autowire!ElectricEngine
|
@Autowire!ElectricEngine
|
||||||
public Engine electricEngine;
|
public Engine electricEngine;
|
||||||
|
|
||||||
public void moveAtSpeed(KilometersPerHour speed) {
|
public void moveAtSpeed(KilometersPerHour speed) {
|
||||||
if (speed <= 45) {
|
if (speed <= 45) {
|
||||||
electricEngine.engage();
|
electricEngine.engage();
|
||||||
} else {
|
} else {
|
||||||
fuelEngine.engage();
|
fuelEngine.engage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
auto container = DependencyContainer.getInstance();
|
auto dependencies = DependencyContainer.getInstance();
|
||||||
|
|
||||||
container.register!HybridCar;
|
dependencies.register!HybridCar;
|
||||||
container.register!(Engine, FuelEngine);
|
dependencies.register!(Engine, FuelEngine);
|
||||||
container.register!(Engine, ElectricEngine);
|
dependencies.register!(Engine, ElectricEngine);
|
||||||
|
|
||||||
auto car = container.resolve!HybridCar;
|
auto car = dependencies.resolve!HybridCar;
|
||||||
|
|
||||||
car.moveAtSpeed(10); // Should print "hummmmmmmm...."
|
car.moveAtSpeed(10); // Should print "hummmmmmmm...."
|
||||||
car.moveAtSpeed(50); // Should print "VROOOOOOM!"
|
car.moveAtSpeed(50); // Should print "VROOOOOOM!"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
import poodinis.dependency;
|
import poodinis.dependency;
|
||||||
|
|
||||||
interface Database{};
|
interface Database{};
|
||||||
class RelationalDatabase : Database {}
|
class RelationalDatabase : Database {}
|
||||||
|
|
||||||
class DataWriter {
|
class DataWriter {
|
||||||
@Autowire
|
@Autowire
|
||||||
public Database database; // Automatically injected when class is resolved
|
public Database database; // Automatically injected when class is resolved
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
auto container = DependencyContainer.getInstance();
|
auto dependencies = DependencyContainer.getInstance();
|
||||||
container.register!DataWriter;
|
dependencies.register!DataWriter;
|
||||||
container.register!(Database, RelationalDatabase);
|
dependencies.register!(Database, RelationalDatabase);
|
||||||
|
|
||||||
auto writer = container.resolve!DataWriter;
|
auto writer = dependencies.resolve!DataWriter;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue