# Dependency Injection Setup

> Add new services to DIContainer, create protocol-based implementations, set up singletons or factories for Leavn app dependency injection

- Skill: `aibot88/dependency-injection-setup` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add aibot88/dependency-injection-setup`
- Raw SKILL.md: https://api.skillmd.com/api/skills/aibot88/dependency-injection-setup/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: aibot88 (https://skillmd.com/u/aibot88)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/aibot88/dependency-injection-setup

---


# Dependency Injection Setup

## Instructions

Add new services to Leavn's DIContainer:

1. **Create service protocol**:
   ```swift
   // Services/MyDomain/MyServiceProtocol.swift
   public protocol MyServiceProtocol {
       func doSomething() async throws -> Result
   }
   ```

2. **Create implementation**:
   ```swift
   // Services/MyDomain/MyServiceLive.swift
   public final class MyServiceLive: MyServiceProtocol {
       public init() { }

       public func doSomething() async throws -> Result {
           // Implementation
       }
   }
   ```

3. **Register in DIContainer**:
   ```swift
   // For singletons (stateful services):
   private lazy var _myService = MyServiceLive()
   var myService: MyServiceProtocol {
       _myService
   }

   // For factories (stateless services):
   var myService: MyServiceProtocol {
       MyServiceLive()
   }
   ```

4. **Use in ViewModels**:
   ```swift
   let service = DIContainer.shared.myService
   ```

Use this skill when: Creating new service, adding dependency, setting up DI, refactoring to protocol-oriented design

