Slide 1

Slide 1 text

J04%$!NHVIJSP ஋ΦϒδΣΫτͷ $PEBCMFରԠ

Slide 2

Slide 2 text

ࣗݾ঺հ ‣ ໨ࠇതོ!NBHVIJSP ‣ גࣜձࣾΧΧΫίϜ ‣ ৯΂ϩά৯΂ϩά৽نࣄۀ ‣ J04ΞϓϦΤϯδχΞ

Slide 3

Slide 3 text

ࠓ೔࿩͢͜ͱ 4XJGUͰಋೖ͞Εͨ$PEBCMFΛར༻ͯ͠ ஋ΦϒδΣΫτΛؚΉܕʹ+40/σίʔυ ͨ͠ࡍͷରԠํ๏Λ͓࿩͠·͢

Slide 4

Slide 4 text

αϯϓϧίʔυ

Slide 5

Slide 5 text

struct User: Codable { let id: Int let name: String let age: Int } let json = """ { "id" : 1234, "name" : "maguhiro", "age" : 34 } """.data(using: .utf8)! let user = try! JSONDecoder() .decode(User.self, from: json) print("\(user.id)") // 1234

Slide 6

Slide 6 text

໰୊ࣄྫ Ϣʔβʔ*%Λ୯७ͳ*OUܕͰఆٛͯ͠͠· ͏ͱɺҾ਺ʹϢʔβʔ*%Λ౉ؔ͢਺Ͱ ޡͬͯಉ͡*OUܕͰ͋Δଞͷ஋Λ౉ͯ͠͠ ·͏ڪΕ͕͋Γ·͢

Slide 7

Slide 7 text

class UserRepositoryImpl { func find(_ userID: Int) -> User? { // Կ͔͠Β͔ΒσʔλΛऔಘͯ͠ฦ٫ return user } } let userID = 1234 let photoID = 1000 let user = UserRepositoryImpl().find(photoID) // ↑ޡͬͯࣸਅIDΛҾ਺ʹ౉ͯ͠͠·͍ͬͯΔ

Slide 8

Slide 8 text

ͭΒ͍఺

Slide 9

Slide 9 text

ͲͪΒ΋ಉ͡ܕͳͷͰϏϧυ ΤϥʔɾϥϯλΠϜΤϥʔʹ ͳΒͳ͍

Slide 10

Slide 10 text

Ͳ͏ͳΕ͹
 خ͍͠ʁ

Slide 11

Slide 11 text

ϏϧυΤϥʔʹͳͬͯ͘Ε Δͱخ͍͠Ͱ͢ΑͶʁ

Slide 12

Slide 12 text

ͭ·Γ

Slide 13

Slide 13 text

Ϣʔβʔ*%Λද͢ܕ͕*OUܕͰ ͸ͳ͘ɺϢʔβʔ*%Ͱ͋Δࣄ Λࣔ͢ಠࣗͷܕͰఆ͍ٛͨ͠

Slide 14

Slide 14 text

஋ΦϒδΣΫτΛ ಋೖ͠Α͏ʂ

Slide 15

Slide 15 text

஋ΦϒδΣΫτͱ͸ʁ υϝΠϯۦಈઃܭ %%% ͷઓज़తઃܭͷ ཁૉͷҰͭɻಛ௃ͱͯ͠ҎԼͷΑ͏ͳ΋ ͷ͕͋Δɻ ‣ ෆมͰ͋Δ ‣ ஋ಉ࢜Ͱൺֱ͕Մೳ ‣ ަ׵Մೳ

Slide 16

Slide 16 text

ઌఔͷίʔυʹ
 ద༻ͯ͠ΈΑ͏

Slide 17

Slide 17 text

struct UserID: Codable { let value: Int init(value: Int) { self.value = value } } struct User: Codable { let id: UserID let name: String let age: Int }

Slide 18

Slide 18 text

class UserRepositoryImpl { func find(_ userID: UserID) -> User? { // Կ͔͠Β͔ΒσʔλΛऔಘͯ͠ฦ٫ return user } } let userID = UserID(value: 1234) let photoID = 1000 let user = UserRepositoryImpl().find(photoID) // ↑ޡͬͯࣸਅIDΛҾ਺ʹ౉͢ͱɺҎԼͷΑ͏ͳΤϥʔ͕ൃੜ͢Δ // Cannot convert value of type 'Int' to expected argument type 'UserID'

Slide 19

Slide 19 text

ޡͬͨར༻ʹ͙͢ ؾ͚ͮΔͧʂ

Slide 20

Slide 20 text

Ͱ͸ɺαϯϓϧͷ +40/σίʔυ ͯ͠ΈΑ͏ʂ

Slide 21

Slide 21 text

let json = """ { "id" : 1234, "name" : "maguhiro", "age" : 34 } """.data(using: .utf8)! let user = try! JSONDecoder() .decode(User.self, from: json) // Swift.DecodingError.typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "id", intValue: nil)], debugDescription: "Expected to decode Dictionary but found a number instead.", underlyingError: nil))

Slide 22

Slide 22 text

Τϥʔ͕ൃੜ

Slide 23

Slide 23 text

஋ΦϒδΣΫτͱͯ͠6TFS*% ܕΛఆٛͨ͠ࣄͰɺσʔλߏ ଄ͷ֊૚͕ਂ͘ͳͬͯ͠·ͬ ͨͨΊ

Slide 24

Slide 24 text

Ͳ͏͢Ε͹ʁ

Slide 25

Slide 25 text

TJOHMF7BMVF$POUBJOFSΛ࢖͓͏ %FDPEFS &ODPEFSϓϩτίϧͰఆٛ͞ Ε͍ͯΔؔ਺ ΦϑΟγϟϧυΩϡϝϯτʹ͸ҎԼͷΑ ͏ʹهࡌ͞Ε͍ͯΔɻ l୯ҰͷϓϦϛςΟϒ஋Λอ࣋͢Δͷʹద ͨ͠σίʔυɾΤϯίʔυίϯςφz

Slide 26

Slide 26 text

࢖ͬͯΈΑ͏ʂ

Slide 27

Slide 27 text

஋ΦϒδΣΫτΛ ද͢ϓϩτίϧͷ ࡞੒

Slide 28

Slide 28 text

protocol ValueObject: Codable, CustomStringConvertible, Equatable { associatedtype Value: Codable, CustomStringConvertible, Equatable var value: Value { get } init(value: Value) }

Slide 29

Slide 29 text

extension ValueObject { init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() let value = try container.decode(Value.self) self.init(value: value) } func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() try container.encode(value) } var description: String { return value.description } static func == (lhs: Self, rhs: Self) -> Bool { return lhs.value == rhs.value } }

Slide 30

Slide 30 text

6TFS*%ܕ΁ద༻

Slide 31

Slide 31 text

struct UserID: ValueObject { let value: Int init(value: Int) { self.value = value } }

Slide 32

Slide 32 text

let json = """ { "id" : 1234, "name" : "maguhiro", "age" : 34 } """.data(using: .utf8)! let user = try! JSONDecoder() .decode(User.self, from: json) print(“\(user.id)”) // 1234

Slide 33

Slide 33 text

·ͱΊ ‣ ୯Ұͷ஋ΛΤϯίʔυɾσίʔυ͢Δ ৔߹ʹ͸ɺTJOHMF7BMVF$POUBJOFSΛ࢖ ͍·͠ΐ͏ʂ ‣ ஋ΦϒδΣΫτͷΤϯίʔυɾσίʔ υΛ࣮ݱ͠ޡͬͨར༻Λ๷͝͏ʂ

Slide 34

Slide 34 text

͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ IUUQTHJUIVCDPNNBHVIJSP$PEBCMF4VQQPSU'PS7BMVF0CKFDU