|
@@ -74,20 +74,28 @@ namespace Inversion {
|
|
|
return register_local_factory_type(typeof(T), factory, lifecycle);
|
|
return register_local_factory_type(typeof(T), factory, lifecycle);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public Registration register_local_type(Type implementation_type, owned FactoryDelegate factory_func, Lifecycle lifecycle) requires (lifecycle != Lifecycle.SINGLETON) {
|
|
|
|
|
|
|
+ public Registration register_local_type(Type implementation_type, owned FactoryDelegate? factory_func, Lifecycle lifecycle) requires (lifecycle != Lifecycle.SINGLETON) {
|
|
|
|
|
+ if(factory_func == null) {
|
|
|
|
|
+ var object_factory = new ObjectFactory(implementation_type);
|
|
|
|
|
+ return register_local_factory_type(implementation_type, object_factory, lifecycle);
|
|
|
|
|
+ }
|
|
|
var delegate_factory = new DelegateFactory((owned)factory_func);
|
|
var delegate_factory = new DelegateFactory((owned)factory_func);
|
|
|
return register_local_factory_type(implementation_type, delegate_factory, lifecycle);
|
|
return register_local_factory_type(implementation_type, delegate_factory, lifecycle);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public Registration register_local<T>(owned FactoryDelegate factory_func, Lifecycle lifecycle) requires (lifecycle != Lifecycle.SINGLETON) {
|
|
|
|
|
- return register_local_type(typeof(T), (owned)factory_func, lifecycle);
|
|
|
|
|
|
|
+ public Registration register_local<T>(owned TypedFactoryDelegate<T>? factory_func = null, Lifecycle lifecycle = Lifecycle.SCOPED) requires (lifecycle != Lifecycle.SINGLETON) {
|
|
|
|
|
+ if(factory_func == null) {
|
|
|
|
|
+ return register_local_type(typeof(T), null, lifecycle);
|
|
|
|
|
+ }
|
|
|
|
|
+ var delegate_factory = DelegateFactory.typed<T>((owned)factory_func);
|
|
|
|
|
+ return register_local_factory_type(typeof(T), delegate_factory, lifecycle);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public Registration register_local_transient<T>(owned FactoryDelegate factory_func) {
|
|
|
|
|
|
|
+ public Registration register_local_transient<T>(owned TypedFactoryDelegate<T>? factory_func = null) {
|
|
|
return register_local<T>((owned)factory_func, Lifecycle.TRANSIENT);
|
|
return register_local<T>((owned)factory_func, Lifecycle.TRANSIENT);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public Registration register_local_scoped<T>(owned FactoryDelegate factory_func) {
|
|
|
|
|
|
|
+ public Registration register_local_scoped<T>(owned TypedFactoryDelegate<T>? factory_func = null) {
|
|
|
return register_local<T>((owned)factory_func, Lifecycle.SCOPED);
|
|
return register_local<T>((owned)factory_func, Lifecycle.SCOPED);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -141,20 +149,26 @@ namespace Inversion {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private Object resolve_registration_as(Registration registration, Type requested_type) throws Error {
|
|
private Object resolve_registration_as(Registration registration, Type requested_type) throws Error {
|
|
|
|
|
+ Object result = null;
|
|
|
switch (registration.lifecycle) {
|
|
switch (registration.lifecycle) {
|
|
|
case Lifecycle.SINGLETON:
|
|
case Lifecycle.SINGLETON:
|
|
|
- return container.get_or_create_singleton(registration, requested_type);
|
|
|
|
|
|
|
+ result = container.get_or_create_singleton(registration, requested_type);
|
|
|
|
|
+ break;
|
|
|
|
|
|
|
|
case Lifecycle.SCOPED:
|
|
case Lifecycle.SCOPED:
|
|
|
case Lifecycle.TRANSIENT:
|
|
case Lifecycle.TRANSIENT:
|
|
|
default:
|
|
default:
|
|
|
- Object instance;
|
|
|
|
|
- if (!this.scoped_instances.try_get(registration, out instance)) {
|
|
|
|
|
- instance = create_with_injection_context(this, requested_type, registration);
|
|
|
|
|
- this.scoped_instances.set(registration, instance);
|
|
|
|
|
|
|
+ if (!this.scoped_instances.try_get(registration, out result)) {
|
|
|
|
|
+ result = create_with_injection_context(this, requested_type, registration);
|
|
|
|
|
+ this.scoped_instances.set(registration, result);
|
|
|
}
|
|
}
|
|
|
- return instance;
|
|
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(!result.get_type().is_a(requested_type)) {
|
|
|
|
|
+ throw new ContainerError.INCOMPATIBLE_TYPE(@"Instansiated object has type $(result.get_type().name()) which is incompatible with the requested type $(requested_type.name())");
|
|
|
}
|
|
}
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|