Loopback: Dependency injection

Created on 2 Sep 2015  路  5Comments  路  Source: strongloop/loopback

I'm pretty sure you already thought about this for a while, however I guess it worth to mention it again since it will be particularly useful in the context of a framework. Also for testing simplifies the things.

I found it very handy especially for models and remote methods, where they might look like this:

  User.followers = function (userId, $req, $app, $UserFollowsModel, next) {
    var data = { where: {聽toUser: userId } }
    UserFollowsModel.find(data, next)
  }

  User.remoteMethod('followers', {
    returns: { root: true },
    accepts: { arg: 'id', type: 'string', required: true },
    http: {
      path: '/:id/followers',
      verb: 'GET',
      status: 200,
      errorStatus: 400
    }
  })

There're a bunch of packages that can suit properly, or may be writing your own DI container is also simple.
I'd vote take some inspiration from Angular DI, they've done well.

feature stale

Most helpful comment

Yeah, it's always on our radar. There are quite a few interesting use cases (including the one you illustrate) that requires some sort of DI. We'll evaluate it as part the next major release.

All 5 comments

Yeah, it's always on our radar. There are quite a few interesting use cases (including the one you illustrate) that requires some sort of DI. We'll evaluate it as part the next major release.

+1

+1, any updates so far?

+1

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

Was this page helpful?
0 / 5 - 0 ratings