scottcorgan / Injector

Node.js directory-independent dependency injection for apps and testing.
36 stars 1 forks source link

Improvement: Aliases for modules for example to support modules from "node_modules" with special characters #36

Closed Sharaal closed 8 years ago

Sharaal commented 10 years ago

Hi,

my first pull request, but I hope it will works to send you a little improvement to your greate project. :)

Introduction

The injector allows to load modules from "node_modules" per inject and per defining as dependency. So:

var dependency = require('dependency');
exports.module = function () {};

Is the same as:

exports.module = function (inject) {
    var dependency = inject('dependency');
};

And the same as:

exports.module = function (dependency) {};

Problem

But the problem are modules with special characters in the name, like "body-parser" (connector extension for express).

So this will not work, its a syntax error:

exports.module = function (body-parser) {};

Solution

Cause of this problem I add the possibility to define aliases for the Injector.

So with this feature you can add aliases to the injector configuration:

Injector.create('', {
    aliases: {
        bodyParser: 'body-parser'
    }
});

And now use the alias as dependency for modules:

exports.module = function (bodyParser) {};

Implementation

I add the aliases to the register method and replace the names in the dependency configuration of the modules. This was the only central location I found to check and replace the aliases for both types of modules (parsed modules and modules from "node_modules").

But if you find a better location feel free. :)

scottcorgan commented 10 years ago

@dragonprojects I'm intrigued by the aliases idea. That is originally why I made the inject method available. It was to handle modules with invalid variable names.

Out of curiosity, what is your issue with the inject method?

Sharaal commented 10 years ago

Oh, i deleted my comment... So new try. :)

I think the inject method brakes the concept behind the injector.

The key concept is to give the module all the dependencies per function parameter. With the inject method the module must take the dependencies itself.

Another reason is more complexity in unit testing. Instead of giving the module the mock object for the dependency I must mock the inject method to return the mock objects depends on the name.

Both strange behavior only cause of special characters in the name. I think thats not a clear concept and behavior. The aliases remove with stranges and make it clear.

scottcorgan commented 10 years ago

That is a very valid argument. And I agree with you. Is this PR a fix for that?

As far as unit testing, the inject method automatically works the same as injected dependencies (right now).

On Tue, Aug 19, 2014 at 12:53 PM, Christoph Herrmann notifications@github.com wrote:

Oh, i deleted my comment... So new try. :) I think the inject method brakes the concept behind the injector. The key concept is to give the module all the dependencies per function parameter. With the inject method the module must take the dependencies itself. Another reason is more complexity in unit testing. Instead of giving the module the mock object for the dependency I must mock the inject method to return the mock objects depends on the name.

Both strange behavior only cause of special characters in the name. I think thats not a clear concept and behavior. The aliases remove with stranges and make it clear.

Reply to this email directly or view it on GitHub: https://github.com/scottcorgan/Injector/pull/36#issuecomment-52689130

Sharaal commented 10 years ago

Yes, the pull request add the aliase feature to the injector to support dependencies with special characters in that clear way for the module.

Sharaal commented 8 years ago

After nearly two years, the still open pull-request can closed.