Пример #1
0
        protected void ApplyEvent(SourcedEvent evnt)
        {
            if (evnt.EventSourceId != SourcedEvent.UndefinedEventSourceId)
            {
                var message = String.Format("The {0} event cannot be applied to event source {1} with id {2} " +
                                            "since it was already owned by event source with id {3}.",
                                            evnt.GetType().FullName, this.GetType().FullName, EventSourceId, evnt.EventSourceId);
                throw new InvalidOperationException(message);
            }

            if (evnt.EventSequence != SourcedEvent.UndefinedEventSequence)
            {
                // TODO: Add better exception message.
                var message = String.Format("The {0} event cannot be applied to event source {1} with id {2} " +
                            "since the event already contains a sequence {3} while {4} was expected.",
                            evnt.GetType().FullName, this.GetType().FullName, EventSourceId, evnt.EventSequence, SourcedEvent.UndefinedEventSequence);
                throw new InvalidOperationException(message);
            }

            evnt.EventSourceId = EventSourceId;
            evnt.EventSequence = Version + 1;

            // First handle event. This to support the set of the
            // Id property for the first event. If we first Append
            // the event to the event stream, the handler cannot set
            // the Id property anymore.
            HandleEvent(evnt);

            _uncommittedEvents.Append(evnt);

            OnEventApplied(evnt);
        }
Пример #2
0
        private void SaveEvent(SourcedEvent evnt, Guid eventSourceId, SQLiteTransaction transaction)
        {
            if (evnt == null || transaction == null) throw new ArgumentNullException();
            using (var dataStream = new MemoryStream())
            {
                var bag = _converter.Convert(evnt);

                var formatter = new BinaryFormatter();
                formatter.Serialize(dataStream, bag);
                var data = dataStream.ToArray();

                using (var cmd = new SQLiteCommand(Query.InsertNewEventQuery, transaction.Connection))
                {
                    cmd.SetTransaction(transaction)
                        .AddParam("Id", eventSourceId)
                        .AddParam("Name", evnt.GetType().FullName)
                        .AddParam("Sequence", evnt.EventSequence)
                        .AddParam("Data", data);
                    cmd.ExecuteNonQuery();
                }
            }
        }
Пример #3
0
        protected internal void ApplyEvent(SourcedEvent evnt)
        {
            if (evnt.EventSourceId != SourcedEvent.UndefinedEventSourceId)
            {
                var message = String.Format("The {0} event cannot be applied to event source {1} with id {2} " +
                                            "since it was already owned by event source with id {3}.",
                                            evnt.GetType().FullName, this.GetType().FullName, EventSourceId, evnt.EventSourceId);
                throw new InvalidOperationException(message);
            }

            if (evnt.EventSequence != SourcedEvent.UndefinedEventSequence)
            {
                // TODO: Add better exception message.
                var message = String.Format("The {0} event cannot be applied to event source {1} with id {2} " +
                            "since the event already contains a sequence {3} while {4} was expected.",
                            evnt.GetType().FullName, this.GetType().FullName, EventSourceId, evnt.EventSequence, SourcedEvent.UndefinedEventSequence);
                throw new InvalidOperationException(message);
            }

            evnt.EventSourceId = EventSourceId;
            evnt.EventSequence = Version + 1;

            HandleEvent(evnt);

            _uncommittedEvents.Append(evnt);

            OnEventApplied(evnt);
        }