- ๐ฏ I majored in Electronic Engineering
- ๐ซ Konkuk University (2015.03 ~ 2021.08)
practice-architecture's Introduction
practice-architecture's People
practice-architecture's Issues
DI ๊ด๋ จ ํธ๋ฌ๋ธ ์ํ - 02
2) EnviornmentObject๋ฅผ ํตํ DI๋ ์ด๋๊น์ง ์ ํ๋ ๊น?
์ง๊ธ๋ถํฐ ์์๋ก ์ฌ์ฉํ ํ๋ก์ ํธ๋ง ๋ณด๋ฉด DI๋ ๋ฌด์จ ๊ด๊ณ๊ฐ ์๋ ์ถ์ ์ ์์ง๋ง ํ์ฌ Clean-Architecture ์์ ์ DI๋ฅผ EnvrionmentObject๋ฅผ ํตํด์ ํ๊ณ ์๊ธฐ ๋๋ฌธ์ ๋์ผํ ๋ด์ฉ์ด๋ค!
import Foundation
final class AppState: ObservableObject {
@Published var count: Int = 0
}
import SwiftUI
@main
struct DI_TestApp: App {
var body: some Scene {
let appDI = AppState()
WindowGroup {
RootView()
.environmentObject(appDI)
}
}
}
์์ ๊ฐ์ ObservableObject๋ฅผ EnvironmentObject๋ก ์ฌ์ฉํ๋ฉด AppState๋ ์ด๋๊น์ง ์ ํ๋ ๊น?
RootView โโโ TestView โโ TestView2
โโโ ContentView
RootView - TestView - TestView2
๋ navigation push๋ก ์คํ์ด ์์ด๋ ํํ์ด๊ณ
RootView - ContentView
๋ UIKit ๋ฐฉ์์ RootViewController๋ฅผ ๋ฐ๊ฟ์ฃผ๊ณ ์๋ค
import SwiftUI
struct TestView: View {
@EnvironmentObject
var appState: AppState
var body: some View {
VStack {
NavigationLink(
destination: { TestView2() },
label: {
Text("Push")
}
)
Button(
action: {
appState.count += 1
},
label: {
Text("up")
}
)
}
}
}
struct TestView2: View {
@EnvironmentObject
var appState: AppState
var body: some View {
Text("\(appState.count)")
}
}
TestView์์ EnvironmentObject์ count ํ๋กํผํฐ๊ฐ์ ์ฆ๊ฐ์ํค๊ณ TestView2๋ก ํ๋ฉด์ ํ๋ฅผ ํ ๊ฒฝ์ฐ, ์ ์์ ์ผ๋ก ์ฆ๊ฐ๋ Count๊ฐ์ ํ์ธํ ์ ์๋ค. (EnviornmentObject์ ์ ๊ทผ๊ฐ๋ฅ)
private func rootViewChange() {
let window = UIApplication
.shared
.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }
window?.rootViewController = UIHostingController(rootView: ContentView())
window?.makeKeyAndVisible()
}
ํ์ง๋ง ํด๋น ๋์ ์ดํ, RootView์์ ContentView๋ก ํ๋ฉด ์ ํ์ ์๋ํ๋ฉด ์ฑ์ด Crash๊ฐ ๋๋๊ฒ์ ํ์ธํ ์ ์๋ค.
struct ContentView: View {
@EnvironmentObject
var appState: AppState
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
Text("\(appState.count)")
}
.padding()
}
}
์ด๋ RootView๋ฅผ ๋ฐ๊ฟ์ฃผ๋ ๊ณผ์ ์์ enviornmetObject๋ฅผ ๋ค์ ์ฃผ์ ํด์ฃผ์ง ์์๊ธฐ ๋๋ฌธ์ AppState์ ์ ๊ทผํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
๋ฐ๋ผ์, UIKit ๋ฐฉ์์ RootView Change๋ฅผ ์๋ํ๋ ๊ฒฝ์ฐ์๋ UIHostingViewController์ ๋ค์ .enviornmentObject()
๋ฉ์๋๋ฅผ ํตํด์ ์ฌ์ฉํ ObservableObject๋ฅผ ๋ค์ ์ฃผ์
ํด์ฃผ๊ฑฐ๋, SwiftUI ๋ฐฉ์์ Group์ ํตํด์ ๋ณ๊ฒฝํด์ฃผ์ด์ผ ํ๋ค.
ํ๋ก์ ํธ๋ ์ฌ๊ธฐ์ ํ์ธํ ์ ์์ต๋๋ค
DI ๊ด๋ จ ํธ๋ฌ๋ธ ์ํ - 1
DI์ค ๋ง๋ ๋ฌธ์
๊นํ ๋ ํฌ์ ์ฌ๋ฆฐ ์์ฃผ ์์ ํ ์ด ํ๋ก์ ํธ์์๋ ๋ฏธ์ฒ ์๊ฐํ์ง ๋ชปํ์๋๋ฐ ํ์ฌ ํ๋ก์ ํธ์ ์ ์ฉํ๋ฉด์ ์๊ฐ ํด๋ณผ๋งํ ๋ฌธ์ ๋ฅผ ๋ง์ฃผ์ณค๋ค.
1) Usecase์ ์ค๋ณต init
final class DefaultSearchMediaUseCase: SearchMediaUseCase {
private let mediaRepository: MediaRepository
private let mediaDetailRepository: MediaDetailRepository
init(
mediaRepository: MediaRepository,
mediaDetailRepository: MediaDetailRepository
) {
self.mediaRepository = mediaRepository
self.mediaDetailRepository = mediaDetailRepository
}
...
}
final class ~~ViewModel {
private let usecase: SearchMediaUsecase
init(
usecase: SearchMediaUsecase
) {
self.usecase = usecase
}
}
์ฌ๋ฌ viewModel์ ๋ง๋ค๋ค ๋ณด๋ฉด ์ค๋ณต๋๋ usecase๊ฐ ์๊ธด๋ค.
usecase์ Datatorage๋ฅผ ํฌํจํ๊ณ ์๋ Repository๊ฐ ์๋ ๊ฒฝ์ฐ, Realm์ด ์ฌ๋ฌ๋ฒ init ๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค
Realm ๊ณต์ ํํ์ด์ง์์ zero-copy design์ผ๋ก db์ ๋ณ๊ฒฝ ๋ด์ฉ์ด ๋์ผํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ชจ๋ ๊ฐ์ฒด์ ๋ฐ์๋๋ค๋ ๋ด์ฉ์ ํ์ธํด ๋ฐํ์ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ง๋ ์์์ง๋ง, ์ด์ฐจํผ ๋์ผํ DataStorage๋ฅผ ๊ฐ๋ฆฌํค๋ usecase๊ฐ ์๋ก ๋ค๋ฅธ viewModel์ด ๋ง๋ค์ด์ง ๋๋ง๋ค ์๋กญ๊ฒ ๋ง๋ค์ด์ง ํ์๊ฐ ์์๊น? ํ๋ ์๋ฌธ์ด ๋ค์๋ค.
import Foundation
final class AppDI: AppDIInterface {
private let mediaUsecase: SearchMediaUseCase = DefaultSearchMediaUseCase(
mediaRepository: MediaRepositoryImpl(),
mediaDetailRepository: MediaDetailRepositoryImpl(RealmDataStorage.shared)
)
func mediaDetailViewModel(
_ media: Media
) -> MediaDetailViewModel {
return MediaDetailViewModel(
media: media,
usecase: mediaUsecase
)
}
}
๊ธฐ์กด์ ๋ฉ์๋๋ฅผ ํตํด์ usecsae๋ฅผ ๋ง๋ค๋ ํํ์์ AppDI์ ํ๋กํผํฐ์ usecase๋ฅผ ๊ฐ์ง๊ณ ์๋๋ก ์ค์ ํ๋ค.
ํ์ง๋ง AppDI๋ ์ฑ์ด ์์ํ๋ ์์ ์ Init๋๋๋ฐ ๊ทธ๋ผ ์ ์ ๊ฐ ๋ชจ๋ ํ๋ฉด์ ๋์๋ค๋์ง ์์์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ์ ์์ ํ์๊ฐ ์๋ usecase๋ ์์ํ๋ ์์ ์ ๋ชจ๋ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
import Foundation
final class AppDI: AppDIInterface {
private lazy var mediaUsecase: SearchMediaUseCase = {
let mediaRepository: MediaRepository = MediaRepositoryImpl()
let mediaDetailRepository: MediaDetailRepository = MediaDetailRepositoryImpl(RealmDataStorage.shared)
return DefaultSearchMediaUseCase(
mediaRepository: mediaRepository,
mediaDetailRepository: mediaDetailRepository
)
}()
func mediaDetailViewModel(
_ media: Media
) -> MediaDetailViewModel {
return MediaDetailViewModel(
media: media,
usecase: mediaUsecase
)
}
}
์์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ usecase๋ฅผ lazy๋ฅผ ํตํด์ ์ฌ์ฉ๋๋ ์์ ์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ๋๋ก ์ค์ ํ๋ค.
๋ฌผ๋ก , ์ ์ ๊ฐ ๋ชจ๋ ํ๋ฉด์ ๋์๋ค๋๋ฉด ํ์ฌ ๋น์ฅ ํ์์๋ usecase๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ๋ ๋ฌธ์ ๋ ํผํ ์ ์๊ฒ ์ง๋ง ์ฑ์ด ์์ํ๋ ์์ ์ ๋ถํ์ํ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ๋ง์ ์ ์๋ค.
(๋ ์ข์ ์์ด๋์ด๊ฐ ๋ ์ค๋ฅด๋ฉด ์ถ๊ฐํ๋๋ก ํ๊ฒ ์ต๋๋ค.)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.