0.8.75 • Published 2 years ago

@rafterjs/di-autoloader v0.8.75

Weekly downloads
182
License
GPL-3.0-or-later
Repository
github
Last release
2 years ago

A dependency injection autoloader

Build Status

A config driven autoloader for dependency injection.

Getting started

yarn add @rafterjs/di-autoloader

Example

Check out the files in ./example and ./test/fixtures

The autoloading config

// service-config.js

const servicesConfig = {
    config: {
        path: `${__dirname}/../test/mocks/test-config`,
        dependencies: []
    },
    testClass: {
        path: `${__dirname}/../test/mocks/test-class`,
        dependencies: [
            `config.foo.bar`,
            'testFunction'
        ]
    },
    testFunction: {
        path: `${__dirname}/../test/mocks/test-function`,
        dependencies: []
    }
};

export default servicesConfig;

Example class with DI

// test-class.js

class TestClass {
    // The dependencies will be injected via the constructor
    constructor(bar, testFunction) {
        this._bar = bar;
        this._testFunction = testFunction;
    }

    /**
     * @return {string}
     */
    getBar() {
        return this._bar;
    }

    /**
     * @return {Function}
     */
    getFunction() {
        return this._testFunction;
    }

    /**
     * @return {string}
     */
    getData() {
        return `here's some data`;
    }
}

export default TestClass;

Instantiating the DI container using the autoloader

import BoxDiAutoLoader from '../lib/box-di-autoloader';
import serviceConfig from './service-config';

const boxDiAutoLoader = new BoxDiAutoLoader(serviceConfig);
boxDiAutoLoader.load();

const testClass = boxDiAutoLoader.get('testClass');
console.log(testClass.getData());

Why use dependency injection (DI)?

DI is a well vetted programming pattern that encourages decoupling of services and allows you to maintain separation of concerns. This in turn means your code is more testable and maintainable.

Check out: http://tutorials.jenkov.com/dependency-injection/dependency-injection-benefits.html and https://stackoverflow.com/questions/130794/what-is-dependency-injection

Why should we use it in node?

Just because we have import or require doesn't mean we should forgo programming best practices. What import does is hardcode your dependencies, rather than making them configurable or reusable eg.

Say we have a DbService which currently uses MongoDb. In an import style your code would be something like:

import mongoose from 'mongoose';

class DbService {
    async connect(url) {
        return mongoose.connect(url);
    }

    async find(query, Model) {
        return mongoose.find(query, Model);
    }

    ...
}

But now your app has grown and you realise that the load on your DB is reducing application performance; you need to introduce a caching layer to reduce the load on the DB.

In our import scenario above that would require a bunch of rewriting at the DbService level. Instead if we had structured it like:

class DbService {
    constructor(dao) {
        this._dao = dao;
    }

    async connect(url) {
        return this._dao.connect(url);
    }

    async find(query, Model) {
        return this._dao.find(query, Model);
    }

    ...
}

We could now inject MongoDbCacheDao instead of injecting mongoose. This would handle the caching, and delegate through to Mongo if needed. The DbService doesn't care if it is cached or not; it is none of it's concern. eg

import crypto from 'crypto'; // NOTE: importing from the node standard lib is ok

class MongoDbCacheDao {
    constructor(dao) {
        this._dao = dao;
        this._cache = {};
    }

    async connect(url) {
        return this._dao.connect(url);
    }

    async find(query, Model) {
        const md5sum = crypto.createHash('md5');
        md5sum.update(
            JSON.stringify(query) +
            JSON.stringify(Model)
        );

        const cacheId =  md5sum.digest('hex');
        if (!this._cache[cacheId]) {
            this._cache[cacheId] = await this._dao.find(query, Model);
        }

        return this._cache[cacheId];
    }

    ...
}

So even though we have import/require in node, it is still valuable to separate concerns and use dependency injection. It will allow you to be much more flexible in the future and adapt to changes more quickly.

Why use an autoloader with DI

As your application grows, so too does your dependency graph. Handling all your dependencies manually becomes very tedious and you end up with lots of boilerplate code in the form of factories. Instead, when using an autoloader, you maintain your dependencies in a simple config file and you only need to create factories in rare circumstances when class/function instantiation is much more complex.

Overall, this means less code to write and maintain, which means less complexity and faster time to market.

0.8.74

2 years ago

0.8.73

2 years ago

0.8.75

2 years ago

0.8.70

2 years ago

0.8.72

2 years ago

0.8.71

2 years ago

0.8.69

2 years ago

0.8.68

2 years ago

0.8.67

3 years ago

0.8.66

3 years ago

0.8.63

3 years ago

0.8.65

3 years ago

0.8.64

3 years ago

0.8.62

3 years ago

0.8.61

3 years ago

0.8.60

3 years ago

0.8.56

3 years ago

0.8.55

3 years ago

0.8.58

3 years ago

0.8.57

3 years ago

0.8.54

3 years ago

0.8.59

3 years ago

0.8.52

3 years ago

0.8.51

3 years ago

0.8.53

3 years ago

0.8.50

3 years ago

0.8.49

3 years ago

0.8.45

3 years ago

0.8.47

3 years ago

0.8.46

3 years ago

0.8.48

3 years ago

0.8.44

3 years ago

0.8.43

3 years ago

0.8.41

3 years ago

0.8.40

3 years ago

0.8.42

3 years ago

0.8.39

3 years ago

0.8.37

3 years ago

0.8.34

3 years ago

0.8.36

3 years ago

0.8.35

3 years ago

0.8.33

3 years ago

0.8.32

3 years ago

0.8.30

3 years ago

0.8.31

3 years ago

0.8.32-alpha.0

3 years ago

0.8.23

3 years ago

0.8.22

3 years ago

0.8.25

3 years ago

0.8.24

3 years ago

0.8.21

3 years ago

0.8.20

3 years ago

0.8.27

3 years ago

0.8.26

3 years ago

0.8.29

3 years ago

0.8.28

3 years ago

0.8.19

3 years ago

0.8.9

3 years ago

0.8.8

3 years ago

0.8.12

3 years ago

0.8.11

3 years ago

0.8.14

3 years ago

0.8.13

3 years ago

0.8.10

3 years ago

0.8.16

3 years ago

0.8.15

3 years ago

0.8.18

3 years ago

0.8.17

3 years ago

0.8.7

3 years ago

0.8.6

3 years ago

0.8.5

3 years ago

0.8.4

3 years ago

0.8.1

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.7.0-alpha.6

4 years ago

0.7.0-alpha.3

4 years ago

0.7.0-alpha.2

4 years ago

0.7.0-alpha.0

4 years ago

0.6.0

4 years ago

0.5.1-alpha.1

4 years ago

0.5.1-alpha.0

4 years ago

0.5.0

4 years ago

0.4.2-alpha.4

4 years ago

0.4.2-alpha.3

4 years ago

0.4.2-alpha.2

4 years ago

0.4.2-alpha.1

4 years ago

0.4.2-alpha.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago