Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Repository pattern in Swift
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
naoty
April 20, 2016
Technology
6.5k
3
Share
Repository pattern in Swift
potatotips #28
naoty
April 20, 2016
More Decks by naoty
See All by naoty
Modular API Client
naoty
1
430
Timepiece
naoty
0
3.8k
Contribution to Rails
naoty
0
4.4k
久々のRailsプロジェクトで導入した開発環境
naoty
2
1.2k
Report of DIYish programming activity
naoty
1
290
How to build gems for Rails
naoty
1
190
Qiita/Kobito vs ?
naoty
0
210
Other Decks in Technology
See All in Technology
ふりかえりを 「あそび」にしたら、 学習が勝手に進んだ / Playful Retros Drive Learning
katoaz
0
310
不確実性と戦いながら見積もりを作成するプロセス/mitsumori-process
hirodragon112
1
190
試されDATA SAPPORO [LT]Claude Codeで「ゆっくりデータ分析」
ishikawa_satoru
0
230
「決め方」の渡し方 / How to hand over the "decision-making process"
pauli
7
1.2k
ログ基盤・プラグイン・ダッシュボード、全部整えた。でも最後は人だった。
makikub
4
780
AIにより大幅に強化された AWS Transform Customを触ってみる
0air
0
320
今年60歳のおっさんCBになる
kentapapa
1
240
ADOTで始めるサーバレスアーキテクチャのオブザーバビリティ
alchemy1115
2
220
AWSで2番目にリリースされたサービスについてお話しします(諸説あります)
yama3133
0
130
Claude Teamプランの選定と、できること/できないこと
rfdnxbro
1
1.4k
ブラックボックス化したMLシステムのVertex AI移行 / mlops_community_62
visional_engineering_and_design
1
290
自分をひらくと次のチャレンジの敷居が下がる
sudoakiy
5
1.9k
Featured
See All Featured
Code Review Best Practice
trishagee
74
20k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.1k
Un-Boring Meetings
codingconduct
0
250
Rails Girls Zürich Keynote
gr2m
96
14k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
170
Optimizing for Happiness
mojombo
378
71k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.2k
Side Projects
sachag
455
43k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
210
Transcript
/BPUP,BOFLP 3FQPTJUPSZ QBUUFSOJO4XJGU
None
ϦϙδτϦύλʔϯ w ϦϙδτϦͱɺσʔλͷऔಘɺอଘɺআͷͨΊ ͷΠϯλʔϑΣΠε w ϦϙδτϦΛͬͯσʔλʹΞΫηε͢Δɻ w ϝϞϦɺ3FBMNɺ"1*ͱ͍ͬͨόοΫΤϯυΛ Δඞཁ͕ͳ͘ͳΔɻ
ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository query model params JSON NSPredicate RealmObject
JO4XJGU protocol RecipeRepository { func find(id: UInt) -> Recipe? func
findAll(query: Query, sort: Sort) -> [Recipe] func save(recipes: [Recipe]) -> Recipe? func delete(recipes: [Recipe]) -> Recipe? }
JO4XJGU class MemoryRecipeRepository: RecipeRepository { let recipes = [ Recipe(id:
1, name: “…”), Recipe(id: 2, name: “…”), Recipe(id: 3, name: “…”), ] func find(id: UInt) -> Recipe? { recipes.filter { $0.id == id }.first } }
w σʔλͷΞΫηε͍͍ͩͨඇಉظ w ΫΤϦιʔτͲ͏ͬͯࢦఆ͢Δͷ͔ w 3FDJQF3FQPTJUPSZ $IFG3FQPTJUPSZ 6TFS3FQPTJUPSZʜͱࣅͨΑ͏ͳίʔυΛίϐϖ
͢Δ͜ͱʹͳΓͦ͏
ϦϙδτϦͰඇಉظॲཧΛ ͔͋ͭ͏
1SPNJTF protocol RecipeRepository { func find(id: UInt) -> Task<Void, Recipe,
ErrorType> func findAll(query: Query, sort: Sort) -> Task<Void, [Recipe], ErrorType> func save(recipes: [Recipe]) -> Task<Void, Recipe, ErrorType> func delete(recipes: [Recipe]) -> Task<Void, Recipe, ErrorType> }
ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository query model params JSON NSPredicate RealmObject
ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> params JSON NSPredicate
RealmObject query
൚༻తͳΫΤϦΛఆٛ͢Δ
OBPUZ"OZ2VFSZ let query = AnyQuery.Equal(key: “name”, value: “naoty”) query.predicate //=>
NSPredicate(format: "name == ‘naoty’") query.dictionary //=> ["name": “naoty”] let sort = AnySort.Ascending(key: “id”) sort.sortDescriptors //=> [NSSortDescriptor(key: "id", ascending: true)] sort.dictionary //=> ["sort": ["id"]]
OBPUZ"OZ2VFSZ protocol RecipeRepository { func find(id: UInt) -> Task<Void, Recipe,
ErrorType> func findAll(query: AnyQuery, sort: AnySort) -> Task<Void, [Recipe], ErrorType> func save(recipes: [Recipe]) -> Task<Void, Recipe, ErrorType> func delete(recipes: [Recipe]) -> Task<Void, Recipe, ErrorType> }
ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> params JSON NSPredicate
RealmObject query
ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> [String: AnyObject] JSON
NSPredicate RealmObject AnyQuery
൚༻తͳϦϙδτϦܕ Λఆٛ͢Δ
͜ΕͰ͖ͳ͍ protocol Repository { associatedtype Domain func find(id: Uint) ->
Task<Void, Domain, ErrorType> } let repository: Repository = MemoryRecipeRepository()
ܕফڈ struct AnyRepository<DomainType>: Repository { let _find: (id: UInt) ->
Task<Void, DomainType, ErrorType> init<T: Repository where T.Domain == DomainType>(_ repository: T) { _find = repository.find } func find(id: UInt) -> Task<Void, DomainType, ErrorType> { return _find(id) } } let repository = AnyRepository(MemoryRecipeRepository()) // repository: AnyRepository<Recipe>
ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> [String: AnyObject] JSON
NSPredicate RealmObject AnyQuery
ViewController AnyRepository<Recipe> WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> [String: AnyObject] JSON
NSPredicate RealmObject AnyQuery
࣮ྫ w IUUQTHJUIVCDPNOBPUZ1MBZHSPVOE USFFNBTUFS3FQPTJUPSZ
Ԡ༻ྫΩϟογϡ
Ωϟογϡ w ϦϙδτϦ͔Βऔಘͨ݁͠ՌΛΩϟογϡ͍ͨ͠ɻ w Ωϟογϡʹͳ͚ΕผͷϦϙδτϦ͔Βऔಘͯ͠ɺ Ωϟογϡʹอଘ্ͨ͠Ͱฦ͢ɻ w Ωϟογϡʹ͋Εฦ͢ɻ w ྫϩάΠϯϢʔβʔͷऔಘͳͲ
͋Γ͕ͱ͏͍͟͝·ͨ͠