arkavo-org / arkavo-org/app

🔴 CRITICAL: Replace unsafe ServiceLocator with proper DI

Open
#208 0 comments 0 reactions 0 assignees View on GitHub
enhancement tech debt
Dominant language
Swift
Stars
0
Forks
0
Avg merge
1h 41m
Merged PRs (30d)
1

Description

## Problem

The current `ServiceLocator` pattern in `ViewModelFactory` uses string-based type resolution with `fatalError` on missing services.

## Location

`ArkavoApp.swift` lines 1033-1174

## Current Implementation

```swift
class ServiceLocator {
private var services: [String: Any] = [:]

func register(_ service: T) {
let key = String(describing: T.self) // String-based - no compile-time safety
services[key] = service
}

func resolve() -> T {
let key = String(describing: T.self)
guard let service = services[key] as? T else {
fatalError("No registered service for type \(T.self)") // Crash!
}
return service
}
}
```

## Issues

1. **String-based resolution** - No compile-time type checking
2. **fatalError on missing service** - Crashes instead of graceful handling
3. **No lifecycle management** - Services live forever
4. **Hidden dependencies** - ViewModels access `ViewModelFactory.shared` directly
5. **Untestable** - Cannot mock services for unit tests

## Proposed Solution

Implement a proper dependency injection container:

```swift
protocol DIContainer {
func register(_ type: T.Type, factory: @escaping () -> T)
func resolve(_ type: T.Type) throws -> T
}

// Usage with property wrappers
@Injected var messageRouter: MessageRouting
```

## Acceptance Criteria

- [ ] Type-safe service registration (compile-time checked)
- [ ] Graceful error handling on missing services
- [ ] Services injectable via initializers or property wrappers
- [ ] All ViewModels can be instantiated with mock dependencies
- [ ] Unit tests pass with mocked services

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading ArkavoApp.swift lines 1033-1174 and trace how ViewModelFactory and its ViewModels obtain services. Run the existing unit-test suite to identify current coverage and mocking constraints. The work is done when the container is type-safe, missing services fail gracefully, ViewModels accept injectable dependencies, and tests pass with mocks.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
developer-experience
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.