Browse Source

Allows configuration of MongoDB entity indexes

pull/19073/head
liangshiwei 3 years ago
parent
commit
c749766dc6
  1. 8
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs
  2. 10
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasMongoIndexManagerAction.cs
  3. 10
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs
  4. 19
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs
  5. 25
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs

8
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs

@ -0,0 +1,8 @@
using MongoDB.Driver;
namespace Volo.Abp.MongoDB;
public interface IHasCreateCollectionOptions
{
CreateCollectionOptions GetCreateCollectionOptions();
}

10
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasMongoIndexManagerAction.cs

@ -0,0 +1,10 @@
using System;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Volo.Abp.MongoDB;
public interface IHasMongoIndexManagerAction
{
Action<IMongoIndexManager<BsonDocument>>? IndexesAction { get; set; }
}

10
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs

@ -1,5 +1,7 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace Volo.Abp.MongoDB;
@ -10,6 +12,10 @@ public interface IMongoEntityModelBuilder<TEntity>
string CollectionName { get; set; }
BsonClassMap<TEntity> BsonMap { get; }
CreateCollectionOptions<TEntity> CreateCollectionOptions { get; }
Action<IMongoIndexManager<BsonDocument>>? IndexesAction { get; set; }
}
public interface IMongoEntityModelBuilder
@ -19,4 +25,8 @@ public interface IMongoEntityModelBuilder
string CollectionName { get; set; }
BsonClassMap BsonMap { get; }
CreateCollectionOptions CreateCollectionOptions { get; }
Action<IMongoIndexManager<BsonDocument>>? IndexesAction { get; set; }
}

19
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs

@ -1,27 +1,41 @@
using MongoDB.Bson.Serialization;
using System;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Volo.Abp.MongoDB;
public class MongoEntityModelBuilder<TEntity> :
IMongoEntityModel,
IHasBsonClassMap,
IHasCreateCollectionOptions,
IHasMongoIndexManagerAction,
IMongoEntityModelBuilder,
IMongoEntityModelBuilder<TEntity>
{
public Type EntityType { get; }
public string CollectionName { get; set; } = default!;
public Action<IMongoIndexManager<BsonDocument>>? IndexesAction { get; set; }
BsonClassMap IMongoEntityModelBuilder.BsonMap => _bsonClassMap;
BsonClassMap<TEntity> IMongoEntityModelBuilder<TEntity>.BsonMap => _bsonClassMap;
private readonly BsonClassMap<TEntity> _bsonClassMap;
CreateCollectionOptions<TEntity> IMongoEntityModelBuilder<TEntity>.CreateCollectionOptions => _createCollectionOptions;
CreateCollectionOptions IMongoEntityModelBuilder.CreateCollectionOptions => _createCollectionOptions;
private readonly CreateCollectionOptions<TEntity> _createCollectionOptions;
public MongoEntityModelBuilder()
{
EntityType = typeof(TEntity);
_bsonClassMap = new BsonClassMap<TEntity>();
_createCollectionOptions = new CreateCollectionOptions<TEntity>();
_bsonClassMap.ConfigureAbpConventions();
}
@ -29,4 +43,9 @@ public class MongoEntityModelBuilder<TEntity> :
{
return _bsonClassMap;
}
public CreateCollectionOptions GetCreateCollectionOptions()
{
return _createCollectionOptions;
}
}

25
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs

@ -74,7 +74,10 @@ public class MongoModelBuilder : IMongoModelBuilder
baseClasses.AddRange(entityModel.EntityType.GetBaseClasses(includeObject: false));
CreateCollectionIfNotExists(dbContext, entityModel.CollectionName);
var createCollectionOptions = entityModel.As<IHasCreateCollectionOptions>().GetCreateCollectionOptions();
var indexesAction = entityModel.As<IHasMongoIndexManagerAction>().IndexesAction;
CreateCollectionIfNotExists(dbContext, entityModel.CollectionName, createCollectionOptions);
CreateCollectionIndexes(dbContext, entityModel.CollectionName, indexesAction);
}
baseClasses = baseClasses.Distinct().ToList();
@ -141,7 +144,7 @@ public class MongoModelBuilder : IMongoModelBuilder
typeof(MongoEntityModelBuilder<>).MakeGenericType(entityType)
)!
);
buildAction?.Invoke(model);
}
@ -150,14 +153,24 @@ public class MongoModelBuilder : IMongoModelBuilder
return _entityModelBuilders.Values.Cast<IMongoEntityModel>().ToImmutableList();
}
protected virtual void CreateCollectionIfNotExists(AbpMongoDbContext dbContext, string collectionName)
protected virtual void CreateCollectionIfNotExists(AbpMongoDbContext dbContext, string collectionName, CreateCollectionOptions createCollectionOptions)
{
var filter = new BsonDocument("name", collectionName);
var options = new ListCollectionNamesOptions { Filter = filter };
var options = new ListCollectionsOptions { Filter = filter };
if (!dbContext.Database.ListCollections(options).Any())
{
dbContext.Database.CreateCollection(collectionName, createCollectionOptions);
}
}
if (!dbContext.Database.ListCollectionNames(options).Any())
protected virtual void CreateCollectionIndexes(AbpMongoDbContext dbContext, string collectionName, Action<IMongoIndexManager<BsonDocument>>? indexesAction = null)
{
var collection = dbContext.Database.GetCollection<BsonDocument>(collectionName);
if (collection != null)
{
dbContext.Database.CreateCollection(collectionName);
indexesAction?.Invoke(collection.Indexes);
}
}
}

Loading…
Cancel
Save