{"id":32150853,"url":"https://github.com/nslogmeng/swift-service","last_synced_at":"2026-01-21T09:03:59.579Z","repository":{"id":310535411,"uuid":"1040248230","full_name":"nslogmeng/swift-service","owner":"nslogmeng","description":"A lightweight, zero-dependency, type-safe dependency injection framework for modern Swift.","archived":false,"fork":false,"pushed_at":"2026-01-16T15:44:02.000Z","size":2428,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-16T20:36:08.179Z","etag":null,"topics":["dependency","dependency-injection","ioc-container","ios","ios-development","service-locator","spm","swift","swift-concurrency","swift-package-manager","swift6","swiftui"],"latest_commit_sha":null,"homepage":"https://nslogmeng.github.io/swift-service/documentation/service/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=repo-about","language":"Swift","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/nslogmeng.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2025-08-18T17:20:11.000Z","updated_at":"2026-01-16T15:44:06.000Z","dependencies_parsed_at":"2025-08-18T19:25:23.173Z","dependency_job_id":"9e57a3d0-b4f3-4033-8f29-04d3a18ecab0","html_url":"https://github.com/nslogmeng/swift-service","commit_stats":null,"previous_names":["nslogmeng/service"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/nslogmeng/swift-service","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nslogmeng%2Fswift-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nslogmeng%2Fswift-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nslogmeng%2Fswift-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nslogmeng%2Fswift-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nslogmeng","download_url":"https://codeload.github.com/nslogmeng/swift-service/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nslogmeng%2Fswift-service/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28597357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T02:08:49.799Z","status":"ssl_error","status_checked_at":"2026-01-20T02:08:44.148Z","response_time":117,"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":["dependency","dependency-injection","ioc-container","ios","ios-development","service-locator","spm","swift","swift-concurrency","swift-package-manager","swift6","swiftui"],"created_at":"2025-10-21T10:26:12.237Z","updated_at":"2026-01-21T09:03:59.574Z","avatar_url":"https://github.com/nslogmeng.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"./images/logo.png\" alt=\"Service Logo\"\u003e\n\u003c/div\u003e\n\n# Service\n\n[![Swift Version Status](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fnslogmeng%2Fswift-service%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/nslogmeng/swift-service)\n[![Platform Support Status](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fnslogmeng%2Fswift-service%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/nslogmeng/swift-service)\n[![Build Status](https://github.com/nslogmeng/swift-service/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/nslogmeng/swift-service/actions/workflows/build.yml)\n[![Test Status](https://github.com/nslogmeng/swift-service/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/nslogmeng/swift-service/actions/workflows/test.yml)\n[![Documentation](https://img.shields.io/badge/Documentation-available-blue)](https://nslogmeng.github.io/swift-service/documentation/service/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-badge)\n[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/nslogmeng/swift-service)\n\n\u003cdiv align=\"center\"\u003e\n    \u003cstrong\u003eEnglish\u003c/strong\u003e | \u003ca href=\"./README.zh-Hans.md\"\u003e\u003cstrong\u003e简体中文\u003c/strong\u003e\u003c/a\u003e\n\u003c/div\u003e\n\u003cbr/\u003e\n\nA lightweight, zero-dependency, type-safe dependency injection framework designed for modern Swift projects.  \nInspired by [Swinject](https://github.com/Swinject/Swinject) and [swift-dependencies](https://github.com/pointfreeco/swift-dependencies), Service leverages modern Swift features for simple, robust dependency injection. **Extremely easy to learn** with familiar register/resolve patterns, elegant dependency injection through property wrappers.\n\n## ✨ Core Features\n\n- **🚀 Modern Swift**: Uses property wrappers, TaskLocal, and concurrency primitives, fully leverages modern Swift features\n- **🎯 Simple API, Ready to Use**: Use `@Service` property wrapper, no manual dependency passing needed, cleaner code\n- **📦 Zero Dependencies, Lightweight**: No third-party dependencies, adds no burden to your project, perfect for any Swift project\n- **🔒 Type-Safe, Compile-Time Checked**: Leverages Swift's type system to catch errors at compile time\n- **⚡ Thread-Safe, Concurrency-Friendly**: Built-in thread safety guarantees, perfect support for Swift 6 concurrency model\n- **🌍 Environment Isolation, Test-Friendly**: Task-level environment switching based on TaskLocal, easily swap dependencies in tests\n- **🎨 MainActor Support**: Dedicated `@MainService` API for SwiftUI view models and UI components\n- **🔍 Automatic Circular Dependency Detection**: Runtime detection of circular dependencies with clear error messages\n- **🧩 Modular Assembly**: Organize service registrations through ServiceAssembly pattern for clearer code structure\n\n## 📦 Installation\n\nAdd to your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/nslogmeng/swift-service\", .upToNextMajor(from: \"1.0.0\"))\n],\ntargets: [\n    .target(\n        name: \"MyProject\",\n        dependencies: [\n            .product(name: \"Service\", package: \"swift-service\"),\n        ]\n    )\n]\n```\n\n## 🚀 Quick Start\n\nGet started with Service in just three steps:\n\n### 1. Register Services\n\n```swift\nimport Service\n\n// Register a service (supports both protocols and concrete types)\nServiceEnv.current.register(DatabaseProtocol.self) {\n    DatabaseService(connectionString: \"sqlite://app.db\")\n}\n```\n\n### 2. Inject Dependencies\n\nUse the `@Service` property wrapper to automatically resolve dependencies:\n\n```swift\nstruct UserRepository {\n    @Service\n    var database: DatabaseProtocol\n    \n    func fetchUser(id: String) -\u003e User? {\n        return database.findUser(id: id)\n    }\n}\n```\n\n### 3. Use Services\n\n```swift\nlet repository = UserRepository()\nlet user = repository.fetchUser(id: \"123\")\n// database is automatically injected, no manual passing needed!\n```\n\n### 🎨 SwiftUI View Model Support\n\n```swift\n// Register MainActor service\nServiceEnv.current.registerMain(UserViewModel.self) {\n    UserViewModel()\n}\n\n// Use @MainService in your views\nstruct UserView: View {\n    @MainService\n    var viewModel: UserViewModel\n    \n    var body: some View {\n        Text(viewModel.userName)\n    }\n}\n```\n\n### 🧪 Test Environment Switching\n\n```swift\n// Switch to test environment in tests\nawait ServiceEnv.$current.withValue(.test) {\n    // Register mock services for testing\n    ServiceEnv.current.register(DatabaseProtocol.self) {\n        MockDatabase()\n    }\n    \n    // All service resolutions use test environment\n    let repository = UserRepository()\n    // Test with mock database...\n}\n```\n\n## 📚 Documentation\n\nFor comprehensive documentation, tutorials, and examples, see the [Service Documentation](https://nslogmeng.github.io/swift-service/documentation/service/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs).\n\n### Topics\n\n#### Essentials\n\n- **[Getting Started](https://nslogmeng.github.io/swift-service/documentation/service/gettingstarted/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Quick setup guide\n- **[Basic Usage](https://nslogmeng.github.io/swift-service/documentation/service/basicusage/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Core patterns and examples\n- **[Service Environments](https://nslogmeng.github.io/swift-service/documentation/service/serviceenvironments/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Managing different service configurations\n\n#### Advanced Topics\n\n- **[MainActor Services](https://nslogmeng.github.io/swift-service/documentation/service/mainactorservices/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Working with UI components\n- **[Service Assembly](https://nslogmeng.github.io/swift-service/documentation/service/serviceassembly/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Organizing service registrations\n- **[Circular Dependencies](https://nslogmeng.github.io/swift-service/documentation/service/circulardependencies/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Understanding and avoiding circular dependencies\n- **[Resetting Services](https://nslogmeng.github.io/swift-service/documentation/service/resettingservices/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Clearing caches and resetting service environments\n\n#### Examples\n\n- **[Real-World Examples](https://nslogmeng.github.io/swift-service/documentation/service/realworldexamples/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Practical use cases\n\n#### Deep Dive\n\n- **[Understanding Service](https://nslogmeng.github.io/swift-service/documentation/service/understandingservice/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Deep dive into architecture\n- **[Concurrency Model](https://nslogmeng.github.io/swift-service/documentation/service/concurrencymodel/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=service-github\u0026utm_content=readme-docs)** - Understanding Service's concurrency model\n\n## 💡 Why Service?\n\n### 🎯 Extremely Easy to Learn\n\nIf you're familiar with traditional dependency injection patterns (like Swinject), Service will feel very familiar. With property wrappers, you don't even need to manually pass dependencies:\n\n```swift\n// Traditional way: need to manually pass dependencies\nclass UserService {\n    init(database: DatabaseProtocol, logger: LoggerProtocol) { ... }\n}\nlet service = UserService(database: db, logger: logger)\n\n// Service way: automatic injection, cleaner code\nclass UserService {\n    @Service var database: DatabaseProtocol\n    @Service var logger: LoggerProtocol\n}\nlet service = UserService()  // Dependencies automatically injected!\n```\n\n### 🚀 Built for Modern Swift\n\n- **Swift 6 Concurrency Model**: Perfect support for `Sendable` and `@MainActor`, with dedicated APIs for UI services\n- **TaskLocal Environment Isolation**: Task-based environment switching, no need to modify global state in tests\n- **Property Wrappers**: Leverages modern Swift features for elegant dependency injection\n\n### 🛡️ Safe and Reliable\n\n- **Compile-Time Type Checking**: Leverages Swift's type system to catch errors at compile time\n- **Thread Safety Guarantees**: Built-in locking mechanism, supports concurrent access\n- **Circular Dependency Detection**: Automatic runtime detection and reporting of circular dependencies\n\n### 📦 Lightweight, Zero Burden\n\n- **Zero Dependencies**: No third-party dependencies, won't add complexity to your project\n- **Minimal Runtime Cost**: Efficient implementation with minimal impact on app performance\n- **Wide Applicability**: Perfect for SwiftUI apps, server-side Swift, command-line tools, and any Swift project\n\n### 🧩 Flexible and Powerful\n\n- **Multiple Registration Methods**: Supports factory functions, direct instances, and ServiceKey protocol\n- **Modular Assembly**: Organize service registrations through ServiceAssembly for clearer code structure\n- **Environment Isolation**: Production, development, and test environments are completely isolated\n\n## 📄 License\n\nThis project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnslogmeng%2Fswift-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnslogmeng%2Fswift-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnslogmeng%2Fswift-service/lists"}