예제 #1
0
        public void SaveEvents(AggregateKey key, int expectedRevision, IEnumerable <DomainEvent> events)
        {
            var commitHeaders = new Dictionary <string, object>
            {
                { CommitIdHeader, Guid.NewGuid() },
                { AggregateClrTypeHeader, key.AggregateType.AssemblyQualifiedName }
            };

            // updateHeaders(commitHeaders);

            var streamName = _aggregateIdToStreamName(key.AggregateType, key.AggregateId);
            var newEvents  = events.Cast <object>().ToList();


            var eventNumber = expectedRevision;

            var preparedEvents = PrepareEvents(newEvents, commitHeaders).ToList();

            using (ISession session = this.factory.OpenSession())
                using (ITransaction tx = session.BeginTransaction())
                {
                    foreach (var uncommittedEvent in preparedEvents)
                    {
                        var entry = new RecordedEvent()
                        {
                            Created       = DateTime.UtcNow,
                            CreatedEpoch  = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds,
                            Data          = uncommittedEvent.Data,
                            EventId       = uncommittedEvent.EventId,
                            EventStreamId = streamName,
                            IsJson        = uncommittedEvent.IsJson,
                            EventType     = uncommittedEvent.Type,
                            Metadata      = uncommittedEvent.Metadata,
                            EventNumber   = eventNumber
                        };
                        session.Save(entry);
                        eventNumber += 1;
                    }

                    tx.Commit();
                }

            foreach (var @event in events)
            {
                _publisher.Publish(@event);
            }
        }
예제 #2
0
        public void SaveEvents(AggregateKey key, int expectedRevision, IEnumerable<DomainEvent> events)
        {
            var commitHeaders = new Dictionary<string, object>
            {
                {CommitIdHeader, Guid.NewGuid()},
                {AggregateClrTypeHeader, key.AggregateType.AssemblyQualifiedName}
            };

            // updateHeaders(commitHeaders);

            var streamName = _aggregateIdToStreamName(key.AggregateType, key.AggregateId);
            var newEvents = events.Cast<object>().ToList();

            var eventNumber = expectedRevision;

            var preparedEvents = PrepareEvents(newEvents, commitHeaders).ToList();

            using (ISession session = this.factory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                foreach (var uncommittedEvent in preparedEvents)
                {
                    var entry = new RecordedEvent()
                    {
                        Created = DateTime.UtcNow,
                        CreatedEpoch = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds,
                        Data = uncommittedEvent.Data,
                        EventId = uncommittedEvent.EventId,
                        EventStreamId = streamName,
                        IsJson = uncommittedEvent.IsJson,
                        EventType = uncommittedEvent.Type,
                        Metadata = uncommittedEvent.Metadata,
                        EventNumber = eventNumber
                    };
                    session.Save(entry);
                    eventNumber += 1;
                }

                tx.Commit();
            }

            foreach (var @event in events)
            {
                _publisher.Publish(@event);
            }
        }