示例#1
0
        public Session(
            ILoggerFactory loggerFactory,
            IEventStore eventStore,
            IEventPublisher eventPublisher,
            IEventSerializer eventSerializer,
            IEventUpdateManager eventUpdateManager            = null,
            IEnumerable <IMetadataProvider> metadataProviders = null)
        {
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }
            if (metadataProviders == null)
            {
                metadataProviders = Enumerable.Empty <IMetadataProvider>();
            }

            _logger = loggerFactory.CreateLogger <Session>();

            _eventStore         = eventStore ?? throw new ArgumentNullException(nameof(eventStore));
            _eventSerializer    = eventSerializer ?? throw new ArgumentNullException(nameof(eventSerializer));
            _eventUpdateManager = eventUpdateManager;
            _metadataProviders  = metadataProviders.Concat(new IMetadataProvider[]
            {
                new StreamTypeMetadataProvider(),
                new EventTypeMetadataProvider(),
                new CorrelationIdMetadataProvider()
            });
            _eventPublisher = eventPublisher ?? throw new ArgumentNullException(nameof(eventPublisher));
        }
示例#2
0
        public static ISession Create(
            ITransaction transaction,
            IEventStore eventStore,
            ISnapshotStore snapshotStore,
            ILoggerFactory loggerFactory                      = null,
            IEventPublisher eventPublisher                    = null,
            IEventRouter eventRouter                          = null,
            IEventUpdateManager eventUpdateManager            = null,
            IEnumerable <IMetadataProvider> metadataProviders = null,
            ISnapshotStrategy snapshotStrategy                = null,
            IEventsMetadataService eventsMetadataService      = null)
        {
            // TODO: refactor ctor's Session sucks :(

            if (eventStore == null)
            {
                throw new ArgumentNullException(nameof(eventStore));
            }
            if (snapshotStore == null)
            {
                throw new ArgumentNullException(nameof(snapshotStore));
            }

            if (loggerFactory == null)
            {
                loggerFactory = new NoopLoggerFactory();
            }

            if (eventRouter == null)
            {
                eventRouter = StubEventRouter.Ok();
            }

            if (eventPublisher == null)
            {
                eventPublisher = new EventPublisher(eventRouter);
            }

            var session = new Session(
                loggerFactory: loggerFactory,
                transaction: transaction,
                eventStore: eventStore,
                snapshotStore: snapshotStore,
                eventPublisher: eventPublisher,
                eventUpdateManager: eventUpdateManager,
                metadataProviders: metadataProviders,
                snapshotStrategy: snapshotStrategy,
                eventsMetadataService: eventsMetadataService);

            return(session);
        }
示例#3
0
        public Session(
            ILoggerFactory loggerFactory,
            ITransaction transaction,
            IEventStore eventStore,
            ISnapshotStore snapshotStore,
            IEventPublisher eventPublisher,
            IEventUpdateManager eventUpdateManager            = null,
            IEnumerable <IMetadataProvider> metadataProviders = null,
            ISnapshotStrategy snapshotStrategy           = null,
            IEventsMetadataService eventsMetadataService = null)
        {
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }
            if (metadataProviders == null)
            {
                metadataProviders = Enumerable.Empty <IMetadataProvider>();
            }

            metadataProviders = metadataProviders.Concat(new IMetadataProvider[]
            {
                new AggregateTypeMetadataProvider(),
                new EventTypeMetadataProvider(),
                new CorrelationIdMetadataProvider()
            });

            if (snapshotStrategy == null)
            {
                snapshotStrategy = new IntervalSnapshotStrategy();
            }

            if (eventsMetadataService == null)
            {
                eventsMetadataService = new EventsMetadataService();
            }

            _logger = loggerFactory.Create(nameof(Session));

            _transaction           = transaction ?? throw new ArgumentNullException(nameof(transaction));
            _eventStore            = eventStore ?? throw new ArgumentNullException(nameof(eventStore));
            _snapshotStore         = snapshotStore ?? throw new ArgumentNullException(nameof(snapshotStore));
            _eventPublisher        = eventPublisher ?? throw new ArgumentNullException(nameof(eventPublisher));
            _snapshotStrategy      = snapshotStrategy;
            _eventUpdateManager    = eventUpdateManager;
            _metadataProviders     = metadataProviders;
            _eventsMetadataService = eventsMetadataService;
        }
示例#4
0
 public static ISession Create(
     ITransaction transaction,
     ICompositeStores stores,
     ILoggerFactory loggerFactory                      = null,
     IEventPublisher eventPublisher                    = null,
     IEventRouter eventRouter                          = null,
     IEventUpdateManager eventUpdateManager            = null,
     IEnumerable <IMetadataProvider> metadataProviders = null,
     ISnapshotStrategy snapshotStrategy                = null,
     IEventsMetadataService eventsMetadataService      = null)
 {
     return(Create(transaction,
                   stores.EventStore,
                   stores.SnapshotStore,
                   loggerFactory,
                   eventPublisher,
                   eventRouter,
                   eventUpdateManager,
                   metadataProviders,
                   snapshotStrategy,
                   eventsMetadataService));
 }
        private async Task <Session> ArrangeSessionAsync <TAggregate>(Guid aggregateId, IEventUpdateManager eventUpdateManager = null, params IDomainEvent[] arrangeEvents)
            where TAggregate : Aggregate, new()
        {
            var metadataProviders = new IMetadataProvider[]
            {
                new StreamTypeMetadataProvider(),
                new EventTypeMetadataProvider(),
                new CorrelationIdMetadataProvider()
            };

            var loggerFactory  = new LoggerFactory();
            var eventPublisher = new EventPublisher(new StubEventRouter());

            var eventStore      = new InMemoryEventStore();
            var eventSerializer = new EventSerializer(new JsonTextSerializer());

            var session = new Session(loggerFactory, eventStore, eventPublisher, eventSerializer, eventUpdateManager);

            var aggregate = (TAggregate)Activator.CreateInstance(typeof(TAggregate), args: aggregateId);

            aggregate.SetVersion(arrangeEvents.Length - 1);

            var serializedEvents = arrangeEvents.Select((evt, index) =>
            {
                index++;

                var metadatas =
                    metadataProviders.SelectMany(md => md.Provide(aggregate, evt, EventSource.Metadata.Empty)).Concat(new[]
                {
                    new KeyValuePair <string, object>(MetadataKeys.EventId, Guid.NewGuid()),
                    new KeyValuePair <string, object>(MetadataKeys.EventVersion, aggregate.Version + index)
                });
                return(eventSerializer.Serialize(evt, new EventSource.Metadata(metadatas)));
            });

            eventStore.BeginTransaction();

            await eventStore.SaveAsync(serializedEvents);

            await eventStore.CommitAsync();

            return(session);
        }
        private async Task <Session> ArrangeSessionAsync <TAggregate>(Guid aggregateId, IEventUpdateManager eventUpdateManager = null, params IDomainEvent[] arrangeEvents)
            where TAggregate : Aggregate, new()
        {
            var metadataProviders = new IMetadataProvider[]
            {
                new AggregateTypeMetadataProvider(),
                new EventTypeMetadataProvider(),
                new CorrelationIdMetadataProvider()
            };

            var loggerFactory  = new NoopLoggerFactory();
            var eventPublisher = new EventPublisher(new StubEventRouter());

            var stores = new InMemoryStores();

            var session = new Session(loggerFactory, stores, stores.EventStore, stores.SnapshotStore, eventPublisher, eventUpdateManager: eventUpdateManager);

            var aggregate = (TAggregate)Activator.CreateInstance(typeof(TAggregate), args: aggregateId);

            var serializedEvents = arrangeEvents.Select((e, index) =>
            {
                index++;

                var eventVersion = aggregate.Version + index;

                var metadata = metadataProviders.SelectMany(md => md.Provide(aggregate, e, MetadataCollection.Empty)).Concat(new[]
                {
                    new KeyValuePair <string, object>(MetadataKeys.EventId, Guid.NewGuid()),
                    new KeyValuePair <string, object>(MetadataKeys.EventVersion, eventVersion)
                });

                return(new UncommittedEvent(aggregate, e, eventVersion)
                {
                    Metadata = new MetadataCollection(metadata)
                });
            });

            stores.BeginTransaction();

            await stores.EventStore.AppendAsync(serializedEvents);

            await stores.CommitAsync();

            return(session);
        }