Friday, 4 October 2019

Code structure question - encapsulating functionality in an argument

Hi, I am looking at writing a node app and I'm trying to get the foundation right before I start to build too much. For context here I'm creating a server based application that will consume and produce json as well as handle database / socket connections, etc.My question has a few aspects to it. First, I'd like to bundle up different components of the app up into a single "context" object, as opposed to requiring modules individually. Second, I'd like to pass this object as a parameter to business logic functions, as opposed to requiring / constructing the module from the business logic script.I'd like to have all functionality associated with the app maintain a consistent interface, namely as functions that are passed two objects - one containing state, and the other containing input. The state / context object will encapsulate all data access and events as well as other shared resources like logging. I've seen this practice before of passing around an object that contains everything, but I'd like to get some feedback if this is considered good practice.// someAppLogic.js module.exports = async function(context, input) { context.logger.debug('started someAppLogic function'); const user = await context.database.select('users', /* ... */); if(input.flag) context.database.update('users', /* ... */); context.events.emit('asdf'); someOtherAppLogic(context, { id: 17 }); } And when referencing this:// main.js const context = new Context({ prop: 'val' }); someAppLogic(context, req.body);

Submitted October 04, 2019 at 08:34PM by ChuckItOver

No comments:

Post a Comment