public void Save(Type aggregatedRootType, Guid aggregateId, UncommittedEventStream eventsToSave) { var eventEntities = eventsToSave.Select(e => Convert(aggregatedRootType, aggregateId, e)); foreach (var @event in eventEntities) { _context.Insert(@event); } _context.Commit(); }
public CommittedEventStream Commit(UncommittedEventStream uncommittedEventStream) { var events = uncommittedEventStream.Select(e => e).ToArray(); var parameters = new List<OracleParameter>(); var insertStatementBuilder = new StringBuilder(); insertStatementBuilder.Append("BEGIN "); for (var position = 0; position < events.Length; position++) { insertStatementBuilder.Append(GetParameterizedInsertStatement(position)); parameters.AddRange(_eventParameters.BuildFromEvent(position, events[position])); } insertStatementBuilder.Append(" END;"); Tuple<int, long>[] returnedIds; try { OpenConnection(); var transaction = _connection.BeginTransaction(); try { using (var command = _connection.CreateCommand()) { command.Transaction = transaction; command.CommandText = insertStatementBuilder.ToString(); command.Parameters.AddRange(parameters.ToArray()); command.BindByName = true; command.ExecuteNonQuery(); returnedIds = ExtractReturnedIds(parameters); transaction.Commit(); } } catch (Exception) { transaction.Rollback(); throw; } } finally { EnsureConnectionClosed(_connection); } PopulateEventId(returnedIds, events); var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId); if (events.Any()) committedEventStream.Append(events); return committedEventStream; }