Dependency Injection and AOP
Last updated
Was this helpful?
Last updated
Was this helpful?
in JavaScript might seem to be a weird topic to solve at first look. Anyway, dependency injection in JavaScript might be explained to be useful with the following sentence, "We want to be able to inject code based on a key, rather than referencing a file that contains the code we want to use.". When we reference a code, like a class or a method, by a key, we can easily reference code and thus also switch the code. There might be also benefits when it becomes to testing and refactoring.
We will get some kind of AOP when are implemented in JavaScript. Anyway, I am not sure if we are going to be able to solve the following. I want to be able to hook at some code execution based on naming conventions. Lets say, I want to measure performance of all methods in my controller. I want to be able to define something like this.
The code that would do it would look like this. The pointcut definitions would be somewhere around the code that starts up the server, because it needs to be passed to a library that will create a context, that contains all the code that can be referenced by keys.
The library is just an experiment, to think about dependency injection and aspect oriented programming using pointcuts in JavaScript and NodeJS.
Now I want to be able to pass these to my application. Lets say we can use a library di-and-aop that will instantiate our code and apply our pointcuts to the code execution.
Full example of this application is available on .
I have created a library named that takes componentScan
and pointcuts
variables and creates a context that contains all the code references. When we can call getContext()
method, we get our component and we can start up the server. app
component, which we obtain from context, is actually an instnace of koa server.