Open jChicote opened 9 months ago
https://obarguti.medium.com/creating-a-custom-inversion-of-control-container-ce6f2a3dc64 https://flexiple.com/javascript/javascript-dictionary https://betterprogramming.pub/understandable-dependency-injection-in-javascript-fab97062c34c https://blog.appsignal.com/2022/02/16/dependency-injection-in-javascript-write-testable-code-easily.html
class Container {
constructor() {
this.dependencies = new Map();
this.instances = new Map();
}
register(name, dependency, options = { singleton: false }) {
if (!this.dependencies.has(name)) {
this.dependencies.set(name, {
dependency,
options,
});
}
}
resolve(ClassToResolve) {
if (this.instances.has(ClassToResolve)) {
return this.instances.get(ClassToResolve);
}
const dependencies = ClassToResolve.inject || [];
const resolvedDependencies = dependencies.map((dependency) =>
this.resolve(dependency)
);
const { dependency, options } = this.dependencies.get(ClassToResolve.name);
if (options.singleton) {
const instance = new dependency(...resolvedDependencies);
this.instances.set(ClassToResolve, instance);
return instance;
}
return new dependency(...resolvedDependencies);
}
}
class Logger {
log(message) {
console.log(message);
}
}
class UserService {
static inject = ['Logger'];
constructor(logger) {
this.logger = logger;
}
getUser() {
this.logger.log('Getting user...');
return 'John Doe';
}
}
const container = new Container();
container.register('Logger', Logger, { singleton: true });
container.register('UserService', UserService);
const userService = container.resolve(UserService);
userService.getUser(); // This will log "Getting user..."
Description
The PlanetController and presentation will manage the invocation of the Gateways so that no game system interacts directly with the APIGateways. Their main responsibility is to bundle the functionality of anything related to planets.
Acceptance Criteria
Implementation
Controller
Add GetMainPlanetsQuery
Add PlanetViewModel
Presenter
Unit Tests: