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

Swift 4で行うDTOとDAOの相互変換

Yu Sugawara
September 23, 2017

Swift 4で行うDTOとDAOの相互変換

Yu Sugawara

September 23, 2017
Tweet

More Decks by Yu Sugawara

Other Decks in Technology

Transcript

  1. VO, DTO, POSO, DAOͱ͸ 4 VO (Value Object) 4 getterͷΈ

    4 ෆม VO, DTO, POSO, DAO, Entity ͷҧ͍
  2. VO, DTO, POSO, DAOͱ͸ 4 DTO (Data Transfer Object) 4

    VO + setter 4 Մมɻ֎͔ΒมߋՄೳ 4 ҟͳΔϨΠϠʔؒʢϞσϧ૚ɺϏϡʔ૚ͳͲʣͰσʔλ Λड͚౉͢ͷʹ࢖͏ VO, DTO, POSO, DAO, Entity ͷҧ͍
  3. VO, DTO, POSO, DAOͱ͸ 4 POSO (Plain Old Swift Object)

    4 VO΍DTOͷੑ࣭Λ࣋ͭ 4 Swift struct, enum 4 ໋໊ͷ༝དྷ͸POJO(Plain Old Java Object) VO, DTO, POSO, DAO, Entity ͷҧ͍
  4. VO, DTO, POSO, DAOͱ͸ 4 DAO (Data Access Object) 4

    DBʹΞΫηε͠஋Λߋ৽ͨ͠Γ݁ՌΛฦ͢ VO, DTO, POSO, DAO, Entity ͷҧ͍
  5. // 1. ΦϒδΣΫτΛ࡞੒͢Δ Person *author = [[Person alloc] init]; author.name

    = @"David Foster Wallace"; // 2. ΦϒδΣΫτΛRealmʹ௥Ճ͢Δ [realm beginWriteTransaction]; [realm addObject:author]; [realm commitWriteTransaction]; // author͸DAOʹͳΔ // 3. อଘͨ͠authorΛίϐʔ Person *copiedAuthor = [author deepcopy]; XCTAssertNil(copiedAuthor.realm); // true XCTAssertEqualObjects(author, copiedAuthor); // true // 4. τϥϯβΫγϣϯ͕ͳͯ͘΋มߋՄೳ copiedAuthor = @"other name"; // DTO
  6. ࣮૷: Swift 3࣌୅ 4 ObjectMapperΛ࢖༻ 4 Object to JSON /

    JSON to Object͕Մೳ 4 SwiftͷStructʹ΋ద༻Ͱ͖Δʂ
  7. TransformableObject public protocol TransformableObject { associatedtype TransformedObject } public extension

    TransformableObject where Self: Mappable, TransformedObject: Mappable { func transformToObject() -> TransformedObject? { return TransformedObject(JSON: toJSON()) } }
  8. ڞ௨͢Δgetter/setterͷprotocol protocol ObjectType: Mappable { var bool: Bool { get

    set } var int: Int { get set } var float: Float { get set } var double: Double { get set } var string: String { get set } var date: Date { get set } var data: Data { get set } }
  9. ObjectMapperͷϚοϐϯά extension ObjectType { mutating func mapping(map: Map) { bool

    <- map["bool"] int <- map["int"] float <- map["float"] double <- map["double"] string <- map["string"] date <- (map["date"], DateTransform()) data <- (map["data"], DataTransform()) } }
  10. POSOͷఆٛ struct Struct: ObjectType { var bool = false var

    int = 0 var float: Float = 0 var double: Double = 0 var string = "" var date = Date() var data = Data() init?(map: Map) { } }
  11. DAOͷఆٛ class RealmObject: Object, ObjectType { dynamic var bool =

    false dynamic var int = 0 dynamic var float: Float = 0 dynamic var double: Double = 0 dynamic var string = "" dynamic var date = Date() dynamic var data = Data() required convenience init?(map: Map) { self.init() } }
  12. TransformableObjectʹ४ڌ extension Struct: TransformableObject { typealias TransformedObject = RealmObject }

    extension RealmObject: TransformableObject { typealias TransformedObject = Struct }
  13. Struct → RealmSwift.Object let obj = Struct(JSON: JSONCreator.json())! let realmObj

    = obj.transformToObject()! RealmSwift.Object → Struct let realmObj = RealmObject(JSON: JSONCreator.json())! let obj = obj.transformToObject()!
  14. ར༻γʔϯ let editVC = R.storyboard .ownerEditViewController .instantiateInitialViewController()! editVC.owner = userList.owner!.transformToObject()

    // 1. DAO ! POSO editVC.ownerUpdated .map { $0.transformToObject() } // 2. POSO ! DTO .bind(to: Realm.rx.add(update: true)) .disposed(by: disposeBag)
  15. ࣮૷: Swift 4࣌୅ 4 Codable protocolͷొ৔ 4 Swift 4ͷFoundationʹCodable͕௥Ճ͞Εͨ 4

    Codable͸class, struct, enumͷ͢΂ͯͷγϦΞϥΠ ζ/σγϦΞϥΠζΛՄೳʹ͍ͯ͠Δ
  16. ࣮૷: Swift 4࣌୅ 4 Codable protocolͷొ৔ 4 Swift 4ͷFoundationʹCodable͕௥Ճ͞Εͨ 4

    Codable͸class, struct, enumͷ͢΂ͯͷγϦΞϥΠ ζ/σγϦΞϥΠζΛՄೳʹ͍ͯ͠Δ 4 ܕ҆શੑ
  17. ఆٛ public struct Location : Codable { // 1 public

    let latitude: Double public let longitude: Double } public enum Animal : Int, Codable { // 2 case chicken = 1 case dog case turkey case cow } public struct Farm : Codable { // 3 public let name: String public let location: Location public let animals: [Animal] } SE-0166
  18. ࢖༻ํ๏ let farm = Farm(name: "Old MacDonald's Farm", location: Location(latitude:

    51.621648, longitude: 0.269273), animals: [.chicken, .dog, .cow, .turkey, .dog, .chicken, .cow, .turkey, .dog]) let payload: Data = try JSONEncoder().encode(farm) // 1 do { let farm = try JSONDecoder().decode(Farm.self, from: payload) // 2 // Extracted as user types: let coordinates = "\(farm.location.latitude, farm.location.longitude)" } catch { // Encountered error during deserialization } SE-0166
  19. ObjectConvertible public protocol ObjectConvertible: Encodable {} public extension ObjectConvertible {

    func convert<T: Decodable>() throws -> T { return try JSONDecoder().decode( T.self, from: try JSONEncoder().encode(self)) } }
  20. Ϟσϧڞ௨ͷgetter protocol ObjectType { var bool: Bool { get }

    var int: Int { get } var float: Float { get } var double: Double { get } var string: String { get } }
  21. POSOͷఆٛ struct Object: ObjectType, Codable, ObjectConvertible { var bool: Bool

    var int: Int var float: Float var double: Double var string: String }
  22. DAOͷఆٛ @objcMembers class RealmObject: RealmSwift.Object, ObjectType, Codable, ObjectConvertible { dynamic

    var bool = false dynamic var int = 0 dynamic var float: Float = 0 dynamic var double: Double = 0 dynamic var string = "" }
  23. POSOͷม׵ extension Object { /// 1. POSO ! VO var

    VO: ObjectType { return self } /// 2. POSO ! DAO(DTO) func convertToDAO() throws -> RealmObject { return try convert() } }
  24. DAOͷม׵ extension RealmObject { /// 1. DAO ! POSO func

    convertToPOSO() throws -> Object { return try convert() } /// 2. DAO ! DTO func convertToDTO() throws -> RealmObject { return try convert() } /// 3. DAO ! VO func convertToVO() throws -> ObjectType { return try convertToDTO() } }
  25. ม׵ͷࣦഊ // Swift.DecodingError.keyNotFound let obj: RealmObject = try! Person().convert() 4

    ΋ͪΖΜఆ͕ٛҰக͠ͳ͍Ϟσϧؒͷม׵͸ɺσίʔυ· ͨ͸Τϯίʔυͷྫ֎͕ൃੜ͢Δ
  26. ม׵ͷࣦഊ // Swift.DecodingError.keyNotFound let obj: RealmObject = try! Person().convert() 4

    ΋ͪΖΜఆ͕ٛҰக͠ͳ͍Ϟσϧؒͷม׵͸ɺσίʔυ· ͨ͸Τϯίʔυͷྫ֎͕ൃੜ͢Δ 4 ݱঢ়ͷҊͰ͸ObjectConvertibleͰdecodeՄೳͳܕ͸੍ ݶͤͣɺϓϩάϥϚͷ੹೚Ͱconvert()Λ࢖༻͢Δͱ͍͏ ํ਑