public void SaveEvents(IAggregateRoot aggregateRoot)
        {
            var events = new List <Event>();

            var changes = aggregateRoot.GetAllChanges();

            var enumerator = changes.GetEnumerator();
            var count      = 0;

            while (enumerator.MoveNext())
            {
                count++;
                var @event = enumerator.Current;

                events.Add(new Event()
                {
                    AggregateId = @event.AggregateId,
                    EventType   = @event.GetType().AssemblyQualifiedName,
                    EventData   = _serialiser.SerialiseAs(@event.GetType(), @event),
                    Timestamp   = @event.TimeStamp
                });

                _genericEntityService.SaveOrUpdateEntities(events);
            }
        }
Ejemplo n.º 2
0
        public void SaveEvents(IAggregateRoot aggregateRoot)
        {
            var changes = aggregateRoot.GetAllChanges();

            var enumerator = changes.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    var @event = enumerator.Current;

                    allEvents.Add(new Event()
                    {
                        AggregateId = @event.AggregateId,
                        EventType   = @event.GetType().AssemblyQualifiedName,
                        EventData   = _serialiser.SerialiseAs(@event.GetType(), @event),
                        Timestamp   = @event.TimeStamp
                    });
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                logger.ErrorFormat("Failed to save events!");
                throw;
            }
        }
Ejemplo n.º 3
0
        public void SaveEvents(IAggregateRoot aggregateRoot)
        {
            CreateTableIfRequired();

            var changes = aggregateRoot.GetAllChanges();

            var enumerator = changes.GetEnumerator();
            var count      = 0;

            while (enumerator.MoveNext())
            {
                count++;

                var @event = enumerator.Current;

                using (var connection = new SqlConnection(_connectionString))
                {
                    connection.Open();

                    var transaction = connection.BeginTransaction();

                    var data = serialiser.SerialiseAs(@event.GetType(), @event);

                    using (var command = new SqlCommand(SAVE_EVENT))
                    {
                        // eventId - Identity field.
                        command.Parameters.AddWithValue("@AggregateId", @event.AggregateId);
                        command.Parameters.AddWithValue("@EventType", @event.GetType().AssemblyQualifiedName);
                        command.Parameters.AddWithValue("@EventData", data);
                        command.Parameters.AddWithValue("@TimeStamp", @event.TimeStamp);

                        command.Connection = connection;

                        // DumpCmd(command);
                        command.ExecuteNonQuery();
                    }

                    transaction.Commit();
                    connection.Close();
                }
            }

            logger.DebugFormat("Saved {0} events for {1}.", count, aggregateRoot.EntityName);

            aggregateRoot.MarkCommitted();
        }
        public void SaveEvents(IAggregateRoot aggregateRoot)
        {
            var changes = aggregateRoot.GetAllChanges();

            var enumerator = changes.GetEnumerator();
            var count = 0;

            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open(); 
                var transaction = connection.BeginTransaction();

                while (enumerator.MoveNext())
                {
                    count++;
                    var @event = enumerator.Current;

                    var eventToSave = new Event()
                    {
                        AggregateId = @event.AggregateId,
                        EventType = @event.GetType().AssemblyQualifiedName,
                        EventData = _serialiser.SerialiseAs(@event.GetType(), @event),
                        Timestamp = @event.TimeStamp
                    };

                    connection.Execute(SAVE_EVENT, new
                    {
                        AggregateId = eventToSave.AggregateId,
                        EventType = eventToSave.EventType,
                        EventData = @eventToSave.EventData,
                        TimeStamp = eventToSave.Timestamp,
                    });
                }

                transaction.Commit();
                connection.Close();
            }
        }