Skip to content

Commit 1bf7618

Browse files
committed
JavaScript dependency injection implementation
1 parent 8496ff5 commit 1bf7618

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.log
3+
.DS_Store

JavaScript/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## Pependency Injection
2+
3+
Purpose: learn how to create sandboxed context for modues to separate them and
4+
minimize cross-modules code coupling, extracting at least two abstraction layers
5+
(applied and system) and how to execute applied code in virtual environment,
6+
changing its behavior using IoC from system layer.
7+
8+
## Files
9+
10+
* `framework.js` - small piece of the framework, just to demonstrate IoC
11+
* `application.js` - small piece of the application, also for IoC demonstration
12+
13+
## How to execute
14+
15+
From the command line, type: `node ./framework.js` or `node framework`
16+
17+
## Tasks
18+
19+
You may select at least one of the following tasks, make a fork of this
20+
repository and implement your solution there. If those tasks are simple
21+
for somebody, please see additional tasks below.
22+
23+
1. Add `setTimeout` and `setInterval` to the application context and use them
24+
printing something from the timer function using `console.log()`
25+
26+
2. Inject a link to `util` library into the application context and make a few
27+
calls to its functions from applied code
28+
29+
3. Implement the ability to run different applications inside framework, using
30+
command line option, e.g.: `node framework <applicationName>`
31+
32+
4. Wrap or intercept `console.log()` call to add more info into console output
33+
in the following format: `<applicationName> <time> <message>`
34+
35+
5. Wrap or intercept `console.log()` in the sandboxed application logging all
36+
console output into a file in the format: `<applicationName> <time> <message>`
37+
38+
6. Give a link to `require` function to the application, add call to it and
39+
wrap it for logging to a file in the format: `<time> <module name>`
40+
41+
7. Export a hash from `application.js` with multiple functions and variables,
42+
print the list with types from framework
43+
44+
8. Export a function from `application.js` and print its parameter count and
45+
source code from the framework
46+
47+
9. Print a list of everything from the application global context (application
48+
sandbox) with the data types specified
49+
50+
10. Compare an application sandboxed context keys before application loaded and
51+
after, print it from the framework and find a difference (keys added / deleted)
52+
53+
## Additional tasks
54+
55+
11. You can combine several tasks (listed above) in your code, implement a more
56+
complex example of interaction between framework and application, preparing
57+
run-time environment (sandbox) and/or improving CLI (command line interface)
58+
59+
12. Implement a similar example in another programming language
60+
61+
13. Improve and/or optimize Impress Application Server code, specifically
62+
everything related to sandboxing, see:
63+
[/lib/impress.application.js](https://github.com/tshemsedinov/impress/blob/master/lib/impress.application.js)
64+
65+
14. Use IoC and code isolation principles using sandboxed context and/or other
66+
similar technique in your projects (any technological stack) to demonstrate its
67+
use and the effect of such use

JavaScript/application.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
// File contains a small piece of the source to demonstrate main module
4+
// of a sample application to be executed in the sandboxed context by
5+
// another pice of code from `framework.js`. Read README.md for tasks.
6+
7+
// Print from the global context of application module
8+
console.log('From application global context');
9+
10+
module.exports = function() {
11+
// Print from the exported function context
12+
console.log('From application exported function');
13+
};

JavaScript/framework.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
// Example showing us how the framework creates an environment (sandbox) for
4+
// appication runtime, load an application code and passes a sandbox into app
5+
// as a global context and receives exported application interface
6+
7+
// The framework can require core libraries
8+
global.api = {};
9+
api.fs = require('fs');
10+
api.vm = require('vm');
11+
12+
// Create a hash and turn it into the sandboxed context which will be
13+
// the global context of an application
14+
let context = { module: {}, console: console };
15+
context.global = context;
16+
let sandbox = api.vm.createContext(context);
17+
18+
// Read an application source code from the file
19+
let fileName = './application.js';
20+
api.fs.readFile(fileName, (err, src) => {
21+
// We need to handle errors here
22+
23+
// Run an application in sandboxed context
24+
let script = api.vm.createScript(src, fileName);
25+
script.runInNewContext(sandbox);
26+
27+
// We can access a link to exported interface from sandbox.module.exports
28+
// to execute, save to the cache, print to console, etc.
29+
});

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# DependencyInjection
1+
# Dependency Injection
22
Resolving dependencies with dependency injection

0 commit comments

Comments
 (0)