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

20250624 関数型DDD 13

Avatar for saka saka
December 24, 2025
1

20250624 関数型DDD 13

Avatar for saka

saka

December 24, 2025

Transcript

  1. DDD

  2. Result let workflowWithSerialization jsonString = jsonString |> deserializeInputDto // JSON͔ΒDTO΁

    |> inputDtoToDomain // DTO͔ΒυϝΠϯΦϒδΣΫτ΁ |> workflow // υϝΠϯͷίΞϫʔΫϑϩʔ |> outputDtoFromDomain // υϝΠϯΦϒδΣΫτ͔ΒDTO΁ |> serializeOutputDto // DTO͔ΒJSON΁ // ࠷ऴग़ྗ͸ผͷJsonString
  3. /// ରԠ͢ΔDTOܕ type SuitDto = Heart = 1 | Spade

    = 2 | Diamond = 3 | Club = 4 type RankDto = Ace = 1 | Two = 2 | Queen = 12 | King = 13 type CardDto = { Suit : SuitDto Rank : RankDto_attachment }
  4. type Example = | A | B of int |

    C of string list | D of Name type ExampleDto = { Tag : string // "A", "B", "C", "D" ͷ͍ͣΕ͔1ͭ // έʔεA͸σʔλແ͠ BData : Nullable<int> // έʔεBͷσʔλ CData : string[] // έʔεCͷσʔλ DData : NameDto // έʔεDͷσʔλ }
  5. // υϝΠϯϩδοΫͱI/O͕ࠞࡏ͢ΔϫʔΫϑϩʔ let payInvoice invoiceId payment = // DB͔ΒಡΈࠐΉ let

    invoice = loadInvoiceFromDatabase(invoiceId) // ࢧ෷͍Λద༻͢Δ invoice.ApplyPayment(payment) // ҟͳΔ݁ՌΛॲཧ͢Δ if invoice.IsFullyPaid then markAsFullyPaidInDb(invoiceId) postInvoicePaidEvent(invoiceId) else markAsPartiallyPaidInDb(invoiceId)
  6. // ڥք͚ͮΒΕͨίϯςΩετͷ୺ʹ͋ΔίϚϯυϋϯυϥʔ let payInvoice payInvoiceCommand = // DB͔ΒಡΈࠐΉ let invoiceId

    = payInvoiceCommand.InvoiceId let unpaidInvoice = loadInvoiceFromDatabase invoiceId // I/O // ७ਮͳυϝΠϯͰͷݺͼग़͠ let payment = payInvoiceCommand.Payment // ७ਮ let paymentResult = applyPayment unpaidInvoice payment // ७ਮ // ݁ՌΛॲཧ match paymentResult with | FullyPaid -> markAsFullyPaidInDb invoiceId // I/O postInvoicePaidEvent invoiceId // I/O | PartiallyPaid updatedInvoice -> updateInvoiceInDb updatedInvoice // I/O