Refactored constructing from container to the classes

This commit is contained in:
Mark van Renswoude 2021-08-17 21:38:20 +02:00
parent dc8d00fdc8
commit 773ee7db08
9 changed files with 50 additions and 13 deletions

View File

@ -21,14 +21,14 @@ logger.add(new winston.transports.Console({
const container = new Container(); const container = new Container();
container.register('Logger', c => logger); container.registerInstance('Logger', logger);
container.register('AsyncFs', c => new AsyncFs()); container.registerType('AsyncFs', AsyncFs);
container.register('DateTimeProvider', c => new DateTimeProvider()); container.registerType('DateTimeProvider', DateTimeProvider);
container.register('ApiRoutes', c => new ApiRoutes(c.Logger, c.NotificationFacade, c.Config)); container.registerType('ApiRoutes', ApiRoutes);
container.register('NotificationRepository', c => new NotificationRepository(c.Logger, c.DateTimeProvider, c.AsyncFs, c.Config)); container.registerType('NotificationRepository', NotificationRepository);
container.register('NotificationFacade', c => new NotificationFacade(c.Logger, c.DateTimeProvider, c.TransportProvider, c.SubjectParser, c.NotificationRepository, c.Config)); container.registerType('NotificationFacade', NotificationFacade);
container.register('TransportProvider', c => new TransportProvider()); container.registerType('TransportProvider', TransportProvider);
container.register('SubjectParser', c => new SubjectParser()); container.registerType('SubjectParser', SubjectParser);
async function asyncMain() async function asyncMain()
@ -47,7 +47,7 @@ async function asyncMain()
} }
container.register('Config', c => config); container.registerInstance('Config', config);
await container.NotificationRepository.init(); await container.NotificationRepository.init();
@ -57,7 +57,7 @@ async function asyncMain()
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: false })); app.use(express.urlencoded({ extended: false }));
app.use('/api', container.ApiRoutes.create(express)); app.use('/api', container.ApiRoutes.createRouter(express));
app.use('/', express.static('frontend/dist')); app.use('/', express.static('frontend/dist'));
app.listen(config.port, () => app.listen(config.port, () =>

View File

@ -3,6 +3,9 @@ const fs = require('fs').promises;
class AsyncFs class AsyncFs
{ {
static create = () => new this();
async exists(path) async exists(path)
{ {
try try

View File

@ -1,5 +1,5 @@
/* /*
* Original concept and code by Magnus Tovslid: * Modified version of code by Magnus Tovslid:
* https://medium.com/@magnusjt/ioc-container-in-nodejs-e7aea8a89600 * https://medium.com/@magnusjt/ioc-container-in-nodejs-e7aea8a89600
*/ */
class Container class Container
@ -9,7 +9,22 @@ class Container
this.services = {}; this.services = {};
} }
register(name, factory)
// Assumes the class has a static method called create which accepts
// a Container style object
registerType(name, type)
{
this.registerFactory(name, c => type.create(c));
}
registerInstance(name, instance)
{
this.registerFactory(name, () => instance);
}
registerFactory(name, factory)
{ {
Object.defineProperty(this, name, { Object.defineProperty(this, name, {
get: () => get: () =>

View File

@ -3,6 +3,9 @@ const { DateTime } = require('luxon');
class DateTimeProvider class DateTimeProvider
{ {
static create = () => new this();
unixTimestamp() unixTimestamp()
{ {
return Math.floor(DateTime.now().toSeconds()); return Math.floor(DateTime.now().toSeconds());

View File

@ -1,5 +1,9 @@
class NotificationFacade class NotificationFacade
{ {
static create = container => new this(container.Logger, container.DateTimeProvider, container.TransportProvider,
container.SubjectParser, container.NotificationRepository, container.Config);
constructor(logger, dateTimeProvider, transportProvider, subjectParser, notificationRepository, config) constructor(logger, dateTimeProvider, transportProvider, subjectParser, notificationRepository, config)
{ {
this.logger = logger; this.logger = logger;

View File

@ -4,6 +4,9 @@ const crypto = require('crypto');
class NotificationRepository class NotificationRepository
{ {
static create = container => new this(container.Logger, container.DateTimeProvider, container.AsyncFs, container.Config);
constructor(logger, dateTimeProvider, asyncFs, config) constructor(logger, dateTimeProvider, asyncFs, config)
{ {
this.logger = logger; this.logger = logger;

View File

@ -1,5 +1,8 @@
class ApiRoutes class ApiRoutes
{ {
static create = container => new this(container.logger, container.NotificationFacade, container.Config);
constructor(logger, notificationFacade, config) constructor(logger, notificationFacade, config)
{ {
this.logger = logger; this.logger = logger;
@ -8,7 +11,7 @@ class ApiRoutes
} }
create(express) createRouter(express)
{ {
const router = express.Router(); const router = express.Router();

View File

@ -1,5 +1,8 @@
class SubjectParser class SubjectParser
{ {
static create = () => new this();
parse(subject) parse(subject)
{ {
// Possible formats: // Possible formats:

View File

@ -7,6 +7,9 @@ const transportMap = {
class TransportProvider class TransportProvider
{ {
static create = () => new this();
byType(type) byType(type)
{ {
return transportMap.hasOwnProperty(type) ? transportMap[type] : null; return transportMap.hasOwnProperty(type) ? transportMap[type] : null;