Skip to content

Instantly share code, notes, and snippets.

@fumiyasac
Created May 24, 2020 09:13
Show Gist options
  • Save fumiyasac/2e3935496d9faffdc24a63a15cf7d804 to your computer and use it in GitHub Desktop.
Save fumiyasac/2e3935496d9faffdc24a63a15cf7d804 to your computer and use it in GitHub Desktop.
PropertyWrapperを利用したDependendyInjectionの例(実装側)
// (1) DependenciesDefinition.swift
// それぞれのクラスの依存関係を定義する
import Foundation
final class DependenciesDefinition {
// MARK: - Function
// MEMO: PropertyWrapperを利用したDependencyInjectionを実施する
func inject() {
let container = Dependencies.Container.default
// MEMO: 命名で厳密に縛りたい場合は第2引数にこのような名前をつけてあげる
// DI実行側 → container.register(RealmAccessManager.shared, Dependencies.Name(rawValue: "RealmAccessProtocol"))
// DI定義側 → @Dependencies.Inject(Dependencies.Name(rawValue: "RealmAccessProtocol")) private var realmAccessManager: RealmAccessProtocol
// ※ Dependencies.Nameの対応を正しく定義していないとクラッシュが発生するような形にしている
// MEMO: Infrastructure層の登録
let managers: Array<(implInstance: Any, protocolName: Any)> = [
(
implInstance: APIRequestManager.shared,
protocolName: APIRequestProtocol.self
),
// ... 以下同様に依存関係をここに登録する ...
]
let _ = managers.map{ manager in
container.register(
manager.implInstance,
for: Dependencies.Name(rawValue: TypeScanner.getName(manager.protocolName))
)
}
// MEMO: Repository層の登録
let repositories: Array<(implInstance: Any, protocolName: Any)> = [
(
implInstance: AnnoucementRepositoryImpl(),
protocolName: AnnoucementRepository.self
),
// ... 以下同様に依存関係をここに登録する ...
]
let _ = repositories.map{ repository in
container.register(
repository.implInstance,
for: Dependencies.Name(rawValue: TypeScanner.getName(repository.protocolName))
)
}
// MEMO: UseCase層の登録
let useCases: Array<(implInstance: Any, protocolName: Any)> = [
(
implInstance: AnnouncementUseCaseImpl(),
protocolName: AnnouncementUsecase.self
)
// ... 以下同様に依存関係をここに登録する ...
]
let _ = useCases.map{ useCase in
container.register(
useCase.implInstance,
for: Dependencies.Name(rawValue: TypeScanner.getName(useCase.protocolName))
)
}
// MEMO: ViewModel層の登録
let viewModels: Array<(implInstance: Any, protocolName: Any)> = [
(
implInstance: AnnouncementViewModel(),
protocolName: AnnouncementViewModelType.self
)
// ... 以下同様に依存関係をここに登録する ...
]
let _ = viewModels.map{ viewModel in
container.register(
viewModel.implInstance,
for: Dependencies.Name(rawValue: TypeScanner.getName(viewModel.protocolName))
)
}
}
}
// (2) AppDelegate.swift
// DependenciesDefinition.swiftに記載した依存関係の定義を読み込む
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// MEMO: Dependency Injection用の処理を初期化する
let domainDependency = DependenciesDefinition()
domainDependency.inject()
return true
}
// ... 以下省略 ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment