Introduce an interception mechanism
See original GitHub issueIt would be nice to have an interception mechanism similar to the $http interception from angular.js, but based on any services working together with the injection api. This would allow to introduce service behaviors based on aspect oriented programming, for example authorization, exception handling, logging etc.
As a reference I would like to mention the Unity dependency injection framework for .Net: https://msdn.microsoft.com/en-us/library/dn178466(v=pandp.30).aspx
I could imagine that working with interceptors looks like following snippets:
Interceptor implementation
class MyInterceptionBehavior implements InterceptionBehavior {
intercept(input:FunctionInvocation, getNext:GetNextCallback) : FunctionReturn {
let result: FunctionReturn;
// do something before calling the next interceptor or the final implementation
result = getNext()(input, getNext);
// do something after calling the next interceptor or the final implementation
return result;
}
}
Interceptor registration based in decorators
@Intercept({
behavior: MyInterceptionBehavior,
order: 1
})
class MyService {
@Intercept({
behavior: MyInterceptionBehavior,
order: 2
})
doSomething(): void {
}
}
And also manual interceptor registration for types which are out of my control, e.g. Router or Http
bind(MyService).toFactory(() => ....).interceptWith(MyInterceptor)
The interception API from Unity has one drawback. It does not support interface interception without a target implementation. But this would also be nice to have. Imagine I have several service interface where I can provide a generic implementation without to be forced implementing every single interface in the same way. An example usage could be a service proxy. May be this could look like following:
bind(MyService).toInterceptor(MyInterfaceInterceptor)
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (7 by maintainers)
@jeffbcross I would rather compare it to the connect module of Node JS than with the provider of $http interceptors API in Angular 1.
The interceptor API i’m proposing would create a proxy around the service. The proxy has a pipeline of interceptors (similar to the middleware pipeline in Connect) which gets executed during every method and property call of the service. By calling
result = getNext()(input, getNext);
the interceptor will invoke the next interceptor in the pipeline or, in case it is the last interceptor, the original service function.An interceptor would not get fix defined parameter like req, resp or $http, but the metadata of the called service and method. This would include information about the parameter types, values return types, values etc. I called this FunctionInvocation in my sample above.
This would allow us, to attach behaviors (which have to be written only once) to any service (not only $http), which is very powerful. I consider this as very valuable for large enterprise applications.
This is obsolete, and no longer relevant / actionable. To keep our issues clean, we are aggressively closing them.