Introduction to DITranquillity
DITranquillity is a sleek and robust dependency injection library designed for Swift. The library's name, "Tranquillity," reflects its core principles of clarity, simplicity, and security. By using DITranquillity, developers can manage their dependencies with peace of mind, ensuring their projects are organized and maintainable.
Understanding Dependency Injection
Dependency Injection (DI) is a design pattern used to deliver dependencies to an object rather than having the object create them itself. It's a subset of the Inversion Of Control (IoC) principle and supports the Dependency Inversion Principle. This design approach helps in creating flexible and scalable software architectures by decoupling components.
Key Features of DITranquillity
Core Features
- Component and Service Registration: Simplifies the process of defining and registering components and services within the application.
- Injection Types: Supports Initializer, Property, and Method Injections.
- Advanced Injections: Includes options for optional, argument, tag-specific, and many injections.
- Delayed Injection: Allows deferring the injection until it's necessary.
- Circular Dependency Handling: Manages circular dependencies smoothly.
- Scope and Lifetime Control: Lets developers define lifecycles and scopes for their objects.
- Modular Design: Facilitates the creation of modular applications.
- Detailed Logs: Offers comprehensive logging to track DI operations.
- Thread Safety: Ensures thread-safe operations within the application.
- Container Hierarchy: Supports hierarchical organization of containers for better management.
UI Features
- Storyboard and Storyboard References: Provides integration with storyboards.
- View Injection: Offers simple injections for subviews and cells.
Graph API
- Dependency Graph Retrieval: Enables retrieving the dependency graph.
- Graph Validation: Assists in validating the dependency graph.
- Graph Visualization: Future plans include visualizing the dependency graph.
Installation
DITranquillity supports three major package managers, making it easy to integrate into various projects:
- CocoaPods: Add
pod 'DITranquillity'
to the Podfile. - Swift Package Manager (SwiftPM): Add the package directly via Xcode or edit the
Package.swift
file. - Carthage: Include
github "ivlevAstef/DITranquillity"
in the Cartfile.
Usage Example
DITranquillity allows developers to write dependency descriptions separately from the main application code. Here’s a simple example illustrating how to set up and use DITranquillity with a VIPER architecture screen:
let container = DIContainer()
container.register(LoginRouter.init)
container.register(LoginPresenterImpl.init)
.as(LoginPresenter.self)
.lifetime(.objectGraph)
container.register(LoginViewController.init)
.injection(cycle: true, \.presenter)
.as(LoginView.self)
.lifetime(.objectGraph)
container.register(AuthInteractorImpl.init)
.as(AuthInteractor.self)
let router: LoginRouter = container.resolve()
window.rootViewController = router.rootViewController
router.start()
This code snippet showcases a clear separation between the application’s dependency setup and its logic, contributing to a more maintainable codebase.
Requirements
DITranquillity supports a range of platforms and Swift versions:
- iOS 11.0+, macOS 10.13+, tvOS 11.0+, watchOS 4.0+, and Linux.
- Works smoothly with Swift versions from 5.5 to 5.9 and older versions with specific conditions.
History and Future Plans
The project has evolved through various stages:
- v1.x.x: Initial development.
- v2.x.x: Stabilization phase.
- v3.x.x: Feature enhancements.
- v4.x.x: Focus on Graph API and optimization.
- v5.x.x: Pre-compile time validation (planned).
Contributing and Feedback
Developers are encouraged to contribute by reporting issues or enhancing the documentation through GitHub. If DITranquillity proves beneficial, a star on the GitHub repo is a simple yet appreciated form of support. For queries or detailed discussions, reach out via email: [email protected].