aedart/js-ioc

`make` should accept object and not just array as parameters

Opened this issue · 1 comments

While an array is sufficient for certain simple instantiations, it is not good enough for a more complex approach, in which your "factory" method needs to take action upon certain elements given to it.

Consider the following example, in which endpoints need to set default values. The work-around appears okay - but would had been much easier if an object was accepted, instead of an array.

        this.ioc.bind('myEndpoint', (ioc, params = []) => {

            // Work-around - why is this needed ?
            if(params.length !== 0 && typeof params[0] === 'object'){
                params = params[0];
            } else {
                params = {};
            }

            // Id
            let id = 'SharedEndpoint';
            if(params['id'] !== undefined){
                id = params['id'];
            }

            // Transporter
            let transporter = null;
            if(params['transporter'] !== undefined){
                transporter = params['transporter'];
            }

            // Address
            let address = null;
            if(params['address'] !== undefined){
                address = params['address'];
            }

            // Additional data
            let data = {};
            if(params['data'] !== undefined){
                data = params['data'];
            }

            return new Endpoint(id, transporter, address, data);
        });

Possible solution

Change the supported type to be an array or object. The build method just needs to take this into account, when creating concrete instances.