/// <summary>
        /// Configures the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Configure(MongoEntityBuilder <BreakfastItem> context)
        {
            context.Indexes.Add("name_unique",
                                Builders <BreakfastItem> .IndexKeys.Ascending(x => x.Name),
                                o => o.Unique().WithCaseInsensitiveCollation());

            context.Seed.Add(Seed);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the MongoDB collection for the specified type.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task <IMongoCollection <TEntity> > GetCollectionAsync <TEntity>(CancellationToken cancellationToken = default)
            where TEntity : MongoEntity
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(MongoContext));
            }

            var collectionName = _options.Value.GetCollectionName <TEntity>();
            var collection     = _database.GetCollection <TEntity>(collectionName);

            if (_bootstrappedCollections.Contains(typeof(TEntity)))
            {
                return(collection);
            }

            try
            {
                await _semaphore.WaitAsync(cancellationToken);

                if (_bootstrappedCollections.Contains(typeof(TEntity)))
                {
                    return(collection);
                }

                var configurations = _serviceProvider.GetServices <IMongoEntityConfiguration <TEntity> >();

                var builder = new MongoEntityBuilder <TEntity>();
                foreach (var configuration in configurations)
                {
                    configuration.Configure(builder);
                }

                // Indexes.
                var indexTasks = builder.Indexes.Select(index =>
                                                        collection.Indexes.CreateOneAsync(index, null, cancellationToken));
                await Task.WhenAll(indexTasks);

                // Seeds.
                var seedTasks = builder.Seed.Select(async seed =>
                {
                    var cursor = await collection.FindAsync(
                        Builders <TEntity> .Filter.IdEq(seed.Id),
                        cancellationToken: cancellationToken);

                    if (await cursor.AnyAsync(cancellationToken))
                    {
                        return;
                    }

                    await collection.InsertOneAsync(seed, cancellationToken: cancellationToken);
                });
                await Task.WhenAll(seedTasks);

                _bootstrappedCollections.Add(typeof(TEntity));

                return(collection);
            }
            finally
            {
                _semaphore.Release();
            }
        }