Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Repository pattern in Swift

B2ccb3bd2da6c15d991404b6082554d3?s=47 naoty
April 20, 2016

Repository pattern in Swift

potatotips #28

B2ccb3bd2da6c15d991404b6082554d3?s=128

naoty

April 20, 2016
Tweet

Transcript

  1. /BPUP,BOFLP 3FQPTJUPSZ QBUUFSOJO4XJGU

  2. None
  3. ϦϙδτϦύλʔϯ w ϦϙδτϦͱ͸ɺσʔλͷऔಘɺอଘɺ࡟আͷͨΊ ͷΠϯλʔϑΣΠε w ϦϙδτϦΛ࢖ͬͯσʔλʹΞΫηε͢Δɻ w ϝϞϦɺ3FBMNɺ"1*ͱ͍ͬͨόοΫΤϯυΛ஌ Δඞཁ͕ͳ͘ͳΔɻ

  4. ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository query model params JSON NSPredicate RealmObject

  5. 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? }
  6. 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 } }
  7. ໰୊఺ w σʔλ΁ͷΞΫηε͸͍͍ͩͨඇಉظ w ΫΤϦ΍ιʔτ͸Ͳ͏΍ͬͯࢦఆ͢Δͷ͔ w 3FDJQF3FQPTJUPSZ $IFG3FQPTJUPSZ  6TFS3FQPTJUPSZʜͱࣅͨΑ͏ͳίʔυΛίϐϖ

    ͢Δ͜ͱʹͳΓͦ͏
  8. ϦϙδτϦͰඇಉظॲཧΛ ͔͋ͭ͏

  9. 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> }
  10. ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository query model params JSON NSPredicate RealmObject

  11. ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> params JSON NSPredicate

    RealmObject query
  12. ൚༻తͳΫΤϦΛఆٛ͢Δ

  13. 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"]]
  14. 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> }
  15. ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> params JSON NSPredicate

    RealmObject query
  16. ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> [String: AnyObject] JSON

    NSPredicate RealmObject AnyQuery
  17. ൚༻తͳϦϙδτϦܕ Λఆٛ͢Δ

  18. ͜Ε͸Ͱ͖ͳ͍ protocol Repository { associatedtype Domain func find(id: Uint) ->

    Task<Void, Domain, ErrorType> } let repository: Repository = MemoryRecipeRepository()
  19. ܕফڈ 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>
  20. ViewController RecipeRepository WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> [String: AnyObject] JSON

    NSPredicate RealmObject AnyQuery
  21. ViewController AnyRepository<Recipe> WebAPIRecipeRepository RealmRecipeRepository Task<Void, Model, Error> [String: AnyObject] JSON

    NSPredicate RealmObject AnyQuery
  22. ࣮૷ྫ w IUUQTHJUIVCDPNOBPUZ1MBZHSPVOE USFFNBTUFS3FQPTJUPSZ

  23. Ԡ༻ྫΩϟογϡ

  24. Ωϟογϡ w ϦϙδτϦ͔Βऔಘͨ݁͠ՌΛΩϟογϡ͍ͨ͠ɻ w Ωϟογϡʹͳ͚Ε͹ผͷϦϙδτϦ͔Βऔಘͯ͠ɺ Ωϟογϡʹอଘ্ͨ͠Ͱฦ͢ɻ w Ωϟογϡʹ͋Ε͹ฦ͢ɻ w ྫϩάΠϯϢʔβʔͷऔಘͳͲ

  25. ͋Γ͕ͱ͏͍͟͝·ͨ͠