Skip to content

Commit

Permalink
refactor: refactor the framework container: add method to bind a clas…
Browse files Browse the repository at this point in the history
…s to an instance
  • Loading branch information
simplymichael committed Jun 21, 2024
1 parent 561ab2d commit 0b43dd8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
76 changes: 43 additions & 33 deletions src/framework/component/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

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

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


module.exports = {
/**
* Bind a value using a resolver function.
* The resolver function receives the container as parameter
* and should return the value to be bound inside the container.
*
* @param {String} name: The name to associate with the bound data.
* @param {Function} resolver: The resolver function that returns the data to bind.
*/
bind(name, resolver) {
return container.register(name, asFunction(function bind() {
container.register(name, asFunction(function bind() {
return resolver(container);
}));
},

instance(name, object) {
return container.register(name, asValue(object));
return this;
},

value(name, val) {
return container.register(name, asValue(val));
},
/**
* Bind already instantiated object inside the container.
*
* @param {String} name: The name to associate with the class instance
* @param {Object} instance: The instance we want to bind.
*/
instance(name, instance) {
container.register(name, asValue(instance));

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


/*
function bindWithClass(key, value, params) {
let resolver = awilix.asClass(value);

if(params) {
resolver = resolver.inject(() => params);
}
/**
* Bind class instance inside the container.
*
* @param {String} name: The name to associate with the class instance
* @param {Object} className: The class whose instance we want to bind.
*/
instantiate(name, className) {
container.register(name, asClass(className));

container.register({
[key]: resolver
});
}
return this;
},

function bindWithFunction(key, value, params) {
let resolver = awilix.asFunction(value);
/**
* Bind an arbitrary value inside the container.
* @param {String} name: The name to associate with the bound value.
* @param {Mixed} value: The value to be bound.
*/
value(name, value) {
container.register(name, asValue(value));

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

container.register({
[key]: resolver,
});
}
*/
resolve(key) {
return container.resolve(key);
},
};
6 changes: 2 additions & 4 deletions src/service-providers/app-service-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ServiceProvider = require("./service-provider");
* Extending the parent `ServiceContainer` gives us access to
* the DI Container via `this.container()` method.
*/
class AppServiceProvider extends ServiceProvider {
module.exports = class AppServiceProvider extends ServiceProvider {
constructor(config) {
super(config);
}
Expand Down Expand Up @@ -47,6 +47,4 @@ class AppServiceProvider extends ServiceProvider {
return createObjectStore();
});
}
}

module.exports = AppServiceProvider;
};
6 changes: 2 additions & 4 deletions src/service-providers/service-provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const FrameworkServiceProvider = require("../framework/component/service-provider");

class ServiceProvider extends FrameworkServiceProvider {
module.exports = class ServiceProvider extends FrameworkServiceProvider {
#config;

constructor(config) {
Expand All @@ -12,6 +12,4 @@ class ServiceProvider extends FrameworkServiceProvider {
config() {
return this.#config;
}
}

module.exports = ServiceProvider;
};

0 comments on commit 0b43dd8

Please sign in to comment.