From c3736be4f7a65d7fc6b7a4cd3ea1f4290eb11887 Mon Sep 17 00:00:00 2001 From: Alexander Zinn Date: Fri, 24 Oct 2025 00:57:18 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 3 ++ .prettierrc | 12 +++++ DependencyInjection/.gitignore | 34 ++++++++++++++ DependencyInjection/README.md | 74 +++++++++++++++++++++++++++++++ DependencyInjection/package.json | 17 +++++++ DependencyInjection/tsconfig.json | 29 ++++++++++++ 6 files changed, 169 insertions(+) create mode 100644 .gitignore create mode 100644 .prettierrc create mode 100644 DependencyInjection/.gitignore create mode 100644 DependencyInjection/README.md create mode 100644 DependencyInjection/package.json create mode 100644 DependencyInjection/tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ca0b3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store + +node_modules/ diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..caa814d --- /dev/null +++ b/.prettierrc @@ -0,0 +1,12 @@ +{ + "arrowParens": "avoid", + "bracketSameLine": true, + "bracketSpacing": false, + "printWidth": 160, + "semi": true, + "singleAttributePerLine": false, + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "none", + "useTabs": false +} diff --git a/DependencyInjection/.gitignore b/DependencyInjection/.gitignore new file mode 100644 index 0000000..a14702c --- /dev/null +++ b/DependencyInjection/.gitignore @@ -0,0 +1,34 @@ +# dependencies (bun install) +node_modules + +# output +out +dist +*.tgz + +# code coverage +coverage +*.lcov + +# logs +logs +_.log +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# caches +.eslintcache +.cache +*.tsbuildinfo + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/DependencyInjection/README.md b/DependencyInjection/README.md new file mode 100644 index 0000000..ea2f0e3 --- /dev/null +++ b/DependencyInjection/README.md @@ -0,0 +1,74 @@ +# Simple Dependency Injection Container + +A learning project to understand how Dependency Injection works by building one from scratch. + +Inspired by the Phenix RTS platform's DI system. + +## Learning Goals + +1. Understand how DI containers resolve dependencies +2. Learn about different lifecycle patterns (singleton, transient, factory) +3. Understand circular dependency detection +4. Learn about provider patterns +5. Understand type registration and resolution + +## Steps + +We'll build this incrementally: + +1. **Step 1**: Basic container with manual registration +2. **Step 2**: Constructor injection +3. **Step 3**: Singleton vs Transient lifecycles +4. **Step 4**: Circular dependency detection +5. **Step 5**: Provider pattern +6. **Step 6**: Named registrations +7. **Step 7**: Configuration-driven DI (like Phenix's di.json) + +## Running the Examples + +```bash +# Step 1 - Basic +node step1-basic.js + +# Step 2 - Constructor Injection +node step2-constructor-injection.js + +# Step 3 - Lifecycles +node step3-lifecycles.js + +# Step 4 - Circular Dependencies +node step4-circular-deps.js + +# Step 5 - Providers +node step5-providers.js + +# Step 6 - Named Registrations +node step6-named.js + +# Step 7 - Configuration +node step7-config.js +``` + +## Key Concepts + +### Dependency Injection + +Instead of creating dependencies inside a class, they are "injected" from outside. + +### Inversion of Control (IoC) + +The container controls object creation and lifecycle, not your code. + +### Service Locator vs DI + +DI is better because dependencies are explicit in constructor signatures. + +## Architecture + +``` +Container +├── Registry (stores type registrations) +├── Resolver (resolves dependencies) +├── Lifecycle Manager (handles singleton/transient) +└── Provider System (custom creation logic) +``` diff --git a/DependencyInjection/package.json b/DependencyInjection/package.json new file mode 100644 index 0000000..80452e0 --- /dev/null +++ b/DependencyInjection/package.json @@ -0,0 +1,17 @@ +{ + "name": "dependencyinjection", + "module": "index.ts", + "type": "module", + "private": true, + "scripts": { + "step:1": "bun run src/Step1", + "step:2": "bun run src/Step2" + }, + "devDependencies": { + "@types/bun": "latest", + "prettier": "3.6.2" + }, + "peerDependencies": { + "typescript": "5.9,3" + } +} diff --git a/DependencyInjection/tsconfig.json b/DependencyInjection/tsconfig.json new file mode 100644 index 0000000..bfa0fea --- /dev/null +++ b/DependencyInjection/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + // Environment setup & latest features + "lib": ["ESNext"], + "target": "ESNext", + "module": "Preserve", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true, + "noImplicitOverride": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +}