Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my command event string field being retrieved as null
    text
    copied!<p>I am writing my first EventStore test app, I am re-hydrating my object from a stream, and whilst it gets the numberSold correctly, the title is null, and I don't understand why - the command when retrieved from the stream has the title set as null but I am sure it is being written OK.</p> <p>Can a fresh pair of eyes see what I am doing wrong?</p> <pre><code>private static void Main() { using (store = WireupEventStore()) { var newBook = new Book("my book", 0); newBook.ChangeBookName("renamed book"); newBook.AdjustBooksSold(5); var idToRetrieveLater = newBook.bookId; var bookRepo = new BookRepository(store); bookRepo.Put(newBook); var bookReadBack = bookRepo.Get(idToRetrieveLater); // book name is set to null here, but count==5 is OK } </code></pre> <p>Book class</p> <pre><code>public class Book { public readonly Guid bookId; private int numberSold; private string title { get; set; } private List&lt;object&gt; eventsToCommit = new List&lt;object&gt;(); public Book(string title, int sold) { this.bookId = Guid.NewGuid(); this.title = title; this.numberSold = sold; } public Book(Guid id, IEnumerable&lt;EventMessage&gt; events) { this.bookId = id; foreach (var ev in events) { dynamic @eventToCall = ev.Body; Apply(@eventToCall); } } public void ChangeBookName(string name) { var nameChanged = new BookNameChanged(this.bookId, name); this.RaiseEvent(nameChanged); } public void AdjustBooksSold(int count) { var booksSold = new BookSold(this.bookId, count); this.RaiseEvent(booksSold); } private void Apply(BookNameChanged nameChanged) { this.title = nameChanged.title; } private void Apply(BookSold bookSold) { this.numberSold += bookSold.count; } protected void RaiseEvent(object eventToRaise) { dynamic @ev = eventToRaise; Apply(@ev); this.eventsToCommit.Add(eventToRaise); } public ICollection&lt;object&gt; GetEvents() { return this.eventsToCommit; } public void ResetEvents() { this.eventsToCommit = new List&lt;object&gt;(); } } </code></pre> <p>Book repository</p> <pre><code>public class BookRepository { IStoreEvents store; public BookRepository(IStoreEvents store) { this.store = store; } public void Put(Book book) { using (var stream = this.store.OpenStream(book.bookId, 0, int.MaxValue)) { foreach (object o in book.GetEvents()) { stream.Add(new EventMessage { Body = @o }); } stream.CommitChanges(Guid.NewGuid()); book.ResetEvents(); } } public Book Get(Guid id) { using (var commits = this.store.OpenStream(id, 0, int.MaxValue)) { var eventsToReply = commits.CommittedEvents; return new Book(id, eventsToReply); } } } </code></pre> <p>Commands</p> <pre><code>public class BookNameChanged { public readonly Guid id; public readonly string title; public BookNameChanged(Guid id, string bookName) { this.id = id; this.title = bookName; } } public class BookSold { public readonly Guid id; public readonly int count; public BookSold(Guid id, int count) { this.id = id; this.count = count; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload