-
Notifications
You must be signed in to change notification settings - Fork 2
Fix/#324 의존성 관리 형태 수정 #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
808075e
7752459
1064fda
15c902b
bf16347
7cd1cfe
f03327a
37315e4
6f159a0
5b532c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,19 +8,33 @@ | |
|
|
||
| import Foundation | ||
|
|
||
| public final class DIContainer { | ||
| static var storage: [String: Any] = [:] | ||
|
|
||
| private init() { } | ||
| import FirebaseInterface | ||
|
|
||
| public enum DIContainer { | ||
| private static var storage: [String: Any] = [:] | ||
| private static var lock: NSLock = .init() | ||
| private static var firebaseLogger: FirebaseLogger? | ||
| public static func setLogger(_ logger: FirebaseLogger) { | ||
| firebaseLogger = logger | ||
| } | ||
|
|
||
| public static func register<T>(type: T.Type, _ object: T) { | ||
| storage["\(type)"] = object | ||
| public static func register<Dependency>( | ||
| type: Dependency.Type, | ||
| _ instance: Dependency | ||
| ) { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| storage[String(describing: Dependency.self)] = instance | ||
|
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 중복 등록 감지 로직이 없습니다. 동일한 - storage[String(describing: Dependency.self)] = instance
+ let key = String(describing: Dependency.self)
+ #if DEBUG
+ if storage[key] != nil {
+ assertionFailure("DIContainer: '\(key)' 가 이미 등록되어 있습니다. 중복 등록을 확인하세요.")
+ }
+ #endif
+ storage[key] = instance🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| static func resolve<T>(type: T.Type) -> T { | ||
| guard let object = storage["\(type)"] as? T else { | ||
| static func resolve<Dependency>(type: Dependency.Type) -> Dependency { | ||
| let typeName = String(describing: Dependency.self) | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| guard let savedInstance = storage[typeName] as? Dependency else { | ||
| firebaseLogger?.log(name: "DependencyCrash", parameter: ["type": typeName]) | ||
| fatalError("register 되지 않은 객체 호출: \(type)") | ||
| } | ||
| return object | ||
| return savedInstance | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,14 +9,10 @@ | |||||||||||||||||||||||||||||||||||||||||||
| import Foundation | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @propertyWrapper | ||||||||||||||||||||||||||||||||||||||||||||
| public struct Injected<T> { | ||||||||||||||||||||||||||||||||||||||||||||
| private var type: T.Type | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| public var wrappedValue: T { | ||||||||||||||||||||||||||||||||||||||||||||
| DIContainer.resolve(type: type) | ||||||||||||||||||||||||||||||||||||||||||||
| public struct Injected<Dependency> { | ||||||||||||||||||||||||||||||||||||||||||||
| public var wrappedValue: Dependency { | ||||||||||||||||||||||||||||||||||||||||||||
| DIContainer.resolve(type: Dependency.self) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| public init(_ type: T.Type) { | ||||||||||||||||||||||||||||||||||||||||||||
| self.type = type | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| public init() { } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+12
to
18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion
현재 구현은 프로퍼티 접근마다 @propertyWrapper
public struct Injected<Dependency> {
- public var wrappedValue: Dependency {
- DIContainer.resolve(type: Dependency.self)
- }
+ // 최초 1회만 DIContainer 에서 해석
+ private lazy var cached: Dependency = {
+ DIContainer.resolve(type: Dependency.self)
+ }()
+
+ public var wrappedValue: Dependency { cached }
public init() { }
}장점 단점 검토 부탁드립니다. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,23 +11,19 @@ import Foundation | |
| import CoreDataService | ||
| import Domain | ||
| import NetworkService | ||
| import Core | ||
|
|
||
| import RxSwift | ||
|
|
||
| public final class DefaultFavoritesRepository: FavoritesRepository { | ||
| private let coreDataService: CoreDataService | ||
| private let networkService: NetworkService | ||
| @Injected private var coreDataService: CoreDataService | ||
| @Injected private var networkService: NetworkService | ||
|
|
||
| public var favorites = BehaviorSubject<[FavoritesBusResponse]>(value: []) | ||
|
|
||
| private let disposeBag = DisposeBag() | ||
|
|
||
| public init( | ||
| coreDataService: CoreDataService, | ||
| networkService: NetworkService | ||
| ) { | ||
| self.coreDataService = coreDataService | ||
| self.networkService = networkService | ||
| public init() { | ||
| bindStoreStatus() | ||
| } | ||
|
Comment on lines
+26
to
28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 초기화 시점에
🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
중복 등록으로 이전 인스턴스가 은폐됩니다.
RegularAlarmEditingService가 두 번 등록되고 있습니다. 두 번째 호출이 첫 번째 인스턴스를 덮어쓰므로, 불필요한 객체 생성 + 디버깅 난이도 상승 위험이 있습니다. 실제로 서로 다른 구현체를 주입하려는 의도가 아니라면 한 번만 등록하도록 수정하세요.Also applies to: 38-38
🤖 Prompt for AI Agents