{"id":32558377,"url":"https://github.com/shellicar/core-di","last_synced_at":"2026-02-28T13:15:09.999Z","repository":{"id":257099810,"uuid":"850193581","full_name":"shellicar/core-di","owner":"shellicar","description":"A basic dependency injection library for NodeJS and JavaScript.","archived":false,"fork":false,"pushed_at":"2026-02-22T14:18:09.000Z","size":359,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-22T14:19:39.121Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/shellicar.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-08-31T05:29:59.000Z","updated_at":"2026-02-22T14:14:31.000Z","dependencies_parsed_at":"2025-08-27T12:11:49.207Z","dependency_job_id":"3e5c86fb-ee65-43fa-8acf-2791e542ad32","html_url":"https://github.com/shellicar/core-di","commit_stats":null,"previous_names":["shellicar/core-di"],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/shellicar/core-di","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shellicar%2Fcore-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shellicar%2Fcore-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shellicar%2Fcore-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shellicar%2Fcore-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shellicar","download_url":"https://codeload.github.com/shellicar/core-di/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shellicar%2Fcore-di/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29935139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-28T13:00:17.143Z","status":"ssl_error","status_checked_at":"2026-02-28T12:59:13.669Z","response_time":90,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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-10-28T23:54:31.849Z","updated_at":"2026-02-28T13:15:09.976Z","avatar_url":"https://github.com/shellicar.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @shellicar/core-di\n\n\u003e A basic dependency injection library for TypeScript\n\n[![npm package](https://img.shields.io/npm/v/@shellicar/core-di.svg)](https://npmjs.com/package/@shellicar/core-di)\n[![build status](https://github.com/shellicar/core-di/actions/workflows/node.js.yml/badge.svg)](https://github.com/shellicar/core-di/actions/workflows/node.js.yml)\n\n## Features\n\n* 🎯 Type-safe registration and resolution\n* 🏭 Factory method support\n* 🎨 Decorator-based property injection\n* 🔄 Flexible lifetime management\n* 📦 Service modules for organization\n* 🚨 Clear error messages with dependency chain tracking\n\n## Installation \u0026 Quick Start\n\n```sh\nnpm i --save @shellicar/core-di\n```\n\n```sh\npnpm add @shellicar/core-di\n```\n\n```ts\nimport { createServiceCollection } from '@shellicar/core-di';\nabstract class IAbstract {}\nclass Concrete implements IAbstract {}\nconst services = createServiceCollection();\nservices.register(IAbstract).to(Concrete);\nconst provider = services.buildProvider();\nconst svc = provider.resolve(IAbstract);\n```\n\n\u003c!-- BEGIN_ECOSYSTEM --\u003e\n\n## @shellicar TypeScript Ecosystem\n\n### Core Libraries\n\n- [`@shellicar/core-config`](https://github.com/shellicar/core-config) - A library for securely handling sensitive configuration values like connection strings, URLs, and secrets.\n- [`@shellicar/core-di`](https://github.com/shellicar/core-di) - A basic dependency injection library.\n\n### Reference Architectures\n\n- [`@shellicar/reference-foundation`](https://github.com/shellicar/reference-foundation) - A comprehensive starter repository. Illustrates individual concepts.\n- [`@shellicar/reference-enterprise`](https://github.com/shellicar/reference-enterprise) - A comprehensive starter repository. Can be used as the basis for creating a new Azure application workload.\n\n### Build Tools\n\n- [`@shellicar/build-clean`](https://github.com/shellicar/build-clean) - Build plugin that automatically cleans unused files from output directories.\n- [`@shellicar/build-version`](https://github.com/shellicar/build-version) - Build plugin that calculates and exposes version information through a virtual module import.\n- [`@shellicar/build-graphql`](https://github.com/shellicar/build-graphql) - Build plugin that loads GraphQL files and makes them available through a virtual module import.\n\n### Framework Adapters\n\n- [`@shellicar/svelte-adapter-azure-functions`](https://github.com/shellicar/svelte-adapter-azure-functions) - A [SvelteKit adapter](https://kit.svelte.dev/docs/adapters) that builds your app into an Azure Function.\n- [`@shellicar/cosmos-query-builder`](https://github.com/shellicar/cosmos-query-builder) - Helper class for type safe advanced queries for Cosmos DB (Sql Core).\n\n### Logging \u0026 Monitoring\n\n- [`@shellicar/winston-azure-application-insights`](https://github.com/shellicar/winston-azure-application-insights) - An [Azure Application Insights](https://azure.microsoft.com/en-us/services/application-insights/) transport for [Winston](https://github.com/winstonjs/winston) logging library.\n- [`@shellicar/pino-applicationinsights-transport`](https://github.com/shellicar/pino-applicationinsights-transport) - [Azure Application Insights](https://azure.microsoft.com/en-us/services/application-insights) transport for [pino](https://github.com/pinojs/pino)\n\n\u003c!-- END_ECOSYSTEM --\u003e\n\n## Motivation\n\nComing from .NET I am used to DI frameworks/libraries such as `Autofac`, `Ninject`, `StructureMap`, `Unity`, and Microsoft's own `DependencyInjection`.\n\nI started using `InversifyJS`, and tried out some others along the way, such as `diod`.\n\nWith TypeScript 5.0 generally available with non-experimental decorators, most DI libraries have not been updated, so I decided to create my own.\n\n## Feature Examples\n\nMy set of features is simple, based on my current usage\n\nSee [readme examples](./examples/readme/src) for example source code.\n\n* Type-safe registration.\n\n```ts\nconst services = createServiceCollection();\nabstract class IAbstract { abstract method(): void; }\nabstract class Concrete {}\nservices.register(IAbstract).to(Concrete);\n//                              ^ Error\n```\n\n* Type-safe resolution.\n\n```ts\nconst provider = services.buildProvider();\nconst svc = provider.resolve(IMyService);\n//    ^ IMyService\n```\n\n* Provide factory methods for instantiating classes.\n\n```ts\nservices.register(Redis).to(Redis, x =\u003e {\n  const options = x.resolve(IRedisOptions);\n  return new Redis({\n    port: options.port,\n    host: options.host,\n  });\n});\n```\n\n* Use property injection with decorators for simple dependency definition.\n\n```ts\nabstract class IDependency {}\nclass Service implements IService {\n  @dependsOn(IDependency) private readonly dependency!: IDependency;\n}\n```\n\n* Provide multiple implementations for identifiers and provide a `resolveAll` method.\n* Define instance lifetime with simple builder pattern.\n\n```ts\nservices.register(IAbstract).to(Concrete).singleton();\n```\n\n* Create scopes to allow \"per-request\" lifetimes.\n\n```ts\nconst services = createServiceCollection();\nconst provider = services.buildProvider();\nusing scope = provider.createScope();\n```\n\n* Register classes during a scope\n\n```ts\nusing scope = provider.createScope();\nscope.Services.register(IContext).to(Context);\n```\n\n* Multiple registrations\n\n```ts\nservices.register(IAbstract1, IAbstract2).to(Concrete).singleton();\nconst provider = services.buildProvider();\nprovider.resolve(IAbstract1) === provider.resolve(IAbstract2);\n```\n\n* Override registrations (e.g.: for testing)\n\n```ts\nimport { ok } from 'node:assert/strict';\nconst services = createServiceCollection({ registrationMode: ResolveMultipleMode.LastRegistered });\nservices.register(IOptions).to(Options);\n// Later\nservices.register(IOptions).to(MockOptions);\nconst provider = services.buildProvider();\nconst options = provider.resolve(IOptions);\nok(options instanceof MockOptions);\n```\n\n* Override lifetimes (e.g.: for testing)\n\n```ts\nconst services = createServiceCollection({ logLevel: LogLevel.Debug });\nservices.register(IAbstract).to(Concrete).singleton();\nconst provider = services.buildProvider();\nprovider.Services.overrideLifetime(IAbstract, Lifetime.Transient);\nprovider.resolve(IAbstract) !== provider.resolve(IAbstract);\n```\n\n* Logging options\n\n```ts\nclass CustomLogger extends ILogger {\n  public override debug(message?: any, ...optionalParams: any[]): void {\n    // custom implementation  \n  }\n}\n// Override default logger\nconst services1 = createServiceCollection({ logger: new CustomLogger() });\n// Override default log level\nconst services2 = createServiceCollection({ logLevel: LogLevel.Debug });\n```\n\n* Service modules\n\n```ts\nclass IAbstract {}\nclass Concrete extends IAbstract {}\n\nclass MyModule implements IServiceModule {\n  public registerServices(services: IServiceCollection): void {\n    services.register(IAbstract).to(Concrete);\n  }\n}\n\nconst services = createServiceCollection();\nservices.registerModules(MyModule);\nconst provider = services.buildProvider();\nconst svc = provider.resolve(IAbstract);\n```\n\n## Usage\n\nCheck the test files for different usage scenarios.\n\n```ts\nimport { dependsOn, createServiceCollection, IServiceModule, type IServiceCollection } from '@shellicar/core-di';\n\n// Define the dependency interface\nabstract class IClock {\n  abstract now(): Date;\n}\n// And implementation\nclass DefaultClock implements IClock {\n  now(): Date {\n    return new Date();\n  }\n}\n\n// Define your interface\nabstract class IDatePrinter {\n  abstract handle(): string;\n}\n// And implementation\nclass DatePrinter implements IDatePrinter {\n  @dependsOn(IClock) public readonly clock!: IClock;\n\n  handle(): string {\n    return `The time is: ${this.clock.now().toISOString()}`;\n  }  \n}\n\nclass TimeModule extends IServiceModule {\n  public registerServices(services: IServiceCollection): void {\n    services.register(IClock).to(DefaultClock).singleton();\n    services.register(IDatePrinter).to(DatePrinter).scoped();\n  }\n}\n\n// Register and build provider\nconst services = createServiceCollection();\nservices.registerModules([TimeModule]);\nconst sp = services.buildProvider();\n\n// Optionally create a scope\nusing scope = sp.createScope();\n\n// Resolve the interface\nconst svc = scope.resolve(IDatePrinter);\nconsole.log(svc.handle());\n```\n\n## Credits \u0026 Inspiration\n\n* [InversifyJS](https://github.com/inversify/InversifyJS)\n* [Microsoft.Extensions.DependencyInjection](https://github.com/dotnet/runtime/tree/main/src/libraries/Microsoft.Extensions.DependencyInjection)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshellicar%2Fcore-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshellicar%2Fcore-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshellicar%2Fcore-di/lists"}