public void JsonSerializerAndDeserializer_Test()
        {
            //Arrange
            var product = new Product
                          (
                1L,
                "banana",
                true,
                new List <long>()
            {
                1L,
                2L,
                3L
            },
                new List <ProductRecord>()
            {
                new ProductRecord(1L, "a", DateTime.UtcNow),
                new ProductRecord(2L, "b", DateTime.UtcNow),
                new ProductRecord(3L, "c", DateTime.UtcNow),
                new ProductRecord(4L, "d", DateTime.UtcNow),
            }
                          );

            //Act
            var json = _serializer.Serialize(product);

            var deserializedProduct = _serializer.Deserialize(json, typeof(Product)) as Product;

            //Assert
            deserializedProduct.Id.ShouldBe(product.Id);
            deserializedProduct.Name.ShouldBe(product.Name);
            deserializedProduct.IsPublished.ShouldBe(product.IsPublished);
        }
        public Task SaveAsync(IAggregateRoot aggregateRoot, Type aggregateRootType, int publishedVersion)
        {
            if (aggregateRoot == null)
            {
                throw new ArgumentNullException(nameof(aggregateRoot));
            }

            if (publishedVersion % _aggregateSnapshotConfiguration.VersionInterval != 0)
            {
                return(Task.CompletedTask);
            }

            var payload = _aggregateSnapshotSerializer.Serialize(aggregateRoot);

            var context = new AggregateSnapshotStoreContext()
            {
                AggregateRootId   = aggregateRoot.UniqueId,
                AggregateRootType = aggregateRootType,
                Payload           = payload,
                PublishedVersion  = publishedVersion
            };

            _queue.OnNext(context);

            return(Task.CompletedTask);
        }
Пример #3
0
        public async Task SaveSnapshotAsync(IAggregateRoot aggregateRoot, Type aggregateRootType, int publishedVersion)
        {
            if (aggregateRoot == null)
            {
                throw new ArgumentNullException(nameof(aggregateRoot));
            }

            if (publishedVersion % _aggregateSnapshotConfiguration.VersionInterval != 0)
            {
                return;
            }

            var copiedAggregateRoot   = DeepCopier.Copy(aggregateRoot);
            var aggregateRootJson     = _aggregateSnapshotSerializer.Serialize(copiedAggregateRoot);
            var aggregateRootTypeName = _typeNameProvider.GetTypeName(aggregateRootType);
            var snapshot = new Snapshot()
            {
                CreationTime          = DateTime.UtcNow,
                ModificationTime      = DateTime.UtcNow,
                AggregateRootId       = copiedAggregateRoot.UniqueId,
                AggregateRootTypeName = aggregateRootTypeName,
                Version           = copiedAggregateRoot.Version,
                SerializedPayload = aggregateRootJson,
            };
            var sql = string.Format(InsertOrUpdateSnapshotSql, _snapshotRepository.GetTableName(snapshot.AggregateRootId));

            using (var connection = _snapshotRepository.GetConnection())
            {
                await connection.ExecuteAsync(sql, snapshot);
            }
        }
Пример #4
0
        public async Task SaveSnapshotAsync(IAggregateRoot aggregateRoot, Type aggregateRootType, int publishedVersion)
        {
            if (aggregateRoot == null)
            {
                throw new ArgumentNullException(nameof(aggregateRoot));
            }

            if (publishedVersion % _aggregateSnapshotConfiguration.VersionInterval != 0)
            {
                return;
            }

            var copiedAggregateRoot   = DeepCopier.Copy(aggregateRoot);
            var aggregateRootJson     = _aggregateSnapshotSerializer.Serialize(copiedAggregateRoot);
            var aggregateRootTypeName = _typeNameProvider.GetTypeName(aggregateRootType);
            var snapshot = new Snapshot()
            {
                Id                    = ObjectId.GenerateNewId(),
                CreationTime          = DateTime.UtcNow,
                ModificationTime      = DateTime.UtcNow,
                AggregateRootId       = copiedAggregateRoot.UniqueId,
                AggregateRootTypeName = aggregateRootTypeName,
                Version               = copiedAggregateRoot.Version,
                Payload               = aggregateRootJson,
            };

            var filter = Builders <Snapshot>
                         .Filter
                         .Eq(s => s.AggregateRootId, snapshot.AggregateRootId);

            var update = Builders <Snapshot>
                         .Update
                         .Set(s => s.ModificationTime, snapshot.ModificationTime)
                         .Set(s => s.Version, snapshot.Version)
                         .Set(s => s.Payload, snapshot.Payload)
                         .SetOnInsert(s => s.Id, snapshot.Id)
                         .SetOnInsert(s => s.CreationTime, snapshot.CreationTime)
                         .SetOnInsert(s => s.AggregateRootId, snapshot.AggregateRootId)
                         .SetOnInsert(s => s.AggregateRootTypeName, snapshot.AggregateRootTypeName);

            await _snapshotCollection
            .GetCollection(copiedAggregateRoot.UniqueId)
            .UpdateOneAsync(filter, update, new UpdateOptions()
            {
                IsUpsert = true
            });
        }