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(); }