Пример #1
0
        public static async Task ApplyIndexingAsync <TEntity>(IMongoDbConnection connection, CancellationToken cancellationToken = default) where TEntity : class
        {
            if (HasAppliedIndexes.TryGetValue(typeof(TEntity), out var hasApplied) && hasApplied)
            {
                return;
            }

            var indexModel = IndexModelBuilder <TEntity> .BuildModel().ToArray();

            if (indexModel.Length > 0)
            {
                var definition = EntityMapping.GetOrCreateDefinition(typeof(TEntity));
                using (var diagnostics = DiagnosticRunner.Start(connection, indexModel))
                {
                    try
                    {
                        var collection = connection.GetDatabase().GetCollection <TEntity>(definition.CollectionName);
                        await collection.Indexes.CreateManyAsync(indexModel, cancellationToken).ConfigureAwait(false);

                        HasAppliedIndexes.TryAdd(typeof(TEntity), true);
                    }
                    catch (Exception exception)
                    {
                        diagnostics.Error(exception);
                        throw;
                    }
                }
            }
        }
        /// <inheritdoc />
        public Expression CreateDocumentQueryExpression(IEntityType entityType)
        {
            MongoDbEntityTypeAnnotations annotations = Check.NotNull(entityType, nameof(entityType)).MongoDb();

            IEntityType queryEntityType = entityType;

            if (!entityType.IsDocumentRootEntityType())
            {
                entityType = entityType.GetMongoDbCollectionEntityType();
            }

            Expression queryExpression = Expression.Call(
                Expression.Constant(_mongoDbConnection.GetDatabase()),
                GetCollectionMethodInfo.MakeGenericMethod(entityType.ClrType),
                new Expression[]
            {
                Expression.Constant(annotations.CollectionName),
                Expression.Constant(
                    annotations.CollectionSettings,
                    typeof(MongoCollectionSettings))
            });

            queryExpression = Expression.Call(
                null,
                AsQueryableMethodInfo.MakeGenericMethod(entityType.ClrType),
                new []
            {
                queryExpression,
                Expression.Constant(
                    null,
                    typeof(AggregateOptions))
            });

            if (queryEntityType != entityType)
            {
                queryExpression = Expression.Call(
                    queryExpression,
                    OfTypeMethodInfo.MakeGenericMethod(queryEntityType.ClrType));
            }

            return(queryExpression);
        }
Пример #3
0
        public static async Task WriteAsync <TEntity>(IMongoDbConnection connection, IEnumerable <IWriteCommand> commands, WriteModelOptions options, CancellationToken cancellationToken = default) where TEntity : class
        {
            var writeModels = commands.OfType <IWriteCommand <TEntity> >().SelectMany(c => c.GetModel(options)).ToArray();

            if (writeModels.Any())
            {
                var entityDefinition = EntityMapping.GetOrCreateDefinition(typeof(TEntity));
                var collection       = connection.GetDatabase().GetCollection <TEntity>(entityDefinition.CollectionName);
                using (var diagnostics = DiagnosticRunner.Start(connection, writeModels))
                {
                    try
                    {
                        await collection.BulkWriteAsync(writeModels, cancellationToken : cancellationToken);
                    }
                    catch (Exception exception)
                    {
                        diagnostics.Error(exception);
                        throw;
                    }
                }
            }
        }
 public virtual bool EnsureCreated()
 {
     _mongoDbConnection.GetDatabase();
     return(true);
 }