Big Bang
This commit is contained in:
commit
da706a9c5a
12
App.js
Normal file
12
App.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
const Injectable = require('./Injectable')
|
||||||
|
|
||||||
|
class App extends Injectable {
|
||||||
|
static services = ['emailLogging']
|
||||||
|
run() {
|
||||||
|
setInterval(() => {
|
||||||
|
this.emailLogging.error('Haha made ya\' look!')
|
||||||
|
}, 5000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = exports = App
|
14
DependencyInjector.js
Normal file
14
DependencyInjector.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
const ServiceContainer = require('./ServiceContainer.js')
|
||||||
|
|
||||||
|
class DependencyInjector {
|
||||||
|
constructor() {
|
||||||
|
this.container = new ServiceContainer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Injects the dependencies into an uninstantiated class
|
||||||
|
make(Class) {
|
||||||
|
return Class.__inject(this.container)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = exports = DependencyInjector
|
11
Injectable.js
Normal file
11
Injectable.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
class Injectable {
|
||||||
|
static services = []
|
||||||
|
|
||||||
|
static __inject(container) {
|
||||||
|
this.services.forEach(serviceName => {
|
||||||
|
this.prototype[serviceName] = container.getService(serviceName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = exports = Injectable
|
6
README.md
Normal file
6
README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Basic Dependency Injection
|
||||||
|
## In less than 100 lines of pure, ES6 JavaScript
|
||||||
|
|
||||||
|
This is a very bare-bones dependency injector implementation based on the ES6 classes. It's meant to illustrate the feasibility and benefits of using a service-based dependency injector for Node.js/JavaScript applications.
|
||||||
|
|
||||||
|
I discuss this project in greater depth in my blog post [here](https://garrettmills.dev/blog).
|
5
Service.js
Normal file
5
Service.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class Service {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = exports = Service
|
28
ServiceContainer.js
Normal file
28
ServiceContainer.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
const LogHelper = require('./LogHelper')
|
||||||
|
const EmailLogHelper = require('./EmailLogHelper')
|
||||||
|
|
||||||
|
class ServiceContainer {
|
||||||
|
constructor() {
|
||||||
|
// We define the service classes here, but we won't
|
||||||
|
// instantiate them until they're needed.
|
||||||
|
this.definitions = {
|
||||||
|
logging: LogHelper,
|
||||||
|
emailLogging: EmailLogHelper,
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is where the container will store service instances
|
||||||
|
// so they can be reused when requested.
|
||||||
|
this.instances = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
getService(serviceName) {
|
||||||
|
// Create a service instance if one doesn't already exist.
|
||||||
|
if ( !this.instances[serviceName] ) {
|
||||||
|
const ServiceClass = this.definitions[serviceName]
|
||||||
|
this.instances[serviceName] = new ServiceClass()
|
||||||
|
}
|
||||||
|
return this.instances[serviceName]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = exports = ServiceContainer
|
9
index.js
Normal file
9
index.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// Create the dependency injector instance for the application
|
||||||
|
const DI = require('./DependencyInjector')
|
||||||
|
const di = new DI()
|
||||||
|
|
||||||
|
// Now, create the instance of our application using the DI to inject services
|
||||||
|
const App = di.make(require('./App'))
|
||||||
|
const app = new App() // Has access to the emailLogging service
|
||||||
|
|
||||||
|
app.run();
|
Loading…
Reference in New Issue
Block a user