{"id":26442515,"url":"https://github.com/alt-javascript/cdi","last_synced_at":"2025-03-18T10:19:45.870Z","repository":{"id":57104246,"uuid":"391842770","full_name":"alt-javascript/cdi","owner":"alt-javascript","description":"A familiar application context and dependency injection package for JavaScript, supporting simple singleton and prototype component factory definitions, with a choice of manual or auto wiring (injection) of property references and config placeholders.","archived":false,"fork":false,"pushed_at":"2022-03-30T07:14:42.000Z","size":558,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-17T19:57:49.924Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alt-javascript.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-02T06:47:20.000Z","updated_at":"2021-10-08T04:27:16.000Z","dependencies_parsed_at":"2022-08-20T17:10:45.862Z","dependency_job_id":null,"html_url":"https://github.com/alt-javascript/cdi","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alt-javascript%2Fcdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alt-javascript%2Fcdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alt-javascript%2Fcdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alt-javascript%2Fcdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alt-javascript","download_url":"https://codeload.github.com/alt-javascript/cdi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244102760,"owners_count":20398386,"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":"2025-03-18T10:19:45.362Z","updated_at":"2025-03-18T10:19:45.854Z","avatar_url":"https://github.com/alt-javascript.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Application Contexts and Dependency Injection\n=============================================================\n\n[![NPM](https://nodei.co/npm/@alt-javascript/cdi.svg?downloads=true\u0026downloadRank=true)](https://nodei.co/npm/@alt-javascript/cdi/)\n\u003cbr/\u003e\n![Language Badge](https://img.shields.io/github/languages/top/alt-javascript/cdi)\n![Package Badge](https://img.shields.io/npm/v/@alt-javascript/cdi) \u003cbr/\u003e\n[release notes](https://github.com/alt-javascript/cdi/blob/main/History.md)\n\n\u003ca name=\"intro\"\u003eIntroduction\u003c/a\u003e\n--------------------------------\n\nA familiar application context and dependency injection package for JavaScript, supporting simple\nsingleton and prototype component factory definitions, with a choice of manual or auto\nwiring (injection) of property references and config placeholders.\n\n\u003ca name=\"usage\"\u003eUsage\u003c/a\u003e\n--------------------------------\n### Browser\n\nThe module is also able to be used directly in the browser, in combination with the logger and config modules.\nYou can either import the ApplicationContext globally as an IIFE (Immediately Invoked Function Expression),\nas follows:\n\n```html\n   \u003cscript src=\"https://cdn.jsdelivr.net/npm/@alt-javascript/cdi/dist/alt-javascript-applicationcontext-iife.js\"\u003e\u003c/script\u003e\n    \u003cscript\u003e\n        import { SimpleClass } from'./index.js';\n\n        const applicationContext = new ApplicationContext(SimpleClass);\n    \u003c/script\u003e\n```\n\nOr import the ES6 module bundle from a module, as follows:\n\n```javascript\nimport { ApplicationContext } from 'https://cdn.jsdelivr.net/npm/@alt-javascript/cdi/dist/alt-javascript-cdi-esm.js'\nimport { SimpleClass } from'./index.js';\n\nconst applicationContext = new ApplicationContext(SimpleClass);\n```\n\n### Singleton Components\n\nTo configure a simple singleton service object, simply instantiate an `ApplicationContext` and pass it\nthe class definition. The default scope is `singleton`, and the component name defaults to lowerCamelCase\nof the class name.\n\n```javascript\nimport { ApplicationContext } from '@alt-javascript/cdi';\nimport { SimpleClass } from'./index.js';\n\nconst applicationContext = new ApplicationContext(SimpleClass);\napplicationContext.start();\napplicationContext.get('simpleClass');\n```\nSimple types, objects and functions can be registered as components.\n\n```javascript\nimport { ApplicationContext } from '@alt-javascript/cdi';\nimport { SimpleClass } from './index.js';\n\nconst applicationContext = new ApplicationContext([\n    {name: 'someData', attr:'aValue', behave: () =\u003e {}},\n    {Reference : (what) =\u003e { console.log (`Hello ${what}`)},\n      name : 'aFunc'}]);\napplicationContext.start();\napplicationContext.get('someData').behave();\napplicationContext.get('aFunc')('world!');\n```\n\nSingletons can also be defined with the common alias names `Service`,`Component` and `Singleton`\n\n```javascript\nimport { ApplicationContext, Singleton, Service, Component } from '@alt-javascript/cdi';\nconst { SimpleSingleton, SimpleService, SimpleSingleton } from './index.js';\n\nconst context = new Context([\n    new Singleton(SimpleSingleton), \n    new Service(SimpleService),\n    new Component(SimpleSingleton)]);\nconst applicationContext = new ApplicationContext([context]);\napplicationContext.start();\napplicationContext.get('simpleSingleton');\napplicationContext.get('simpleService');\napplicationContext.get('simpleComponent');\n```\n### Prototype (transient) Components\n\nPrototype, or transient scoped objects can be defined with Prototype definition, or Transient definition.  These\nobjects are created each time they are requested from the context, or wired by the context lifecycle.\n\n```javascript\nimport { ApplicationContext, Prototype, Transient }  from '@alt-javascript/cdi';\nimport { SimpleClass, SimpleTransient }  from './index.js';\n\nconst applicationContext = new ApplicationContext(new Prototype(SimpleClass));\nconst applicationContext = new ApplicationContext(new Transient(SimpleTransient));\napplicationContext.start();\napplicationContext.get('simpleClass');\n```\n\n### Explicit Component Definitions\n\nUse the Component class to declare the full explicit definition of a component, allow full control.\n\n```javascript\nimport { ApplicationContext, Component }  from '@alt-javascript/cdi';\nimport { SimpleClass, SimpleTransient } from './index.js';\n\nconst applicationContext = new ApplicationContext(\n    new Component({\n        Reference : SimpleClass,\n        name : 'useAnExplicitName',\n        qualifier : '@my-scope/SimpleClass',\n        scope : Scopes.SERVICE,\n    }));\n\napplicationContext.start();\napplicationContext.get('useAnExplicitName');\n```\n\n### Component Factory Definitions\n\nA component can be created by referencing a factory function directly.\n\n```javascript\nimport { ApplicationContext, Component } from '@alt-javascript/cdi';\nimport { MyClass }  from './index.js';\n\nconst applicationContext = new ApplicationContext(\n    new Component({\n        factory : MyClass.someStaticFunction(),\n        name : 'fromAFactory',\n        qualifier : '@my-scope/SimpleClass',\n        scope : Scopes.SERVICE,\n    }));\n\napplicationContext.start();\napplicationContext.get('useAnExplicitName');\n```\n### Component Property Injection (wiring)\n\nComponent properties are autowired by name, by default.  In the example `classB` and `classC`  will be autowired,\nbut `classD`, which is non-null will be let alone.  The `attribute` will remain null if not found in the application\ncontext.\n\n```javascript\nexport default class ClassA {\n  constructor() {\n    this.classB = null;\n    this.classC = 'autowired';\n    this.classD = new new ClassD();\n    this.attribute = null;\n  }\n};\n```\n\nConfiguration values that are booted with `@alt-javascript/boot` are injected with the familiar placeholder syntax.\n\n```javascript\nexport default class ClassA {\n  constructor() {\n    this.attribute = '${get.this.from.config}';\n  }\n};\n```\n\n### Advanced Component Property Injection, factory and wireFactory functions\n\nProperties can be injected, directly from functions, or from references to functions on other components using\nthe explicit `Property` declaration class.\n\n```javascript\n    const context = new Context([\n      {\n        name: 'singletonFactory',\n        generator: (param) =\u003e ({ name: 'simplePrototype', attr: param }),\n      },\n      new Service({\n        Reference: 'MyServiceService',\n          properties: [new Property({\n              name:'myServiceProperty',\n              factory: 'singletonFactory',\n              factoryFunction: 'generator',\n              factoryArgs: 'one'})]\n      })]);\n\n    const applicationContext = new ApplicationContext([context]);\n    applicationContext.start();\n    const myServiceService = applicationContext.get('myServiceService');\n    assert.exists(myServiceService, 'myServiceService exists');\n    assert.equal(myServiceService.myServiceProperty.attr, 'one', 'myServiceService.myServiceProperty.attr == one');\n```\n\nWhere the context of the target component being wired is required, a `wireFactory` can be declared on a `prototype`\nand the target `Component` instance is passed as an argument by default.  The `ApplicationContext` declares a \n`logger` prototype with the `loggerFactory` component as the `wireFactory`, allowing the component qualifier to be\nused as the logger category (true).\n\n### Global components: \n\nThe ApplicationContext is designed to  play nicely with other projects in the `@alt-javascript` scope, so the\nbooted `config`, `loggerFactory`, `logger`, `loggerCategoryCache` are available.  \n\nAny component declared with a `logger` and `qualifier` properties will be injected with an appropriate logger,\nas if by _magic_.\n\n\u003ca name=\"license\"\u003eLicense\u003c/a\u003e\n-----------------------------\n\nMay be freely distributed under the [MIT license](https://raw.githubusercontent.com/alt-javascript/cdi/main/LICENSE).\n\nCopyright (c) 2021 Craig Parravicini    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falt-javascript%2Fcdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falt-javascript%2Fcdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falt-javascript%2Fcdi/lists"}