/// <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); }
/// <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); }
/// <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); }
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 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 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_lowercase_id_field() { var testModel = new TestModel4 { id = 1000 }; 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); }
/// <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); }
/// <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); }
/// <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); }
/// <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 ReadValue_throws_on_invalid_arguments() { Assert.That(() => IdReader.ReadValue <IdReaderTest>(null), Throws.TypeOf <ArgumentNullException>()); }
public void ReadValue_throws_when_no_id_field_present() { var testModel = new TestMetadata("test"); Assert.That(() => IdReader.ReadValue(testModel), Throws.ArgumentException); }