Beispiel #1
0
        public void Can_create_with_ModelCreatedEntry_in_journal()
        {
            JournalAppender.Create(0, _commandStore).AppendModelCreated(typeof(ImmutableModel));
            var entries = _commandStore.GetJournalEntries().ToArray();

            Assert.AreEqual(1, entries.Length);
            var firstEntry = entries[0] as JournalEntry <ModelCreated>;

            Assert.NotNull(firstEntry);
            Assert.AreEqual(firstEntry.Item.Type, typeof(ImmutableModel));
            Assert.AreEqual(0, firstEntry.Id);
        }
Beispiel #2
0
        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);
        }