Ejemplo n.º 1
0
        public async Task <Boolean> Save(AggregateRootWithEvents aggregateRoot, Int32 eventsPerRequest = 100)
        {
            var    events     = aggregateRoot.GetChanges();
            String streamName = GetStreamId(aggregateRoot.GetType(), aggregateRoot.Id);

            var encoding = new UTF8Encoding();

            IEnumerable <EventData> data = events.Select(x => new EventData(
                                                             Uuid.NewUuid(),
                                                             x.GetType().Name,
                                                             encoding.GetBytes(JsonConvert.SerializeObject(x, _jsonSerializerSettings)),
                                                             System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(new EventMeta
            {
                BodyType = x.GetType().AssemblyQualifiedName
            })));

            Int32 eventPosition = 0;

            while (true)
            {
                var dataToSend = data.Skip(eventPosition).Take(eventsPerRequest).ToArray();
                await _client.AppendToStreamAsync(streamName, StreamState.Any, dataToSend);

                if (dataToSend.Length != eventsPerRequest)
                {
                    break;
                }

                eventPosition += eventsPerRequest;
            }

            return(true);
        }
Ejemplo n.º 2
0
        protected T GetFirstEvent <T>(AggregateRootWithEvents root) where T : DomainEvent
        {
            IEnumerable <DomainEvent> events = root.GetChanges();

            Assert.Single(events);

            Assert.IsAssignableFrom <T>(events.First());

            return((T)events.First());
        }
Ejemplo n.º 3
0
        public override async Task <bool> Save(AggregateRootWithEvents aggregateRoot)
        {
            if (aggregateRoot is DHCPv4RootScope == false)
            {
                return(await base.Save(aggregateRoot));
            }

            PseudoDHCPv4RootScope pseudoRootScope = new();

            var events = aggregateRoot.GetChanges();

            List <AggregateRootWithEvents> aggregatesToSave = new();

            aggregatesToSave.Add(pseudoRootScope);

            HashSet <Guid> scopesToDelete = new();
            HashSet <Guid> leasesToDelete = new();

            foreach (var item in events)
            {
                switch (item)
                {
                case DHCPv4ScopeAddedEvent e:
                {
                    pseudoRootScope.AddScope(e.Instructions.Id);
                    var pseudoScope = new PseudoDHCPv4Scope(e.Instructions.Id);
                    pseudoScope.AddChange(e);
                    aggregatesToSave.Add(pseudoScope);
                }
                break;

                case DHCPv4ScopeDeletedEvent e:
                {
                    pseudoRootScope.RemoveScope(e.EntityId);
                    scopesToDelete.Add(e.EntityId);
                }
                break;

                case EntityBasedDomainEvent e when
                    item is DHCPv4ScopePropertiesUpdatedEvent ||
                    item is DHCPv4ScopeNameUpdatedEvent ||
                    item is DHCPv4ScopeDescriptionUpdatedEvent ||
                    item is DHCPv4ScopeResolverUpdatedEvent ||
                    item is DHCPv4ScopeAddressPropertiesUpdatedEvent ||
                    item is DHCPv4ScopeParentUpdatedEvent ||
                    item is DHCPv4ScopeAddressesAreExhaustedEvent ||
                    item is DHCPv4ScopeSuspendedEvent ||
                    item is DHCPv4ScopeReactivedEvent:
                {
                    var pseudoScope = new PseudoDHCPv4Scope(e.EntityId);
                    pseudoScope.AddChange(e);
                    aggregatesToSave.Add(pseudoScope);
                }
                break;

                case DHCPv4LeaseCreatedEvent e:
                {
                    pseudoRootScope.AddLease(e.EntityId);
                    var pseudoScope = new PseudoDHCPv4Lease(e.EntityId);
                    pseudoScope.AddChange(e);
                    aggregatesToSave.Add(pseudoScope);
                }
                break;

                case DHCPv4LeaseRemovedEvent e:
                {
                    pseudoRootScope.RemoveLease(e.EntityId);
                    leasesToDelete.Add(e.EntityId);
                }
                break;

                case DHCPv4ScopeRelatedEvent e:
                {
                    var pseudoLease = new PseudoDHCPv4Lease(e.EntityId);
                    pseudoLease.AddChange(e);
                    aggregatesToSave.Add(pseudoLease);
                }
                break;

                default:
                    break;
                }
            }

            foreach (var item in aggregatesToSave)
            {
                await EventStore.Save(item, 20);

                if (item is PseudoDHCPv4Lease)
                {
                    var propertyResolver = Provider.GetService <IDHCPv6ServerPropertiesResolver>();

                    await EventStore.ApplyMetaValuesForStream <PseudoDHCPv4Lease>(
                        item.Id,
                        new EventStoreStreamMetaValues(EventStoreStreamMetaValues.DoNotTruncate, propertyResolver.GetLeaseLifeTime()));
                }
            }

            foreach (var item in scopesToDelete)
            {
                await EventStore.DeleteAggregateRoot <PseudoDHCPv4Scope>(item);
            }

            foreach (var item in leasesToDelete)
            {
                await EventStore.DeleteAggregateRoot <PseudoDHCPv4Lease>(item);
            }

            aggregateRoot.ClearChanges();

            Boolean projectResult = await ReadStore.Project(events);

            return(projectResult);
        }