This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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")) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # --- !Ups | |
| create table users ( | |
| name text primary key, | |
| age int not null); | |
| # --- !Downs | |
| drop table users; |
OlderNewer