Skip to content

Instantly share code, notes, and snippets.

View erikrozendaal's full-sized avatar

Erik Rozendaal erikrozendaal

  • Zilverline
  • Amsterdam
View GitHub Profile
@erikrozendaal
erikrozendaal / accept.java
Created January 23, 2011 19:11
Naive option
public String negotiateFormat(HttpServletRequest request) {
String format = request.getParameter("format");
if (format == null)
format = getFormatFromUriExtension(request);
if (format == null)
format = getFormatFromAcceptHeader(request);
if (format == null)
format = "html";
return format;
}
@erikrozendaal
erikrozendaal / AggregateRoot1.scala
Created February 1, 2011 18:48
Code excerpts for the immutable domain model blogs
trait AggregateRoot[Event] {
protected def applyEvent: Event => Unit
def uncommittedEvents: Iterable[Event] = _uncommittedEvents
def markCommitted = _uncommittedEvents.clear
def loadFromHistory(history: Iterable[Event]) = history.foreach(applyEvent)
protected def record(event: Event) {
@erikrozendaal
erikrozendaal / InvoiceClass.scala
Created February 5, 2011 12:05
Code for immutable domain blog part 3
case class Invoice (
uncommittedEvents: List[InvoiceEvent],
id: Int,
recipient_? : Boolean = false,
nextItemId: Int = 1,
items: Map[Int, InvoiceItem] = Map.empty,
sent_? : Boolean = false,
paid_? : Boolean = false,
dueDate: Option[LocalDate] = None)
extends AggregateRoot[Invoice, InvoiceEvent] {
@erikrozendaal
erikrozendaal / AggregateFactory.scala
Created February 5, 2011 18:58
Code for immutable domain blog part 4
trait AggregateFactory[AR <: AggregateRoot[AR, Event], Event] extends EventSourced[Event] {
def loadFromHistory[T <: AR](history: Iterable[Event]): T = {
var aggregate = applyEvent(history.head)
for (event <- history.tail)
aggregate = aggregate.applyEvent(event)
aggregate.asInstanceOf[AR].markCommitted.asInstanceOf[T]
}
}
@erikrozendaal
erikrozendaal / BehaviorReaction.scala
Created February 8, 2011 20:56
Code for immutable domain blog part 5
trait Reaction[+T]
case class Accepted[+T](events: List[Any], result: T) extends Reaction[T]
case class Rejected(message: String) extends Reaction[Nothing]
trait Behavior[+A] {
protected def apply(events: List[Any]): Reaction[A]
// [... code omitted ...]
def reaction = apply(Nil)
@erikrozendaal
erikrozendaal / InvoiceSpec.scala
Created February 9, 2011 19:26
Code for immutable domain blog part 5
"draft invoice" should {
val invoice: DraftInvoice = Invoice.loadFromHistory(Seq(InvoiceCreated(1)))
"support adding invoice items" in {
val updated = invoice.addItem("Food", "2.95") flatMap (_.addItem("Water", "1.95")) flatMap (_.removeItem(1))
updated.changes must contain(InvoiceItemAdded(1, InvoiceItem(1, "Food", "2.95"), "2.95"))
updated.changes must contain(InvoiceItemAdded(1, InvoiceItem(2, "Water", "1.95"), "4.90"))
updated.changes must contain(InvoiceItemRemoved(1, InvoiceItem(1, "Food", "2.95"), "1.95"))
}
@erikrozendaal
erikrozendaal / domain.rb
Created December 9, 2011 21:25
Simple Ruby DSL for CQRS+ES aggregates
# CQRS+ES Domain DSL
class Symbol
def snake_case
to_s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z\d])([A-Z])/, '\1_\2').
tr("-", "_").
downcase.to_sym
end
@erikrozendaal
erikrozendaal / treemap-bench.scala
Created December 21, 2011 18:36
Scala TreeSet/TreeMap benchmarks
import collection.immutable.TreeMap
import util.Random
object TreeMapTest {
def time(block: => Unit): Double = {
val start = System.nanoTime()
block
val stop = System.nanoTime()
(stop - start) / 1.0e9
@erikrozendaal
erikrozendaal / hashmap-bench.scala
Created January 7, 2012 22:22
Scala TreeMap benchmarks
import collection.mutable.ArrayBuffer
import collection.immutable.HashMap
object HashMapTest {
val random = new util.Random(1234)
def time(block: => Unit): Double = {
val start = System.nanoTime()
block
@erikrozendaal
erikrozendaal / 1.sql
Created February 18, 2012 14:21
Play! 2.0
# --- !Ups
create table users (
name text primary key,
age int not null);
# --- !Downs
drop table users;