public void ReadName_reads_user_defined_id_property()
        {
            IdReader.SetIdMember <TestModel7>("MyId");
            var name = IdReader.ReadName <TestModel7>();

            name.Should().Be("MyId");
        }
Beispiel #2
0
        /// <summary>
        /// Creates an add one command against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TMetadata">The type of the metadata.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new add one command.</returns>
        public static AddOneCommandAsync <TModel, TMetadata> AddOneAsync <TModel, TMetadata>(MongoDatabase database)
        {
            var addOneCommandAsync = AddOneAsync <StorageModel <TModel, TMetadata> >(database);

            AddOneCommandAsync <TModel, TMetadata> commandAsync = async(model, metadata, collectionName, cancellationToken) =>
            {
                if (model == null)
                {
                    throw new ArgumentNullException(nameof(model));
                }

                var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

                var storageModel = new StorageModel <TModel, TMetadata>
                {
                    Id       = IdReader.ReadValue(model),
                    Model    = model,
                    Metadata = metadata
                };

                await addOneCommandAsync(storageModel, modelTypeName, cancellationToken);
            };

            return(commandAsync);
        }
Beispiel #3
0
        /// <summary>
        /// Creates an add or update one command against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="readModelDatabase">The database.</param>
        /// <returns>A new add or update one command.</returns>
        public static AddOrUpdateOneCommandAsync <TModel> AddOrUpdateOneAsync <TModel>(MongoDatabase readModelDatabase)
        {
            if (readModelDatabase == null)
            {
                throw new ArgumentNullException(nameof(readModelDatabase));
            }

            AddOrUpdateOneCommandAsync <TModel> commandAsync = async(model, collectionName, cancellationToken) =>
            {
                if (model == null)
                {
                    throw new ArgumentNullException(nameof(model));
                }

                var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

                var updateOptions = new UpdateOptions
                {
                    IsUpsert = true
                };

                var id     = IdReader.ReadValue(model);
                var filter = Builders <TModel> .Filter.Eq("_id", id);

                var client     = readModelDatabase.CreateClient();
                var collection = client.GetCollection <TModel>(modelTypeName);

                await collection.ReplaceOneAsync(filter, model, updateOptions, cancellationToken);
            };

            return(commandAsync);
        }
        public void ReadName_reads_user_defined_id_field()
        {
            IdReader.SetIdMember <TestModel8>("entityId");
            var name = IdReader.ReadName <TestModel8>();

            name.Should().Be("entityId");
        }
        /// <summary>
        /// Creates a merge complete set command against the specified database.
        /// </summary>
        /// <typeparam name="TId">The type of the identifier.</typeparam>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TMetadata">The type of the metadata.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new merge complete set command.</returns>
        public static MergeCompleteSetCommandAsync <TId, TModel, TMetadata> MergeCompleteSetAsync <TId, TModel, TMetadata>(
            MongoDatabase database)
        {
            // remove from set b where not in set a
            var addOrUpdateManyCommandAsync = AddOrUpdateManyAsync <TId, TModel, TMetadata>(database);

            MergeCompleteSetCommandAsync <TId, TModel, TMetadata> commandAsync = async(models, collectionName, cancellationToken) =>
            {
                if (models == null)
                {
                    throw new ArgumentNullException(nameof(models));
                }

                var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

                // Remove all existing models with ids not in the list of ids given
                var equalFilter = Builders <StorageModel <TModel, TMetadata> > .Filter.In(
                    "_id",
                    models.Select(kvp => IdReader.ReadValue(kvp.Value.Model)));

                var notEqualFilter = Builders <StorageModel <TModel, TMetadata> > .Filter.Not(equalFilter);

                var client     = database.CreateClient();
                var collection = client.GetCollection <StorageModel <TModel, TMetadata> >(modelTypeName);
                await collection.DeleteManyAsync(notEqualFilter, cancellationToken);

                // Add or update the rest
                await addOrUpdateManyCommandAsync(models, modelTypeName, cancellationToken);
            };

            return(commandAsync);
        }
        public void ReadValue_reads_Id_property()
        {
            var testModel = new TestModel("test", Guid.NewGuid());

            var id = IdReader.ReadValue(testModel);

            id.Should().Be(testModel.Id);
        }
        public void ReadValue_reads_underscore_id_field()
        {
            var _id       = 1000.0;
            var testModel = new TestModel6(_id);

            var id = IdReader.ReadValue(testModel);

            id.Should().Be(_id);
        }
        public void ReadValue_reads_underscore_id_property()
        {
            var _id       = 1000.0f;
            var testModel = new TestModel5(_id);

            var id = IdReader.ReadValue(testModel);

            id.Should().Be(_id);
        }
Beispiel #9
0
 public void Setup()
 {
     _foundIds = Lists.NewList <IEnumerable <string> >();
     _log      = Mock.Of <IIdReaderLogger>();
     Mock.Get(_log)
     .Setup(l => l.FoundIds(It.IsAny <IEnumerable <string> >()))
     .Callback <IEnumerable <string> >(s => _foundIds.Add(s));
     _sut = new IdReader(_log);
 }
        public void ReadType_reads_Id_property()
        {
            var type = IdReader.ReadType <TestModel>();

            type.Should().Be(typeof(Guid));

            var type2 = IdReader.ReadType(typeof(TestModel));

            type2.Should().Be(typeof(Guid));
        }
        public void ReadValue_reads_user_defined_id_property()
        {
            var _id       = 3u;
            var testModel = new TestModel7(_id);

            IdReader.SetIdMember <TestModel7>("MyId");
            var id = IdReader.ReadValue(testModel);

            id.Should().Be(_id);
        }
        public void ReadType_reads_underscore_id_field()
        {
            var type = IdReader.ReadType <TestModel6>();

            type.Should().Be(typeof(double));

            var type2 = IdReader.ReadType(typeof(TestModel6));

            type2.Should().Be(typeof(double));
        }
        public void ReadType_reads_underscore_id_property()
        {
            var type = IdReader.ReadType <TestModel5>();

            type.Should().Be(typeof(float));

            var type2 = IdReader.ReadType(typeof(TestModel5));

            type2.Should().Be(typeof(float));
        }
        public void ReadType_reads_lowercase_id_field()
        {
            var type = IdReader.ReadType <TestModel4>();

            type.Should().Be(typeof(long));

            var type2 = IdReader.ReadType(typeof(TestModel4));

            type2.Should().Be(typeof(long));
        }
        public void ReadType_reads_lowercase_id_property()
        {
            var type = IdReader.ReadType <TestModel3>();

            type.Should().Be(typeof(string));

            var type2 = IdReader.ReadType(typeof(TestModel3));

            type2.Should().Be(typeof(string));
        }
        public void ReadValue_reads_user_defined_id_field()
        {
            var _id       = 3ul;
            var testModel = new TestModel8(_id);

            IdReader.SetIdMember <TestModel8>("entityId");
            var id = IdReader.ReadValue(testModel);

            id.Should().Be(_id);
        }
        public void ReadType_reads_Id_field()
        {
            var type = IdReader.ReadType <TestModel2>();

            type.Should().Be(typeof(int));

            var type2 = IdReader.ReadType(typeof(TestModel2));

            type2.Should().Be(typeof(int));
        }
        public void ReadValue_reads_lowercase_id_field()
        {
            var testModel = new TestModel4
            {
                id = 1000
            };

            var id = IdReader.ReadValue(testModel);

            id.Should().Be(testModel.id);
        }
        public void ReadType_reads_user_defined_id_property()
        {
            IdReader.SetIdMember <TestModel7>("MyId");
            var type = IdReader.ReadType <TestModel7>();

            type.Should().Be(typeof(uint));

            var type2 = IdReader.ReadType(typeof(TestModel7));

            type2.Should().Be(typeof(uint));
        }
        public void ReadValue_reads_lowercase_id_property()
        {
            var testModel = new TestModel3
            {
                id = "hello"
            };

            var id = IdReader.ReadValue(testModel);

            id.Should().Be(testModel.id);
        }
        public void ReadValue_reads_Id_field()
        {
            var testModel = new TestModel2
            {
                Id = 5
            };

            var id = IdReader.ReadValue(testModel);

            id.Should().Be(testModel.Id);
        }
        public void ReadType_reads_user_defined_id_field()
        {
            IdReader.SetIdMember <TestModel8>("entityId");
            var type = IdReader.ReadType <TestModel8>();

            type.Should().Be(typeof(ulong));

            var type2 = IdReader.ReadType(typeof(TestModel8));

            type2.Should().Be(typeof(ulong));
        }
Beispiel #23
0
        /// <summary>
        /// Creates an update one command against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TMetadata">The type of the metadata.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new update one command.</returns>
        public static UpdateOneCommandAsync <TModel, TMetadata> UpdateOneAsync <TModel, TMetadata>(MongoDatabase database)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            UpdateOneCommandAsync <TModel, TMetadata> commandAsync = async(model, metadata, collectionName, cancellationToken) =>
            {
                if (model == null)
                {
                    throw new ArgumentNullException(nameof(model));
                }

                var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

                var id           = IdReader.ReadValue(model);
                var storageModel = new StorageModel <TModel, TMetadata>
                {
                    Id       = id,
                    Model    = model,
                    Metadata = metadata
                };

                var updateOptions = new UpdateOptions
                {
                    IsUpsert = false
                };

                var filter = Builders <StorageModel <TModel, TMetadata> > .Filter.Eq("model._id", id);

                var client     = database.CreateClient();
                var collection = client.GetCollection <StorageModel <TModel, TMetadata> >(modelTypeName);

                var result = await collection.ReplaceOneAsync(filter, storageModel, updateOptions, cancellationToken);

                if (result.ModifiedCount == 0)
                {
                    throw new DatabaseException(
                              $"Unable to find {nameof(model)} of type {typeof(TModel).Name} with id '{id}' in data store to update.");
                }
            };

            return(commandAsync);
        }
Beispiel #24
0
        /// <summary>
        /// Creates a remove one command against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new remove one command.</returns>
        public static RemoveOneCommandAsync <TModel> RemoveOneAsync <TModel>(MongoDatabase database)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            RemoveOneCommandAsync <TModel> commandAsync = async(model, collectionName, cancellationToken) =>
            {
                var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

                var filter = Builders <TModel> .Filter.Eq("_id", IdReader.ReadValue(model));

                var client     = database.CreateClient();
                var collection = client.GetCollection <TModel>(modelTypeName);

                await collection.DeleteOneAsync(filter, cancellationToken);
            };

            return(commandAsync);
        }
Beispiel #25
0
        /// <summary>
        /// Creates an add many command against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TMetadata">The type of the metadata.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new add many command.</returns>
        public static AddManyCommandAsync <TModel, TMetadata> AddManyAsync <TModel, TMetadata>(MongoDatabase database)
        {
            var addManyCommandAsync = AddManyAsync <StorageModel <TModel, TMetadata> >(database);

            AddManyCommandAsync <TModel, TMetadata> commandAsync =
                async(storageModels, collectionName, cancellationToken) =>
            {
                var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

                var modelsWithIds = storageModels.Select(
                    sm => new StorageModel <TModel, TMetadata>
                {
                    Id       = IdReader.ReadValue(sm.Model),
                    Model    = sm.Model,
                    Metadata = sm.Metadata
                });

                await addManyCommandAsync(modelsWithIds, modelTypeName, cancellationToken);
            };

            return(commandAsync);
        }
Beispiel #26
0
        /// <summary>
        /// Executes a query to replace many storage models (for Update or AddOrUpdate many).
        /// </summary>
        /// <typeparam name="TId">The type of the identifier.</typeparam>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TMetadata">The type of the metadata.</typeparam>
        /// <param name="database">The database.</param>
        /// <param name="models">The set of storage models to write.</param>
        /// <param name="collectionName">Name of the collection.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="isUpsert">
        /// If set to <c>true</c> models will be defined as upserts (AddOrUpdate); otherwise they
        /// will be defined as updates.
        /// </param>
        /// <returns>The bulk write task results.</returns>
        /// <exception cref="System.ArgumentNullException">If models is null.</exception>
        private static async Task <BulkWriteResult <BsonDocument> > ReplaceManyAsync <TId, TModel, TMetadata>(
            MongoDatabase database,
            IDictionary <TId, StorageModel <TModel, TMetadata> > models,
            string collectionName,
            CancellationToken cancellationToken,
            bool isUpsert)
        {
            if (models == null)
            {
                throw new ArgumentNullException(nameof(models));
            }

            var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

            var client     = database.CreateClient();
            var collection = client.GetCollection <BsonDocument>(modelTypeName);

            var updateModels = new List <WriteModel <BsonDocument> >();

            foreach (var keyValue in models)
            {
                var id = IdReader.ReadValue(keyValue.Value.Model);
                keyValue.Value.Id = id;
                var filter  = new BsonDocument("_id", BsonValue.Create(id));
                var bsonDoc = keyValue.Value.ToBsonDocument();

                var replaceOne = new ReplaceOneModel <BsonDocument>(filter, bsonDoc)
                {
                    IsUpsert = isUpsert
                };
                updateModels.Add(replaceOne);
            }

            var results = await collection.BulkWriteAsync(updateModels, null, cancellationToken);

            return(results);
        }
        public void ReadName_reads_Id_field()
        {
            var name = IdReader.ReadName <TestModel2>();

            name.Should().Be("Id");
        }
        public void ReadName_reads_Id_property()
        {
            var name = IdReader.ReadName <TestModel>();

            name.Should().Be("Id");
        }
 public void SetIdMember_throws_on_invalid_arguments()
 {
     Assert.That(() => IdReader.SetIdMember <IdReaderTest>(" "), Throws.TypeOf <ArgumentNullException>());
 }
 public void ReadType_throws_on_invalid_arguments()
 {
     Assert.That(() => IdReader.ReadType(null), Throws.TypeOf <ArgumentNullException>());
 }