Sobes.tech
Junior — Middle

Miks ei soovitata kasutada LocalContext.current äriloogika kihtides, nagu Use Case või repository?

sobes.tech AI

Vastus AI-lt

Use of LocalContext.current in business logic layers, such as Use Case or repository, is not recommended for several reasons:

  1. Violation of the single responsibility principle — the business layer should be independent of platform-specific or UI-specific details, and LocalContext.current is related to the UI context.

  2. Difficulties with testing — direct access to LocalContext complicates writing unit tests, as it requires a specific environment context.

  3. Loss of portability and reusability — business logic tied to a specific context is more difficult to use in other parts of the application or on other platforms.

It is better to explicitly pass the necessary data or contexts through function parameters or use dependency injection, so that the business layer remains clean and independent of the UI.