Refactored constructing from container to the classes
This commit is contained in:
parent
dc8d00fdc8
commit
773ee7db08
20
index.js
20
index.js
@ -21,14 +21,14 @@ logger.add(new winston.transports.Console({
|
||||
|
||||
|
||||
const container = new Container();
|
||||
container.register('Logger', c => logger);
|
||||
container.register('AsyncFs', c => new AsyncFs());
|
||||
container.register('DateTimeProvider', c => new DateTimeProvider());
|
||||
container.register('ApiRoutes', c => new ApiRoutes(c.Logger, c.NotificationFacade, c.Config));
|
||||
container.register('NotificationRepository', c => new NotificationRepository(c.Logger, c.DateTimeProvider, c.AsyncFs, c.Config));
|
||||
container.register('NotificationFacade', c => new NotificationFacade(c.Logger, c.DateTimeProvider, c.TransportProvider, c.SubjectParser, c.NotificationRepository, c.Config));
|
||||
container.register('TransportProvider', c => new TransportProvider());
|
||||
container.register('SubjectParser', c => new SubjectParser());
|
||||
container.registerInstance('Logger', logger);
|
||||
container.registerType('AsyncFs', AsyncFs);
|
||||
container.registerType('DateTimeProvider', DateTimeProvider);
|
||||
container.registerType('ApiRoutes', ApiRoutes);
|
||||
container.registerType('NotificationRepository', NotificationRepository);
|
||||
container.registerType('NotificationFacade', NotificationFacade);
|
||||
container.registerType('TransportProvider', TransportProvider);
|
||||
container.registerType('SubjectParser', SubjectParser);
|
||||
|
||||
|
||||
async function asyncMain()
|
||||
@ -47,7 +47,7 @@ async function asyncMain()
|
||||
}
|
||||
|
||||
|
||||
container.register('Config', c => config);
|
||||
container.registerInstance('Config', config);
|
||||
|
||||
|
||||
await container.NotificationRepository.init();
|
||||
@ -57,7 +57,7 @@ async function asyncMain()
|
||||
app.use(express.json());
|
||||
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.listen(config.port, () =>
|
||||
|
@ -3,6 +3,9 @@ const fs = require('fs').promises;
|
||||
|
||||
class AsyncFs
|
||||
{
|
||||
static create = () => new this();
|
||||
|
||||
|
||||
async exists(path)
|
||||
{
|
||||
try
|
||||
|
@ -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
|
||||
*/
|
||||
class Container
|
||||
@ -9,7 +9,22 @@ class Container
|
||||
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, {
|
||||
get: () =>
|
||||
|
@ -3,6 +3,9 @@ const { DateTime } = require('luxon');
|
||||
|
||||
class DateTimeProvider
|
||||
{
|
||||
static create = () => new this();
|
||||
|
||||
|
||||
unixTimestamp()
|
||||
{
|
||||
return Math.floor(DateTime.now().toSeconds());
|
||||
|
@ -1,5 +1,9 @@
|
||||
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)
|
||||
{
|
||||
this.logger = logger;
|
||||
|
@ -4,6 +4,9 @@ const crypto = require('crypto');
|
||||
|
||||
class NotificationRepository
|
||||
{
|
||||
static create = container => new this(container.Logger, container.DateTimeProvider, container.AsyncFs, container.Config);
|
||||
|
||||
|
||||
constructor(logger, dateTimeProvider, asyncFs, config)
|
||||
{
|
||||
this.logger = logger;
|
||||
|
@ -1,5 +1,8 @@
|
||||
class ApiRoutes
|
||||
{
|
||||
static create = container => new this(container.logger, container.NotificationFacade, container.Config);
|
||||
|
||||
|
||||
constructor(logger, notificationFacade, config)
|
||||
{
|
||||
this.logger = logger;
|
||||
@ -8,7 +11,7 @@ class ApiRoutes
|
||||
}
|
||||
|
||||
|
||||
create(express)
|
||||
createRouter(express)
|
||||
{
|
||||
const router = express.Router();
|
||||
|
||||
|
@ -1,5 +1,8 @@
|
||||
class SubjectParser
|
||||
{
|
||||
static create = () => new this();
|
||||
|
||||
|
||||
parse(subject)
|
||||
{
|
||||
// Possible formats:
|
||||
|
@ -7,6 +7,9 @@ const transportMap = {
|
||||
|
||||
class TransportProvider
|
||||
{
|
||||
static create = () => new this();
|
||||
|
||||
|
||||
byType(type)
|
||||
{
|
||||
return transportMap.hasOwnProperty(type) ? transportMap[type] : null;
|
||||
|
Loading…
Reference in New Issue
Block a user