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
naoty
April 20, 2016
Technology
3
6.3k
Repository pattern in Swift
potatotips #28
naoty
April 20, 2016
Tweet
Share
More Decks by naoty
See All by naoty
Modular API Client
naoty
1
400
Timepiece
naoty
0
3.5k
Contribution to Rails
naoty
0
4.3k
久々のRailsプロジェクトで導入した開発環境
naoty
2
1.1k
Report of DIYish programming activity
naoty
1
220
How to build gems for Rails
naoty
1
150
Qiita/Kobito vs ?
naoty
0
190
Other Decks in Technology
See All in Technology
商品レコメンドでのexplicit negative feedbackの活用
alpicola
1
350
技術に触れたり、顔を出そう
maruto
1
150
GoogleのAIエージェント論 Authors: Julia Wiesinger, Patrick Marlow and Vladimir Vuskovic
customercloud
PRO
0
150
Goで実践するBFP
hiroyaterui
1
120
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
6
54k
シフトライトなテスト活動を適切に行うことで、無理な開発をせず、過剰にテストせず、顧客をビックリさせないプロダクトを作り上げているお話 #RSGT2025 / Shift Right
nihonbuson
3
2.1k
2025年の挑戦 コーポレートエンジニアの技術広報/techpr5
nishiuma
0
140
Kotlin Multiplatformのポテンシャル
recruitengineers
PRO
2
150
0→1事業こそPMは営業すべし / pmconf #落選お披露目 / PM should do sales in zero to one
roki_n_
PRO
1
1.4k
Accessibility Inspectorを活用した アプリのアクセシビリティ向上方法
hinakko
0
180
JAWS-UG20250116_iOSアプリエンジニアがAWSreInventに行ってきた(真面目編)
totokit4
0
140
生成AI × 旅行 LLMを活用した旅行プラン生成・チャットボット
kominet_ava
0
160
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Optimizing for Happiness
mojombo
376
70k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Being A Developer After 40
akosma
89
590k
GraphQLとの向き合い方2022年版
quramy
44
13k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Done Done
chrislema
182
16k
Why Our Code Smells
bkeepers
PRO
335
57k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
Designing Experiences People Love
moore
139
23k
jQuery: Nuts, Bolts and Bling
dougneiner
62
7.6k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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 ྫϩάΠϯϢʔβʔͷऔಘͳͲ
͋Γ͕ͱ͏͍͟͝·ͨ͠