-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor: Extract service layer from template command #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
NiklasRosenstein
wants to merge
32
commits into
develop
Choose a base branch
from
refactor/service-layer-extraction
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
21e991b
Phase 1: Add core infrastructure for service layer
NiklasRosenstein 2d41e67
Phase 2: Add domain models and error types
NiklasRosenstein 6965a74
Phase 3: Extract ManifestLoader and NamespaceResolver services
NiklasRosenstein a12e5ba
Phase 4: Add advanced services (Templating, Profile, KubernetesApply)
NiklasRosenstein 1195071
Integrate ManifestLoaderService into template and add commands
NiklasRosenstein adc3b19
Integrate NamespaceResolverService into template command
NiklasRosenstein f1aa7f7
Integrate KubernetesApplyService into template command
NiklasRosenstein 03aea59
fmt lint
NiklasRosenstein 794290d
Address GitHub Copilot code review feedback
NiklasRosenstein 16b5816
Integrate TemplatingService into template command
NiklasRosenstein 3844ddf
Integrate ProfileService into run command
NiklasRosenstein 9c22659
Fix type annotations in NamespaceResolverService
NiklasRosenstein 54eec37
Fix remaining type errors in production code
NiklasRosenstein e1c48c2
Fix all type annotations in test files
NiklasRosenstein 5ff8346
update uv.lock
NiklasRosenstein 3403987
Remove low-value tests
NiklasRosenstein 67c6b87
fmt
NiklasRosenstein 15e59bf
fix DI example doctest
NiklasRosenstein a7ea59e
Complete migration from PROVIDER to DIContainer
NiklasRosenstein f69c8d4
Integrate ExecutionContext and TemplateContext throughout all commands
NiklasRosenstein 0d20d40
remove .claude and .tire
NiklasRosenstein 6e8d96a
Remove old DI system and complete migration to single DIContainer
NiklasRosenstein 24deb15
remove CLAUDE.md
NiklasRosenstein 8394e51
Remove ExecutionContext from commands
NiklasRosenstein c4cbdaf
Replace TemplateContext boolean fields with Literal mode
NiklasRosenstein 8fae40f
Delete ExecutionContext class
NiklasRosenstein 52995c8
Code quality improvements for DI system
NiklasRosenstein 74bc41b
Simplify dependency injection documentation
NiklasRosenstein dc59d02
fmt
NiklasRosenstein 95767d3
Remove duplicate namespace-finding logic from KubernetesApplyService
NiklasRosenstein 4b7c2c7
Integrate TemplatingService into template command
NiklasRosenstein 1adb143
add CLAUDE.md
NiklasRosenstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Lint code with `tire lint`, format with `tire fmt (--fix)` and type-check with `tire check` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Dependency Injection in Nyl | ||
|
|
||
| ## Overview | ||
|
|
||
| Nyl uses a custom dependency injection (DI) system to manage dependencies across commands and services. Each command creates its own request-scoped container, ensuring clean separation of concerns and making testing easier. | ||
|
|
||
| ## Core Concepts | ||
|
|
||
| ### DIContainer | ||
|
|
||
| The `DIContainer` class (`src/nyl/core/di.py`) provides: | ||
|
|
||
| - **Factory registration**: Create instances on demand | ||
| - **Singleton registration**: Reuse pre-created instances | ||
| - **Type-safe resolution**: Resolve dependencies by type | ||
|
|
||
| ```python | ||
| from nyl.core import DIContainer | ||
|
|
||
| container = DIContainer() | ||
|
|
||
| # Register a factory | ||
| container.register_factory(MyService, lambda: MyService()) | ||
|
|
||
| # Register a singleton | ||
| container.register_singleton(Config, Config()) | ||
|
|
||
| # Resolve dependencies | ||
| service = container.resolve(MyService) | ||
| ``` | ||
|
|
||
| ### Container Setup Functions | ||
|
|
||
| **setup_base_container()** - Registers core dependencies: | ||
| - `ProfileManager` | ||
| - `ProjectConfig` | ||
| - `SecretsConfig` | ||
| - `ApiClient` | ||
|
|
||
| **setup_service_container()** - Registers service layer: | ||
| - `ManifestLoaderService` | ||
| - `NamespaceResolverService` | ||
| - `KubernetesApplyService` | ||
|
|
||
| ## Adding New Commands | ||
|
|
||
| ```python | ||
| from pathlib import Path | ||
| from nyl.core import DIContainer, setup_base_container | ||
| from nyl.project.config import ProjectConfig | ||
|
|
||
| @app.command() | ||
| def my_command() -> None: | ||
| # Create request-scoped container | ||
| container = DIContainer() | ||
| setup_base_container(container) | ||
|
|
||
| # Resolve dependencies | ||
| project = container.resolve(ProjectConfig) | ||
|
|
||
| # ... command logic ... | ||
| ``` | ||
|
|
||
| **Key principles:** | ||
| - Create new `DIContainer()` for each command invocation | ||
| - Call `setup_base_container()` for core dependencies | ||
| - Resolve services via `container.resolve()` | ||
|
|
||
| ## Adding New Services | ||
|
|
||
| 1. **Create service class** in `src/nyl/services/`: | ||
|
|
||
| ```python | ||
| class MyNewService: | ||
| def do_something(self) -> None: | ||
| pass | ||
| ``` | ||
|
|
||
| 2. **Register in container_setup.py**: | ||
|
|
||
| ```python | ||
| def setup_service_container(container: DIContainer, **kwargs) -> None: | ||
| # Stateless service - use singleton | ||
| container.register_singleton(MyNewService, MyNewService()) | ||
|
|
||
| # OR with dependencies: | ||
| def _create_service() -> MyNewService: | ||
| dep = container.resolve(Dependency) | ||
| return MyNewService(dep) | ||
|
|
||
| container.register_factory(MyNewService, _create_service) | ||
| ``` | ||
|
|
||
| 3. **Use in commands**: | ||
|
|
||
| ```python | ||
| service = container.resolve(MyNewService) | ||
| service.do_something() | ||
| ``` | ||
|
|
||
| ## Best Practices | ||
|
|
||
| - **Request-scoped containers**: Always create new container per command | ||
| - **Factory vs Singleton**: Use factories for lazy init, singletons for stateless services | ||
| - **Type safety**: Always resolve by type, not string | ||
|
|
||
| ## Further Reading | ||
|
|
||
| - `src/nyl/core/di.py` - DIContainer implementation | ||
| - `src/nyl/core/container_setup.py` - Setup functions | ||
| - `src/nyl/services/` - Service implementations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NiklasRosenstein marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.