@michaltomanski
class UserService {
public void createUser(User user);
public User getUser(long id);
}
class UserWriteService {
public void createUser(User user);
}
class UserReadService {
public User getUser(long id);
}
...like ketchup
DeactivateUser(id = 1)
Is the user with id=1 active?
UserDeactivated(id = 1)
UserDeactivated(id = 1) goes to DB
isActive = false
Show to the user his:
Show to the user other people's:
def tailrecM[A, B](f: A => L \/ (A \/ B))(a: A): L \/ B =
f(a) match {
case l @ -\/(_) => l
case \/-(-\/(a0)) => tailrecM(f)(a0)
case \/-(rb @ \/-(_)) => rb
}
def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): Map[A, B1] =
((repr: Map[A, B1]) /: xs.seq) (_ + _ )
lazy val fib: Stream[Int] = 0 #:: 1 #:: fib.zip(fib.tail).map { case (a, b) => a + b }
fib drop 5 take 10 toList
// List(5, 8, 13, 21, 34, 55, 89, 144, 233, 377)
workers.foreach(_ ! PoisonPill)
case class Person(name: String, age: Int)
val somebody = Person("Michal", 25)
somebody match {
case Person(name, 25) => s"$name is 25 years old"
case Person(name, ) => s"$name is not 25 years old"
}
val reversedEven = 1 to 10 filter (_ % 2 == 0) reverse
val first = reversedEven.headOption.getOrElse(0)
class CounterActor extends Actor {
var counter = 0
def receive = {
case IncreaseCounter(amount: Int) => counter = counter + amount
case GimmeCounter => sender ! counter
case _ => println("Unknown message")
}
}
actor ! IncreaseCounter(5) // tell
actor ? GimmeCounter // ask
class PersistedCounter extends PersistentActor {
var counter = 0
override def receiveCommand: Receive = {
case increase: IncreaseCounter =>
persist(increase) { event =>
counter = counter + increase.amount
}
case GimmeCounter => sender ! counter
}
override def receiveRecover: Receive = {
case increase: IncreaseCounter => counter = counter + increase.amount
case RecoveryCompleted => println("Events recovery completed")
}
override def persistenceId: String = "persisted-counter"
}
Persistent Actor
Journal (Cassandra)
Akka persistence query
Postgresql
¯\_(ツ)_/¯
@michaltomanski
slides.com/michaltomanski/cqrs
github.com/michaltomanski/cqrs-demo