Esempio n. 1
0
        /// <summary>
        /// Generates next ID from sequence
        /// </summary>
        /// <param name="container">Container which stores identifiers</param>
        /// <param name="document">Document which stores identifier</param>
        /// <returns>Generated identifier</returns>
        public object GenerateId(object container, object document)
        {
            Type         containerType     = container.GetType();
            PropertyInfo databaseProperty  = containerType.GetProperty("Database");
            PropertyInfo namespaceProperty = containerType.GetProperty("CollectionNamespace");

            if (databaseProperty == null)
            {
                throw new InvalidOperationException("Unable to locate \"Database\" property.");
            }

            if (namespaceProperty == null)
            {
                throw new InvalidOperationException("Unable to locate \"CollectionNamespace\" property.");
            }

            IMongoDatabase      database            = databaseProperty.GetValue(container) as IMongoDatabase;
            CollectionNamespace collectionNamespace = namespaceProperty.GetValue(container) as CollectionNamespace;

            if (database == null)
            {
                throw new InvalidOperationException("Database reference is null.");
            }

            if (collectionNamespace == null)
            {
                throw new InvalidOperationException("CollectionNamespace reference is null.");
            }

            var idSequenceCollection = database.GetCollection <IdentifierDto>(_prefix + "_identifiers");

            IdentifierDto result = idSequenceCollection.FindOneAndUpdate(
                Builders <IdentifierDto> .Filter.Eq(_ => _.Id, collectionNamespace.CollectionName),
                Builders <IdentifierDto> .Update.Inc(_ => _.Seq, 1),
                new FindOneAndUpdateOptions <IdentifierDto>()
            {
                IsUpsert       = true,
                ReturnDocument = ReturnDocument.After
            });

            return(FormatNumber(result.Seq));
        }
        public void JsonParser_Deserialize_Succeeds()
        {
            string json = GetTestJson();

            var dto = Json.Deserialize <Dto>(json);

            Assert.IsNotNull(dto, $"{nameof(dto)} is null");

            Assert.IsNotNull(dto.Value, $"{nameof(dto.Value)} is null");
            Assert.AreEqual("TestValue", dto.Value);

            Assert.IsNotNull(dto.Identifiers, $"{nameof(dto.Identifiers)} is null");
            Assert.AreEqual(2, dto.Identifiers?.Count ?? 0);

            IdentifierDto identifier = dto.Identifiers.First();

            Assert.IsNotNull(identifier, $"{nameof(identifier)} is null");
            Assert.AreEqual(3, identifier.Id);

            identifier = dto.Identifiers.Last();
            Assert.IsNotNull(identifier, $"{nameof(identifier)} is null");
            Assert.AreEqual(5, identifier.Id);
        }