コード例 #1
0
 public EventAppendResult Append(EventCommitRecord record)
 {
     var commitRecord = ConvertTo(record);
     return _connectionFactory.CreateConnection(_connectionString).TryExecute<EventAppendResult>(connection =>
     {
         try
         {
             connection.Insert(commitRecord, _eventTable);
             return EventAppendResult.Success;
         }
         catch (SqlException sqlException)
         {
             if (sqlException.Number == 2601)
             {
                 if (sqlException.Message.Contains(_commitIndexName))
                 {
                     return EventAppendResult.DuplicateCommit;
                 }
                 if (sqlException.Message.Contains(_versionIndexName))
                 {
                     if (commitRecord.Version == 1)
                     {
                         throw new DuplicateAggregateException(commitRecord.AggregateRootTypeCode, commitRecord.AggregateRootId);
                     }
                     throw new ConcurrentException();
                 }
             }
             throw;
         }
     });
 }
コード例 #2
0
        public EventStream ConvertFrom(EventCommitRecord source)
        {
            if (source == null) return null;

            var eventList = new List<IDomainEvent>();

            foreach (var entry in source.Events)
            {
                var eventType = _eventTypeCodeProvider.GetType(entry.EventTypeCode);
                var domainEvent = _binarySerializer.Deserialize(entry.EventData, eventType) as IDomainEvent;
                eventList.Add(domainEvent);
            }

            return new EventStream(source.CommitId, source.AggregateRootId, source.AggregateRootTypeCode, source.Version, source.Timestamp, eventList);
        }
コード例 #3
0
ファイル: InMemoryEventStore.cs プロジェクト: key-value/enode
        public EventAppendResult Append(EventCommitRecord commitRecord)
        {
            var aggregateInfo = _aggregateInfoDict.GetOrAdd(commitRecord.AggregateRootId, new AggregateInfo());
            var originalStatus = Interlocked.CompareExchange(ref aggregateInfo.Status, Editing, UnEditing);

            if (originalStatus == aggregateInfo.Status)
            {
                throw new ConcurrentException();
            }

            try
            {
                if (aggregateInfo.CommitDict.ContainsKey(commitRecord.CommitId))
                {
                    return EventAppendResult.DuplicateCommit;
                }
                if (commitRecord.Version == aggregateInfo.CurrentVersion + 1)
                {
                    aggregateInfo.CommitDict[commitRecord.CommitId] = commitRecord;
                    aggregateInfo.EventDict[commitRecord.Version] = commitRecord;
                    aggregateInfo.CurrentVersion = commitRecord.Version;
                    _eventDict.TryAdd(Interlocked.Increment(ref _sequence), commitRecord);
                    return EventAppendResult.Success;
                }
                else if (commitRecord.Version == 1)
                {
                    throw new DuplicateAggregateException(commitRecord.AggregateRootTypeCode, commitRecord.AggregateRootId);
                }
                else
                {
                    throw new ConcurrentException();
                }
            }
            finally
            {
                Interlocked.Exchange(ref aggregateInfo.Status, UnEditing);
            }
        }
コード例 #4
0
 private SqlEventCommitRecord ConvertTo(EventCommitRecord eventStream)
 {
     return new SqlEventCommitRecord
     {
         CommitId = eventStream.CommitId,
         AggregateRootId = eventStream.AggregateRootId,
         AggregateRootTypeCode = eventStream.AggregateRootTypeCode,
         Version = eventStream.Version,
         Timestamp = eventStream.Timestamp,
         Events = _binarySerializer.Serialize(eventStream.Events)
     };
 }