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
210
How to build gems for Rails
naoty
1
140
Qiita/Kobito vs ?
naoty
0
190
Other Decks in Technology
See All in Technology
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
110
Lambdaと地方とコミュニティ
miu_crescent
2
240
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
12k
第23回Ques_タイミーにおけるQAチームの在り方 / QA Team in Timee
takeyaqa
0
190
Datadog RUM を用いた UX 指標の監視・顧客対応への活用
imamura_ko_0314
0
110
3次元点群データ「VIRTUAL SHIZUOKA』のオープンデータ化による恩恵と協働の未来/FOSS4G Japan 2024
kazz24s
0
130
エンジニアが一生困らない ドキュメント作成の基本
naohiro_nakata
2
140
Microsoft Intune アプリのトラブルシューティング
sophiakunii
1
400
Microsoft Fabric OneLake の実体について
ryomaru0825
0
190
メールサーバ管理者のみ知る話
hinono
1
100
元旅行会社の情シス部員が教えるおすすめなre:Inventへの行き方 / What is the most efficient way to re:Invent
naospon
2
280
SREの組織類型に応じた リーダシップの考察
kenta_hi
PRO
1
620
Featured
See All Featured
Building an army of robots
kneath
302
42k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Building Your Own Lightsaber
phodgson
102
6.1k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Happy Clients
brianwarren
97
6.7k
Writing Fast Ruby
sferik
627
61k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Code Review Best Practice
trishagee
64
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 ྫϩάΠϯϢʔβʔͷऔಘͳͲ
͋Γ͕ͱ͏͍͟͝·ͨ͠