Created
February 5, 2011 12:05
-
-
Save erikrozendaal/812405 to your computer and use it in GitHub Desktop.
Code for immutable domain blog part 3
This file contains 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] { | |
// [... code omitted ...] | |
} |
This file contains 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
def send: Invoice = { | |
require(!sent_?, "invoice already sent") | |
require(readyToSend_?, "recipient and items must be specified before sending") | |
val now = new LocalDate | |
applyEvent(InvoiceSent(id, sentDate = now, dueDate = now.plusDays(14))) | |
} | |
def applyEvent = { | |
// [... code omitted ...] | |
case event: InvoiceSent => | |
copy(event :: uncommittedEvents, sent_? = true, dueDate = Some(event.dueDate)) | |
// [... code omitted ...] | |
} |
This file contains 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
"ready to send invoice" should { | |
"generate invoice sent event" in { | |
val invoice = Invoice.create(1) | |
.changeRecipient(Some("Erik")) | |
.addItem("Food", 2.95) | |
.send | |
invoice.uncommittedEvents must contain( | |
InvoiceSent(1, | |
sentDate = new LocalDate(2011, 1, 29), | |
dueDate = new LocalDate(2011, 2, 12))) | |
} | |
} |
This file contains 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
def markCommitted = copy(uncommittedEvents = Nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment