public Model LoadModel(Type modelType = null) { Model model = null; //Try to load from the most recent snapshot var snapshotInfo = _snapshotStore.Snapshots.LastOrDefault(); if (snapshotInfo != null) { model = _snapshotStore.LoadSnapshot(snapshotInfo); model.SnapshotRestored(); } //If no snapshot present see if the first journal entry is of type ModelCreated if (model == null) { var firstJournalEntry = _commandStore.GetJournalEntries() .Take(1) .OfType <JournalEntry <ModelCreated> >() .SingleOrDefault(); if (firstJournalEntry != null) { modelType = firstJournalEntry.Item.Type; } if (modelType == null) { throw new InvalidOperationException("No model type present"); } model = (Model)Activator.CreateInstance(modelType); } var ctx = Execution.Begin(); //Replay commands foreach (var commandEntry in _commandStore.CommandEntriesFrom(model.Revision + 1)) { ctx.Now = commandEntry.Created; commandEntry.Item.Redo(ref model); ctx.Events.Clear(); model.Revision++; } model.JournalRestored(); return(model); }
public void RollbackMarkerIsWrittenOnRollback() { //Arrange Execution.Begin(); var config = new EngineConfiguration().ForIsolatedTest(); var store = new InMemoryCommandStore(config); store.Initialize(); var target = new JournalAppender(1, new StreamJournalWriter(config, store.CreateJournalWriterStream)); var command = new ACommand(); target.Append(command); target.Append(command); //Act target.AppendRollbackMarker(); //Assert Assert.AreEqual(3, store.GetJournalEntries().Count()); Assert.AreEqual(1, store.GetJournalEntries().OfType <JournalEntry <RollbackMarker> >().Count()); Assert.IsTrue(store.GetJournalEntries().Last() is JournalEntry <RollbackMarker>); }
public void Timestamp_is_copied_to_journal_entry() { JournalEntry entry = null; var fake = A.Fake <IJournalWriter>(); A.CallTo(() => fake.Write(A <JournalEntry> ._)) .Invokes((JournalEntry je) => { entry = je; }); var before = DateTime.Now; Execution.Begin(); var command = new SetTimeCommand(); var target = new JournalAppender(0, fake); var after = DateTime.Now; target.Append(command); Assert.IsNotNull(entry); Assert.IsTrue(before <= entry.Created && entry.Created <= after); }