Skip to content

Commit

Permalink
refactor: refactor the DI container: replace methods bindAs{Class|Fun…
Browse files Browse the repository at this point in the history
…ction} with bind

Replace methods bindAsClass, bindAsFunction with a single bind method in the DI Container. Add new
methods 'instance' and 'value' for binding class instances and arbitrary values respectively
  • Loading branch information
simplymichael committed Jun 17, 2024
1 parent 54cc1d3 commit 30f0f07
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = function createApp({ webRouter, apiRouter }) {
* - req.app.bindWithFunction(dependencyKey, implementationFunction, params)
* - const value = req.app.resolve(dependencyKey)
*/
for(const prop of ["bindWithClass", "bindWithFunction", "resolve"]) {
for(const prop of ["bind", "instance", "value", "resolve"]) {
if(!(prop in app)) {
app[prop] = container[prop].bind(container);
}
Expand Down
2 changes: 1 addition & 1 deletion src/app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ module.exports = {
const app = createApp({ webRouter: function() {} });

const expressAppMethods = ["get", "listen", "set", "use"];
const diContainerMethods = ["bindWithClass", "bindWithFunction", "resolve"];
const diContainerMethods = ["bind", "instance", "value", "resolve"];

for(const method of expressAppMethods) {
expect(app).to.have.property(method).to.be.a("function");
Expand Down
56 changes: 37 additions & 19 deletions src/framework/component/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

const awilix = require("awilix");
const { asValue, asFunction } = awilix;

const container = awilix.createContainer({
injectionMode: awilix.InjectionMode.PROXY,
Expand All @@ -13,31 +14,48 @@ const container = awilix.createContainer({


module.exports = {
bindWithClass(key, value, params) {
let resolver = awilix.asClass(value);

if(params) {
resolver = resolver.inject(() => params);
}

container.register({
[key]: resolver
});
bind(name, resolver) {
return container.register(name, asFunction(function bind() {
return resolver(container);
}));
},

bindWithFunction(key, value, params) {
let resolver = awilix.asFunction(value);

if(params) {
resolver = resolver.inject(() => params);
}
instance(name, object) {
return container.register(name, asValue(object));
},

container.register({
[key]: resolver,
});
value(name, val) {
return container.register(name, asValue(val));
},

resolve(key) {
return container.resolve(key);
},
};


/*
function bindWithClass(key, value, params) {
let resolver = awilix.asClass(value);
if(params) {
resolver = resolver.inject(() => params);
}
container.register({
[key]: resolver
});
}
function bindWithFunction(key, value, params) {
let resolver = awilix.asFunction(value);
if(params) {
resolver = resolver.inject(() => params);
}
container.register({
[key]: resolver,
});
}
*/
4 changes: 2 additions & 2 deletions src/service-providers/app-service-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AppServiceProvider extends ServiceProvider {
const container = this.container();
const config = this.config();

container.bindWithFunction("config", function configGetter() {
container.bind("config", function configGetter() {
return config;
});

Expand All @@ -43,7 +43,7 @@ class AppServiceProvider extends ServiceProvider {
* value will be that object, otherwise, value will be whatever is passed as
* defaultValue.
*/
container.bindWithFunction("registry", function createRegistry() {
container.bind("registry", function createRegistry() {
return createObjectStore();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/service-providers/app-service-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
expect(sp).to.be.an("object");
expect(sp).to.have.property("container").to.be.a("function");

for(const method of ["bindWithClass", "bindWithFunction", "resolve"]) {
for(const method of ["bind", "instance", "value", "resolve"]) {
expect(sp.container()).to.have.property(method).to.be.a("function");
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/service-providers/cache-service-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = class CacheServiceProvider extends ServiceProvider {
/*
* Bind the default cache to the container
*/
container.bindWithFunction("cache", function createCache() {
container.bind("cache", function createCache() {
return cache;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/service-providers/cache-service-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {
expect(sp).to.be.an("object");
expect(sp).to.have.property("container").to.be.a("function");

for(const method of ["bindWithClass", "bindWithFunction", "resolve"]) {
for(const method of ["bind", "instance", "value", "resolve"]) {
expect(sp.container()).to.have.property(method).to.be.a("function");
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/service-providers/service-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
expect(sp).to.be.an("object");
expect(sp).to.have.property("container").to.be.a("function");

for(const method of ["bindWithClass", "bindWithFunction", "resolve"]) {
for(const method of ["bind", "instance", "value", "resolve"]) {
expect(sp.container()).to.have.property(method).to.be.a("function");
}
});
Expand Down

0 comments on commit 30f0f07

Please sign in to comment.