mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add a constuction handler
This commit is contained in:
parent
55726e84db
commit
3b11c4aec4
|
@ -42,8 +42,14 @@ class AClass {
|
||||||
|
|
||||||
public void main() {
|
public void main() {
|
||||||
auto container = new shared DependencyContainer();
|
auto container = new shared DependencyContainer();
|
||||||
container.register!ADependency;
|
container.register!(ADependency).onConstructed((Object obj) {
|
||||||
container.register!AClass;
|
writeln("ADependency constructed");
|
||||||
|
});
|
||||||
|
|
||||||
|
container.register!(AClass).onConstructed((Object obj) {
|
||||||
|
writeln("AClass constructed");
|
||||||
|
});
|
||||||
|
|
||||||
auto instance = container.resolve!AClass; // Will cause the post constructor to be called.
|
auto instance = container.resolve!AClass; // Will cause the post constructor to be called.
|
||||||
container.removeRegistration!AClass; // Will cause the pre destructor to be called.
|
container.removeRegistration!AClass; // Will cause the pre destructor to be called.
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import std.stdio;
|
||||||
|
|
||||||
alias CreatesSingleton = Flag!"CreatesSingleton";
|
alias CreatesSingleton = Flag!"CreatesSingleton";
|
||||||
alias InstanceFactoryMethod = Object delegate();
|
alias InstanceFactoryMethod = Object delegate();
|
||||||
|
alias InstanceEventHandler = void delegate(Object instance);
|
||||||
|
|
||||||
class InstanceCreationException : Exception {
|
class InstanceCreationException : Exception {
|
||||||
this(string message, string file = __FILE__, size_t line = __LINE__) {
|
this(string message, string file = __FILE__, size_t line = __LINE__) {
|
||||||
|
@ -39,6 +40,7 @@ struct InstanceFactoryParameters {
|
||||||
class InstanceFactory {
|
class InstanceFactory {
|
||||||
private Object instance = null;
|
private Object instance = null;
|
||||||
private InstanceFactoryParameters _factoryParameters;
|
private InstanceFactoryParameters _factoryParameters;
|
||||||
|
private InstanceEventHandler _constructionHandler;
|
||||||
|
|
||||||
this() {
|
this() {
|
||||||
factoryParameters = InstanceFactoryParameters();
|
factoryParameters = InstanceFactoryParameters();
|
||||||
|
@ -75,9 +77,17 @@ class InstanceFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
instance = _factoryParameters.factoryMethod();
|
instance = _factoryParameters.factoryMethod();
|
||||||
|
if(_constructionHandler !is null) {
|
||||||
|
_constructionHandler(instance);
|
||||||
|
}
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onConstructed(InstanceEventHandler handler) {
|
||||||
|
_constructionHandler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
private void printDebugUseExistingInstance() {
|
private void printDebugUseExistingInstance() {
|
||||||
if (_factoryParameters.instanceType !is null) {
|
if (_factoryParameters.instanceType !is null) {
|
||||||
writeln(format("DEBUG: Existing instance returned of type %s", _factoryParameters.instanceType.toString()));
|
writeln(format("DEBUG: Existing instance returned of type %s", _factoryParameters.instanceType.toString()));
|
||||||
|
|
|
@ -60,7 +60,6 @@ class Registration {
|
||||||
return linkedRegistration.getInstance(context);
|
return linkedRegistration.getInstance(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (instanceFactory is null) {
|
if (instanceFactory is null) {
|
||||||
throw new InstanceCreationException("No instance factory defined for registration of type " ~ registeredType.toString());
|
throw new InstanceCreationException("No instance factory defined for registration of type " ~ registeredType.toString());
|
||||||
}
|
}
|
||||||
|
@ -72,6 +71,12 @@ class Registration {
|
||||||
this.linkedRegistration = registration;
|
this.linkedRegistration = registration;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Registration onConstructed(InstanceEventHandler handler) {
|
||||||
|
if(instanceFactory !is null)
|
||||||
|
instanceFactory.onConstructed(handler);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue