How can I avoid filling my JavaScript code with type-checking logic when using dependency injection?

glcheetham

I'm trying to employ SOLID principles learnt from strongly-typed languages in a Node.js application, and implement dependency injection through the constructor in my modules.

Without a compiler to help me, I've found myself impulsively writing type-checking and null-reference checking logic to make sure dependencies of the correct type are being passed in.

var _ = require('lodash');

var ExampleModule = function(dependencies) {
    var app, express, http;

    if(_.isUndefined(dependencies) || typeof dependencies !== 'object') {
        throw new ReferenceError('No dependencies object passed to the constructor. Actually passed: ' + typeof dependencies);
    }

    if(_.isUndefined(dependencies.express)) {
        throw new ReferenceError('The express module must be passed as the \'express\' property in the constructor.');
    } else {
        express = dependencies.express;
    }
    // Tempted to add a type check for dependencies.express here

    if(_.isUndefined(dependencies.http)) {
        throw new ReferenceError('The node http module must be passed as the \'http\' property in the constructor.'); 
    } else {
       http = dependencies.http; 
    }
    // Tempted to add a type check for dependencies.http here

};

module.exports = ExampleModule;

This feels fundamentally wrong for two reasons

  • Constructors filled with many conditional statements; likely to increase in size as more dependencies are needed.
  • Many extra unit tests are written to check the type-checks and null-reference checks are working

I don't want to spend half my time maintaining type-checking logic, but I also don't want to spend half my time debugging errors when the wrong type of a dependency is passed in.

What design pattern am I missing that would solve this problem?

Derick Bailey

small, but important clarification: the SOLID "D" is not dependency injection. it is dependency inversion.

for a better understanding of SOLID as it pertains to dynamically typed languages, watch this presentation from Jim Weirich: http://confreaks.tv/videos/rubyconf2009-solid-ruby

it's about Ruby, but all the principles are the same when applied to JavaScript.

there's also my own SOLID JavaScript presentation I did a few years ago: https://sub.watchmecode.net/episode/solid-javascript-presentation/

...

your own answer talks about require as dependency injection, but this isn't correct.

the require call is a module loader, not a dependency manager. the difference is subtle, but important.

the call to require only loads code from another file. it does not supply the dependency to your other code. you have to either call the code w/ the loaded module, or use another tool such as wire.js to supply the dependency for you.

...

regarding your dependency injection question: "it depends" is the only viable answer.

i rarely use type checking like this, when dealing with the internals of my applications.

however, if you're building an API that is called from third parties, it is often necessary to do what you have done, to make sure the API is called correctly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I use Dependency Injection in a .Net Core ActionFilterAttribute?

From Dev

How can I avoid a virtual call when I know the type?

From Dev

How can I prevent someone from altering or avoiding my JavaScript logic when adding buttons to jQuery UI dialog?

From Dev

How can I add dependency injection after the fact?

From Dev

Using dependency injection on a type="" property

From Dev

How can I avoid using the type name type

From Dev

Python/Pandas - how can I avoid ellipsis when using 'describe'

From Dev

How can I avoid passing the scheduler through my business logic, when writing tests for RXJS observables?

From Dev

How do I avoid absolute pathnames in my code when using Git?

From Dev

How can I add constructor params using Dependency Injection

From Dev

How can i access the values from parent component in angular2 using dependency injection?

From Dev

How to avoid dependency injection in Django?

From Dev

How can I avoid resource leak when using a semaphore?

From Dev

Using dependency injection how can I access a method in a class inside a service?

From Dev

How can I organize my code to optionally provide a backing view for my logic layer?

From Dev

How can I prevent someone from altering or avoiding my JavaScript logic when adding buttons to jQuery UI dialog?

From Dev

Instead of using Maven to pull in a third party dependency, how can I implement the source of that dependency directly into my project?

From Dev

How can I avoid circular logic when synchronizing Java Threads?

From Dev

How can I add dependency injection after the fact?

From Dev

How can I do parameterized dependency injection with Spring?

From Dev

How can I avoid repeating my code when creating reply callbacks in NodeJS?

From Dev

How do I catch javascript code injection in URL using python?

From Dev

How can I avoid using a bash script in my launchd plist?

From Dev

How do I avoid absolute pathnames in my code when using Git?

From Dev

How can I implement this type of logic using Promises?

From Dev

How can I avoid encoding when using "FOR XML PATH"?

From Dev

How can jvm check arguments and return type when using jni invoking? How to implement this type checking?

From Dev

Dependency injection - trying to avoid using a service locator

From Dev

How can I read my dependency from my class of a given type of Class/Inteface and execute methods on it?

Related Related

  1. 1

    How can I use Dependency Injection in a .Net Core ActionFilterAttribute?

  2. 2

    How can I avoid a virtual call when I know the type?

  3. 3

    How can I prevent someone from altering or avoiding my JavaScript logic when adding buttons to jQuery UI dialog?

  4. 4

    How can I add dependency injection after the fact?

  5. 5

    Using dependency injection on a type="" property

  6. 6

    How can I avoid using the type name type

  7. 7

    Python/Pandas - how can I avoid ellipsis when using 'describe'

  8. 8

    How can I avoid passing the scheduler through my business logic, when writing tests for RXJS observables?

  9. 9

    How do I avoid absolute pathnames in my code when using Git?

  10. 10

    How can I add constructor params using Dependency Injection

  11. 11

    How can i access the values from parent component in angular2 using dependency injection?

  12. 12

    How to avoid dependency injection in Django?

  13. 13

    How can I avoid resource leak when using a semaphore?

  14. 14

    Using dependency injection how can I access a method in a class inside a service?

  15. 15

    How can I organize my code to optionally provide a backing view for my logic layer?

  16. 16

    How can I prevent someone from altering or avoiding my JavaScript logic when adding buttons to jQuery UI dialog?

  17. 17

    Instead of using Maven to pull in a third party dependency, how can I implement the source of that dependency directly into my project?

  18. 18

    How can I avoid circular logic when synchronizing Java Threads?

  19. 19

    How can I add dependency injection after the fact?

  20. 20

    How can I do parameterized dependency injection with Spring?

  21. 21

    How can I avoid repeating my code when creating reply callbacks in NodeJS?

  22. 22

    How do I catch javascript code injection in URL using python?

  23. 23

    How can I avoid using a bash script in my launchd plist?

  24. 24

    How do I avoid absolute pathnames in my code when using Git?

  25. 25

    How can I implement this type of logic using Promises?

  26. 26

    How can I avoid encoding when using "FOR XML PATH"?

  27. 27

    How can jvm check arguments and return type when using jni invoking? How to implement this type checking?

  28. 28

    Dependency injection - trying to avoid using a service locator

  29. 29

    How can I read my dependency from my class of a given type of Class/Inteface and execute methods on it?

HotTag

Archive