diff --git a/projects/docs/setup/why.md b/projects/docs/setup/why.md index 2a2ec43dc..d1f2ea004 100644 --- a/projects/docs/setup/why.md +++ b/projects/docs/setup/why.md @@ -10,7 +10,9 @@ The goals of Koin are: - Provides different kind of integration from Android ecosystem, to more backend needs like Ktor - Allow to be used with annotations -## Making your Kotlin development easy and productive +## Koin in a nutshell + +### Making your Kotlin development easy and productive Koin is a smart Kotlin dependency injection library to keep you focused on your app, not on your tools. @@ -38,7 +40,7 @@ fun main() { } ``` -## Ready for Android +### Ready for Android Thanks to the Kotlin language, Koin extends the Android platform and provides new features as part of the original platform. @@ -64,23 +66,67 @@ class MyActivity : Application() { } ``` -## Powering Kotlin Multiplatform +### Powering Kotlin Multiplatform Sharing code between mobile platforms is one of the major Kotlin Multiplatform use cases. With Kotlin Multiplatform Mobile, you can build cross-platform mobile applications and share common code between Android and iOS. Koin provides multiplatform dependency injection and help build your components across your native mobile applications, and web/backend applications. -## Performances and Productivity +### Performances and Productivity Koin is a pure Kotlin framework, designed to be straight forward in terms of usage and execution. It easy to use and doesn't impact your compilation time, nor require any extra plugin configuration. -## Koin is a DI Framework +## Koin: A Dependency Injection Framework + +Koin is a popular dependency injection (DI) framework for Kotlin, offering a modern and lightweight solution for managing your application’s dependencies with minimal boilerplate code. + +### Dependency Injection vs. Service Locator + +While Koin may appear similar to a service locator pattern, there are key differences that set it apart: + +- Service Locator: A service locator is essentially a registry of available services where you can request an instance of a service as needed. It is responsible for creating and managing these instances, often using a static, global registry. + +- Dependency Injection: In contrast, Koin is a pure dependency injection framework. With Koin, you declare your dependencies in modules, and Koin handles the creation and wiring of objects. It allows for the creation of multiple, independent modules with their own scopes, making dependency management more modular and avoiding potential conflicts. + +### Koin’s Approach: A Blend of Flexibility and Best Practices + +Koin supports both DI and the Service Locator pattern, offering flexibility to developers. However, it strongly encourages the use of DI, particularly constructor injection, where dependencies are passed as constructor parameters. This approach promotes better testability and makes your code easier to reason about. + +Koin’s design philosophy is centered around simplicity and ease of setup while allowing for complex configurations when necessary. By using Koin, developers can manage dependencies effectively, with DI being the recommended and preferred approach for most scenarios. + + +### Transparency and Design Overview + +Koin is designed to be a versatile Inversion of Control (IoC) container that supports both Dependency Injection (DI) and Service Locator (SL) patterns. To provide a clear understanding of how Koin operates and to guide you in using it effectively, let’s explore the following aspects: + +#### How Koin Balances DI and SL + +Koin combines elements of both DI and SL, which may influence how you use the framework: + +1. **Global Context Usage:** By default, Koin provides a globally accessible component that acts like a service locator. This allows you to retrieve dependencies from a central registry using `KoinComponent` or `inject` functions. + +2. **Isolated Components:** Although Koin encourages the use of Dependency Injection, particularly constructor injection, it also allows for isolated components. This flexibility means you can configure your application to use DI where it makes the most sense while still taking advantage of SL for specific cases. + +3. **SL in Android Components:** In Android development, Koin often uses SL internally within components such as `Application` and `Activity` for ease of setup. From this point, Koin recommends DI, especially constructor injection, to manage dependencies in a more structured way. However, this is not enforced, and developers have the flexibility to use SL if needed. + +#### Why This Matters to You + +Understanding the distinction between DI and SL helps in managing your application’s dependencies effectively: + +- **Dependency Injection:** Encouraged by Koin for its benefits in testability and maintainability. Constructor injection is preferred as it makes dependencies explicit and enhances code clarity. + +- **Service Locator:** While Koin supports SL for convenience, especially in Android components, relying solely on SL can lead to tighter coupling and reduced testability. Koin’s design provides a balanced approach, allowing you to use SL where it’s practical but promoting DI as the best practice. + +#### Making the Most of Koin + +To use Koin effectively: + +- **Follow Best Practices:** Use constructor injection where possible to align with best practices for dependency management. This approach improves testability and maintainability. + +- **Leverage Koin’s Flexibility:** Utilize Koin’s support for SL in scenarios where it simplifies setup, but aim to rely on DI for managing core application dependencies. -Koin is a popular dependency injection framework for Kotlin. While it may seem similar to a service locator pattern, there are some key differences that set it apart. -First, a service locator is essentially a registry of available services, where you can ask for an instance of a service when you need it. The service locator is responsible for creating and managing the instances of these services. +- **Refer to Documentation and Examples:** Review Koin’s documentation and examples to understand how to configure and use DI and SL appropriately based on your project needs. -In contrast, Koin is a pure dependency injection (DI) framework. With Koin, you declare your dependencies and let Koin handle the creation and wiring of objects. Koin works by defining modules that specify how to create objects of a particular type. When you need an instance of an object, Koin looks up the appropriate module and creates the object for you. +- **Visualize Dependency Management:** Diagrams and examples can help illustrate how Koin resolves dependencies and manages them within different contexts. These visual aids can provide a clearer understanding of Koin’s internal workings. -Another key difference is that a service locator often uses a static, global registry of services, whereas Koin allows you to create multiple, independent modules with their own scopes. This makes it easier to manage dependencies and avoid potential conflicts. -Finally, Koin encourages the use of constructor injection, where dependencies are passed as constructor parameters, rather than being obtained through a service locator. This leads to better testability and makes it easier to reason about your code. -Overall, Koin is a modern, lightweight DI framework that can help you manage your application’s dependencies more easily and with less boilerplate code. \ No newline at end of file +> By providing this guidance, we aim to help you navigate Koin’s features and design choices effectively, ensuring you can leverage its full potential while adhering to best practices in dependency management. \ No newline at end of file