{"id":18400867,"url":"https://github.com/magicmark/lentildi","last_synced_at":"2025-08-09T05:19:48.064Z","repository":{"id":69482067,"uuid":"64570377","full_name":"magicmark/LentilDI","owner":"magicmark","description":"💉  Lightweight + Simple ES6 Dependency Injection :)","archived":false,"fork":false,"pushed_at":"2017-09-25T02:34:28.000Z","size":47,"stargazers_count":6,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T14:11:23.693Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magicmark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-31T00:51:18.000Z","updated_at":"2017-03-09T21:24:19.000Z","dependencies_parsed_at":"2023-04-05T09:31:31.156Z","dependency_job_id":null,"html_url":"https://github.com/magicmark/LentilDI","commit_stats":{"total_commits":61,"total_committers":2,"mean_commits":30.5,"dds":0.4098360655737705,"last_synced_commit":"323907a4e82607752b956f884f69275049f52663"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2FLentilDI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2FLentilDI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2FLentilDI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2FLentilDI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magicmark","download_url":"https://codeload.github.com/magicmark/LentilDI/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247607783,"owners_count":20965945,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T02:37:08.096Z","updated_at":"2025-08-09T05:19:48.028Z","avatar_url":"https://github.com/magicmark.png","language":"JavaScript","readme":"![LentilDI Logo](http://i.imgur.com/BKDQnkU.png)\n\nLentilDI\n========\n[![travis](https://travis-ci.org/magicmark/LentilDI.svg?branch=master)](https://travis-ci.org/magicmark/LentilDI)\n[![Coverage Status](https://coveralls.io/repos/github/magicmark/LentilDI/badge.svg?branch=master)](https://coveralls.io/github/magicmark/LentilDI?branch=master)\n[![npm](https://img.shields.io/npm/v/lentildi.svg)](https://www.npmjs.com/package/lentildi)\n\nLightweight + Simple ES6 Dependency Injection :)\n\n**LentilDI** lets you build apps without the pain of having to instantiate, wire up, and manually manage your dependency tree. LentilDI emphasises:\n\n* Reduction of boilerplate dependency juggling\n* Ease of module testing\n\nCheck out the [hello world](https://github.com/magicmark/LentilDI/blob/master/examples/hello_world/index.js) example for a quick introduction.\n\n## Install\n\n```\n$ npm install --save lentildi\n```\n\n## Example\n\nWith LentilDI, you can go from something like this:\n\n```javascript\nconst tuba = new Tuba();\nconst horn = new Horn();\nconst percussion = new Percussion();\nconst conductor = new Conductor('Snoop Dogg');\nconst brassSection = new BrassSection(conductor, tuba, horn, fs, os);\n...\nconst orchestra = new Orchestra(conductor, brassSection, percussion);\n```\n\nTo something like this:\n\n```javascript\nconst lentil = new Lentil();\nlentil.setArgs(Conductor, ['Snoop Dogg']);\nconst orchestra = lentil.create(Orchestra);\n```\n\n### Automatic Dependency Wiring\n\nTypically, we might pass in our dependencies (including built-in objects) and bind them to `this` in large constructors such as this:\n\n```javascript\nclass BrassSection extends LentilBase {\n\n    constructor (conductor, tuba, horn, fs, os) {\n        this.conductor = conductor;\n        this.tuba = tuba;\n        this.horn = horn;\n        this.fs = fs;\n        this.os = os;\n    }\n\n    loadSheetMusic () {\n        const sheetMusic = this.conductor.getScore();\n        this.fs.readFile(sheetMusic, ...\n    }\n\n    ...\n```\n\nWhen we use LentilDI, we get that wiring done for us for free:\n\n```javascript\nclass BrassSection extends LentilBase {\n\n    static lentilDeps () {\n        return {\n            conductor: Conductor,\n            tuba: Tuba,\n            horn: Horn,\n            fs,\n            os,\n        }\n    }\n\n    loadSheetMusic () {\n        const sheetMusic = this.conductor.getScore();\n        this.fs.readFile(sheetMusic, ...\n    }\n\n    ...\n```\n\n### Testing\n\nTesting modules is easy as pie - just create your module as normal!\nIf you want to override anything in `lentilDeps`, just pass an object as the last argument to your constructor.\n\n```javascript\nit('BrassSection should play some music', function () {\n    const dummyConductor = { ... };\n    const dummyTuba = { ... };\n    const dummyHorn = { ... };\n\n    // Note that we don't have to override fs or os\n    // We can let Lentil assign them as default values\n    const brassSection = new BrassSection({\n        conductor: dummyConductor,\n        tuba: dummyTuba,\n        horn: dummyHorn,\n    });\n\n    brassSection.playMusic();\n\n    ...\n});\n```\n\n### More Examples\nCheck out some full example apps (with tests!) [here](https://github.com/magicmark/LentilDI/tree/master/examples).\n\n## LentilDep\n\nYou can specify different types of dependencies in your `lentilDeps` declaration.\n\nCurrently, Lentil understands 3 types of dependencies:\n\n* `LentilDep.Provided`\n* `LentilDep.Regular`\n* `LentilDep.Lentil`\n\n### LentilDep.Provided\nFor cases where you might have an externally instantiated class (e.g. a logger) that you want to be available in any of your modules:\n\n```javascript\nconst logger = log4js.getLogger('My Logger');\n\nconst lentil = new Lentil();\nlentil.provide('logger', logger);\nconst myApp = lentil.create(MyApp);\n```\n\nYour logger instance will now be available as normal through `this.logger` inside a Lentil module:\n\n```javascript\nclass SomeModule extends LentilBase {\n\n    static lentilDeps () {\n        return {\n            logger: LentilDep.Provided('logger'),\n        }\n    }\n\n    doSomething() {\n        this.logger.info( ... );\n    }\n\n}\n```\n\n### LentilDep.Regular\nThis is the default type where values are simply passed along to your module.\n\nUnless otherwise specified, this is how Lentil will treat a dependency.\n\n```javascript\nclass SomeModule extends LentilBase {\n\n    static lentilDeps () {\n        return {\n            whatever: 'Whatever',\n        }\n    }\n\n}\n```\n\n'Whatever' would now be available through `this.whatever`. (This is particularly useful for built in objects such as `os`, `console` etc.)\n\nFor the sake of clarity, note that this is functionally equivalent to the following:\n\n```javascript\nclass SomeModule extends LentilBase {\n\n    static lentilDeps () {\n        return {\n            // This is not recommended as Lentil can do this wrapping for us.\n            whatever: LentilDep.Regular('Whatever'),\n        }\n    }\n\n}\n```\n\n### LentilDep.Lentil\nFor sub-dependencies that you wish Lentil to also construct (i.e. other modules that extend from LentilBase.)\n\nSimilar to LentilDep.Regular, you do not need to explicitly wrap modules in this; Lentil will do this for you.\n\n```javascript\nclass SomeOtherModule extends LentilBase {\n\n    static lentilDeps () {\n        return {\n            someModule: SomeModule,\n        }\n    }\n\n}\n```\n\nThis is equivalent to the following:\n\n```javascript\nclass SomeOtherModule extends LentilBase {\n\n    static lentilDeps () {\n        return {\n            // This is not recommended as Lentil can do this wrapping for us.\n            someModule: LentilDep.Lentil(SomeModule),\n        }\n    }\n\n}\n```\n\n## Constructor Arguments\nYou can pass in arguments to your modules (useful for one-offs such as config values).\n\nTo do so, pass an array of arguments to `lentil.setArgs`:\n\n```javascript\nconst lentil = new Lentil();\nlentil.setArgs(Conductor, ['Snoop Dogg']);\n...\n```\n\nInside your module, your arguments are available as normal.\n\n(Remember to call `super`.)\n\n```javascript\nclass Conductor extends LentilBase {\n\n    constructor (conductorName, ...args) {\n        super(...args);\n\n        console.log(`Orchestra is being conducted by ${conductorName}`);\n    }\n\n    ...\n\n}\n```\n\n## Full Documentation\nComing Soon\n\n## Contributing\nPlease do!\n\n## Why 'Lentil'?\nI like lentils\n\n## Licence\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicmark%2Flentildi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagicmark%2Flentildi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicmark%2Flentildi/lists"}