Skip to content

Instantly share code, notes, and snippets.

View j5ik2o's full-sized avatar

Junichi Kato j5ik2o

View GitHub Profile
@j5ik2o
j5ik2o / event_persistence_gatway.rs
Last active June 24, 2023 14:00
RustでのステートレスなEvent Sourcingの実装
// ここでいうステートレスというのはアプリケーションに状態があるのではなく、DBに状態があることを意味しています。
// 逆にAkka Clusterで実装されるEvent Sourcingシステムでは、アプリケーションの状態はアプリケーションが保持しており、
// DBはバックアップログを持つストレージの役割になります。
pub struct EventPersistenceGateway<'a> {
journal_table_name: String,
snapshot_table_name: String,
client: &'a Client,
}
// https://github.com/fpinscala/fpinscala/.../parsing/Parsers.scala の一部
extension [A](p: Parser[A])
// 中略
def opt: Parser[Option[A]] = p.map(Some(_)) | succeed(None)
def map[B](f: A => B): Parser[B] = p.flatMap(f andThen succeed)
def |(p2: => Parser[A]): Parser[A] = p.or(p2)
/*
* Copyright 2022 Junichi Kato
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
import java.util.Currency
final case class Money(amount: BigDecimal, currency: Currency)
object Money {
implicit object MoneyAdder extends Adder[Money] {
override type Output = Either[Exception, Money]

State Sourcing(CRUD)なら以下のようなイメージ。まぁ普通によく見るロジック。

class AddCartItemCommandProcessorOnSS(cartRepository: CartRepository) {

  def execute(cartId: CartId, itemId: ItemId, num: ItemNum): Unit = {
    // 最新の集約(グローバルなエンティティ)をストレージから取得する
    val cart = cartRepository.findById(cartId)
    // ロジック実行: 予算超過ならカートオブジェクトが商品の追加を拒否する! (1)
    val newCart = cart.addItem(itemId, num) 
pub struct ToyVec<T> {
elements: Box<[T]>,
len: usize,
}
pub struct Iter<'vec, T> {
// 参照型のフィールドにはライフタイム指定子
elements: &'vec Box<[T]>,
len: usize,
pos: usize,

[info] Reapplying settings... [info] Set current project to reactive-aws-clients-project (in build file:/home/parallels/Sources/reactive-aws-clients/) WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.twitter.jvm.Hotspot (file:/home/parallels/.cache/coursier/v1/https/repo1.maven.org/maven2/com/twitter/util-jvm_2.12/20.3.0/util-jvm_2.12-20.3.0.jar) to field sun.management.ManagementFactoryHelper.jvm WARNING: Please consider reporting this to the maintainers of com.twitter.jvm.Hotspot WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release 2020-03-27 04:55:45.069-0700 info [finagle] Using bridged scheduler with 4 workers 2020-03-27 04:55:45.074-0700 info [finagle] Finagle version 20.3.0 (rev=e3fd6d1bd88c9ed7edeb98a6598e23800924c633) built at 20200308-110321 2020-03-27 04:55:45.740-0700 info [SonatypeService] sonatypeRepository : https:

「割り勘」ドメインのワークショップ(WIP)

成果物

  • チームで設計に合意すること

副産物

  • ドメインモデル図
  • モデル名を書いた付箋を模造紙に貼る

いまだかつてない自動販売機をかんがえる

ビジネスの概要

売り切りの通常の自動販売機 モデルではなく、サービスとしての自動販売機を考える。サービスの特徴は以下。

  • 利用者はあらかじめ会員登録が必要で、月額100円で専用のスマフォアプリから何度でも飲料を購入できる

アクター/外部システム

@j5ik2o
j5ik2o / DCI.md
Last active October 24, 2019 06:19

Lean Architecture APPENDIX A, ScalaでのDCIサンプルです。コプリエンさん作です。

increaseBalanceUnitを返すとはないわ〜。(今どき可変オブジェクトを中心にドメインを考えたくないという意味で。多分こういう考え方の人は関数型勢に多い。

import java.time.Instant // 元のサンプルはjava.util.Dateだったがさすがにそれは辞めろ的な意味で書き換えた

// Data
trait Account {
  protected var balance: Long = 0