{"id":20136855,"url":"https://github.com/danielfeather/ts-dependency-injection-example","last_synced_at":"2026-06-09T15:04:09.316Z","repository":{"id":116913799,"uuid":"365847781","full_name":"danielfeather/ts-dependency-injection-example","owner":"danielfeather","description":"An example of how to get a barebones DI container set up in TypeScript","archived":false,"fork":false,"pushed_at":"2021-07-08T23:06:02.000Z","size":70,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-02T22:44:14.887Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/danielfeather.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":"2021-05-09T21:04:05.000Z","updated_at":"2021-07-08T23:06:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"9224072a-a131-4e99-a7fb-843aada32d07","html_url":"https://github.com/danielfeather/ts-dependency-injection-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/danielfeather/ts-dependency-injection-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfeather%2Fts-dependency-injection-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfeather%2Fts-dependency-injection-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfeather%2Fts-dependency-injection-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfeather%2Fts-dependency-injection-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielfeather","download_url":"https://codeload.github.com/danielfeather/ts-dependency-injection-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfeather%2Fts-dependency-injection-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34112225,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-09T02:00:06.510Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":"2024-11-13T21:23:00.091Z","updated_at":"2026-06-09T15:04:09.297Z","avatar_url":"https://github.com/danielfeather.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TS Dependency Injection Example\n\nAn example of how to get a barebones DI container set up in TypeScript.\n\nI created this out of a curiosity of how dependancy injection would work in a JavaScript/TypeScript project.\n\nThe example I have created is using TypeScript, but that doesn't mean this is not viable in plain JavaScript, the main reason for using TypeScript is that it's easier to read and follow, and it gives you nice code completion in your editor and helps your spot errors you wouldn't have seen until runtime.\n\n## Components\n\nThere are a few components to this example.\n\n### Application\n\nThe application class is the starting point in the example. \n\nFirst, we instantiate the container and pass it the bindings that have been defined in config/bindings.ts\n\nThen we create an instance of the application and run it. \n\nInside the application's run method I just have a couple of lines where I request an instance of the DummyController from the container, which in turn, requests the DummyService class from the container and invokes its foo method and logs 'bar' to the console.\n\n### Container\n\nThe container is the most important part of the example its job is to fulfill requests for classes by loading and executing factories.\n\nWhen you request a class from the container using the get method, it looks up the classes factory using the config/bindings object.\n\nOnce it has the factory it will then load it using a dynamic import and then execute the factory's invoke function which will return an instance of the requested class.\n\n### Factory\n\nThe factory is the class that is responsible for instantiating the requested class and returning it. \n\nTake for example the DummyControllerFactory, it is responsible for fulfilling any requests for the DummyController class. If you open the file you will notice that it just imports the DummyController class file and then instantiates a new one, but with one difference. It has a dependency.\n\nThis is where Dependency injection is handy, if you look at the DummyControllerFactory it requests an instance of the DummyService as that is needed by the DummyController in order to work.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielfeather%2Fts-dependency-injection-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielfeather%2Fts-dependency-injection-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielfeather%2Fts-dependency-injection-example/lists"}