Esempio n. 1
0
        /// <summary>
        /// Creates an add or 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="readModelDatabase">The database.</param>
        /// <returns>A new add or update one command.</returns>
        public static AddOrUpdateOneCommandAsync <TModel, TMetadata> AddOrUpdateOneAsync <TModel, TMetadata>(MongoDatabase readModelDatabase)
        {
            if (readModelDatabase == null)
            {
                throw new ArgumentNullException(nameof(readModelDatabase));
            }

            AddOrUpdateOneCommandAsync <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 = true
                };

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

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

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

            return(commandAsync);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a get many query against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new get many query.</returns>
        public static GetManyQueryUsingFilterDefinitionAsync <TModel> GetManyUsingFilterDefinitionAsync <TModel>(MongoDatabase database)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            GetManyQueryUsingFilterDefinitionAsync <TModel> queryAsync = async(filterDefinition, collectionName, cancellationToken) =>
            {
                if (filterDefinition == null)
                {
                    throw new ArgumentNullException((nameof(filterDefinition)));
                }

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

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

                return(await collection.Find(filterDefinition).ToListAsync(cancellationToken));
            };

            return(queryAsync);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a get all query 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 get all query.</returns>
        public static GetAllQueryAsync <TModel, TMetadata> GetAllAsync <TModel, TMetadata>(MongoDatabase database)
        {
            var getAllQueryAsync = GetAllAsync <StorageModel <TModel, TMetadata> >(database);

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

                var results = await getAllQueryAsync(modelTypeName, cancellationToken);

                return(results.Select(sm => sm.Model));
            };

            return(queryAsync);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a remove 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 remove many command.</returns>
        public static RemoveManyCommandAsync <TModel, TMetadata> RemoveManyAsync <TModel, TMetadata>(MongoDatabase database)
        {
            var removeManyCommandAsync = RemoveManyAsync <StorageModel <TModel, TMetadata> >(database);

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

                await removeManyCommandAsync(where, modelTypeName, cancellationToken);
            };

            return(commandAsync);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a remove many 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 many command.</returns>
        public static RemoveManyCommandUsingFilterDefinitionAsync <TModel> RemoveManyUsingFilterDefinitionAsync <TModel>(MongoDatabase database)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

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

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

                await collection.DeleteManyAsync(filterDefinition, cancellationToken);
            };

            return(commandAsync);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a project many query against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TProjection">The type of the projection.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new project many query.</returns>
        public static ProjectManyQueryUsingFilterDefinitionAsync <TModel, TProjection> ProjectManyUsingFilterDefinitionAsync <TModel, TProjection>(MongoDatabase database)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            ProjectManyQueryUsingFilterDefinitionAsync <TModel, TProjection> queryAsync = async(filterDefinition, project, collectionName, cancellationToken) =>
            {
                if (filterDefinition == null)
                {
                    throw new ArgumentNullException((nameof(filterDefinition)));
                }

                if (project == null)
                {
                    throw new ArgumentNullException((nameof(project)));
                }

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

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

                var projectionDefinition = Builders <TModel> .Projection.Expression(project);

                var findOptions = new FindOptions <TModel, TProjection>()
                {
                    Projection = projectionDefinition
                };

                var findResults = await collection.FindAsync(filterDefinition, findOptions, cancellationToken);

                return(await findResults.ToListAsync(cancellationToken));
            };

            return(queryAsync);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
        /// <summary>
        /// Creates an add or update many command against the specified database.
        /// </summary>
        /// <typeparam name="TId">The type of the identifier.</typeparam>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new add or update many command.</returns>
        public static AddOrUpdateManyCommandAsync <TId, TModel> AddOrUpdateManyAsync <TId, TModel>(MongoDatabase database)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            AddOrUpdateManyCommandAsync <TId, TModel> commandAsync =
                async(models, collectionName, cancellationToken) =>
            {
                await ReplaceManyAsync(database, models, collectionName, cancellationToken, isUpsert : true);
            };

            return(commandAsync);
        }