Exemple #1
0
        public async Task SC01_CreateStreamAsync()
        {
            IEventStore eventStore =
                new CosmosEventStore(this, EndpointUrl, AuthorizationKey, DatabaseId, _eventsContainerId);

            var meterRegistered = new MeterRegistered
            {
                MeterId        = "87000001",
                PostalCode     = "1000 AA",
                HouseNumber    = "25",
                ActivationCode = "542-484"
            };

            var meterActivated = new MeterActivated();

            // 1. Add a new stream.
            var streamId = $"meter:{meterRegistered.MeterId}";

            var succes = await eventStore.AppendToStreamAsync(
                streamId,
                0,
                new IEvent[] { meterRegistered, meterActivated });

            Assert.IsTrue(succes, "Unexpected stream version encountered.");
        }
        private async Task AppendReadingsCollectedEvents(string streamId, DateTime fromDate, int dayCount)
        {
            IEventStore eventStore = new CosmosEventStore(this, EndpointUrl, AuthorizationKey, DatabaseId);

            var stream = await eventStore.LoadStreamAsync(streamId);

            // Only append more ReadingsCollected events if we haven't done this before.
            // This can be detected by looking at the stream version.
            var events = Enumerable.Range(0, dayCount - 1)
                         .Select(i => new MeterReadingsCollected
            {
                Date     = fromDate.AddDays(i),
                Readings = GenerateMeterReadings(fromDate.AddDays(i)).ToArray()
            })
                         .ToList();

            await eventStore.AppendToStreamAsync(streamId, stream.Version, events);

            // Wait a little while before adding the last one.
            // This ensures that the last event will be at a different timestamp
            // which is better for explaining the mechanism.
            await Task.Delay(TimeSpan.FromSeconds(2));

            await eventStore.AppendToStreamAsync(streamId, stream.Version + events.Count, new IEvent[]
            {
                new MeterReadingsCollected
                {
                    Date     = fromDate.AddDays(dayCount - 1),
                    Readings = GenerateMeterReadings(fromDate.AddDays(dayCount - 1)).ToArray()
                }
            });
        }
        private static async Task WriteVolumeCosmos(IConfigurationRoot config)
        {
            string connectionstring = config.GetConnectionString("CosmosConnectionString");

            var eventStore = CosmosEventStore.GetInstance(connectionstring, "corvuseventstore", "corvusevents", NullSnapshotReader.Instance);

            await ExecuteVolume(eventStore).ConfigureAwait(false);
        }
        private CosmosEventStore GetValidationFailureEventStore()
        {
            var eventStore =
                new CosmosEventStore(_eventTypeResolver, _endpointUrl, _authorizationKey, _databaseId,
                                     _validationFailureContainerId);

            return(eventStore);
        }
        private CosmosEventStore GetMigratedEventStore()
        {
            var eventStore =
                new CosmosEventStore(_eventTypeResolver, _endpointUrl, _authorizationKey, _databaseId,
                                     _migratedEventContainerId);

            return(eventStore);
        }
#pragma warning disable IDE0051 // Remove unused private members
#pragma warning disable RCS1213 // Remove unused member declaration.
        private static async Task SimpleCosmos(IConfigurationRoot config)
        {
            Console.WriteLine("Running with Cosmos in Azure");
            string connectionstring = config.GetConnectionString("CosmosConnectionString");

            var eventStore = CosmosEventStore.GetInstance(connectionstring, "corvuseventstore", "corvusevents", NullSnapshotReader.Instance);

            await ExecuteSimple(eventStore).ConfigureAwait(false);

            Console.WriteLine("Finished running with Cosmos in Azure");
        }
        public async Task SC06A_SaveSnapshotAsync()
        {
            IEventStore    eventStore    = new CosmosEventStore(this, EndpointUrl, AuthorizationKey, DatabaseId);
            ISnapshotStore snapshotStore = new CosmosSnapshotStore(EndpointUrl, AuthorizationKey, DatabaseId);

            // Request parameters.
            var meterId = "87000001";

            // Load domain object.
            var repository = new MeterRepository(eventStore);
            var meter      = await repository.LoadMeterAsync(meterId);

            var snapshot = meter.GetSnapshot();

            await snapshotStore.SaveSnapshotAsync($"meter:{meterId}", meter.Version, snapshot);
        }
        public async Task SC06C_LoadSnapshotAsync()
        {
            IEventStore    eventStore    = new CosmosEventStore(this, EndpointUrl, AuthorizationKey, DatabaseId);
            ISnapshotStore snapshotStore = new CosmosSnapshotStore(EndpointUrl, AuthorizationKey, DatabaseId);

            var repository = new MeterRepositorySnapshotDecorator(
                eventStore,
                snapshotStore,
                new MeterRepository(eventStore));

            // Request parameters.
            var meterId = "87000001";

            // Load domain object.
            var meter = await repository.LoadMeterAsync(meterId);

            Assert.AreEqual(20, meter.Version);
        }
        public async Task SC03_DomainAddAsync()
        {
            IEventStore eventStore = new CosmosEventStore(this, EndpointUrl, AuthorizationKey, DatabaseId);

            // Request parameters.
            var meterId        = "87000002";
            var postalCode     = "9999 BB";
            var houseNumber    = "4";
            var activationCode = "745-195";

            // New domain object.
            var meter = new Meter(meterId, postalCode, houseNumber, activationCode);

            var repository = new MeterRepository(eventStore);
            var succes     = await repository.SaveMeterAsync(meter);

            Assert.IsTrue(succes, "Unexpected stream version encountered.");
        }
Exemple #10
0
        public override void Build()
        {
            var builder = this as ISettingsBuilder;
            ICosmosEventStoreSettings settings = this as ICosmosEventStoreSettings;
            Uri queryUri = UriFactory.CreateDocumentCollectionUri(settings.DatabaseName, settings.CollectionName);

            if (settings.WithNewStorageIfNotExists)
            {
                settings.DocumentClient.CreateDatabaseIfNotExistsAsync(new Database {
                    Id = settings.DatabaseName
                }).Wait();
                CreateAggregateCollection(settings.DocumentClient, settings.DatabaseName, settings.CollectionName, settings.Throughput);
            }

            var eventStore = new CosmosEventStore(settings.DocumentClient, queryUri, builder.Container.Resolve <ISerializer>());

            builder.Container.RegisterSingleton <IEventStore>(() => eventStore, builder.Name);
        }
        public async Task SC04_DomainUpdateAsync()
        {
            IEventStore eventStore = new CosmosEventStore(this, EndpointUrl, AuthorizationKey, DatabaseId);

            // Request parameters.
            var meterId        = "87000002";
            var activationCode = "745-195";

            // Load domain object.
            var repository = new MeterRepository(eventStore);
            var meter      = await repository.LoadMeterAsync(meterId);

            // Call business logic on domain object.
            meter.Activate(activationCode);

            var succes = await repository.SaveMeterAsync(meter);

            Assert.IsTrue(succes, "Unexpected stream version encountered.");
        }
        public async Task SC02_AppendToExistingStreamAsync()
        {
            IEventStore eventStore = new CosmosEventStore(this, EndpointUrl, AuthorizationKey, DatabaseId);

            var streamId = $"meter:87000001";
            var stream   = await eventStore.LoadStreamAsync(streamId);

            var readingsCollected = new MeterReadingsCollected
            {
                Date     = new DateTime(2020, 4, 30),
                Readings = GenerateMeterReadings(new DateTime(2020, 4, 30)).ToArray()
            };

            var succes = await eventStore.AppendToStreamAsync(
                streamId,
                stream.Version,
                new IEvent[] { readingsCollected });

            Assert.IsTrue(succes, "Unexpected stream version encountered.");
        }