docs/en/framework/architecture/best-practices/mongodb-integration.md
//[doc-seo]
{
"Description": "Learn best practices for integrating MongoDB in your ABP Framework applications with clear guidelines on defining MongoDbContext."
}
This document offers best practices for implementing MongoDB integration in your modules and applications.
Ensure you've read the MongoDB Integration document first.
MongoDbContext interface and class for each module.MongoDbContext that inherits from IAbpMongoDbContext.ConnectionStringName attribute to the MongoDbContext interface.IMongoCollection<TEntity> properties to the MongoDbContext interface only for the aggregate roots. Example:[ConnectionStringName("AbpIdentity")]
public interface IAbpIdentityMongoDbContext : IAbpMongoDbContext
{
IMongoCollection<IdentityUser> Users { get; }
IMongoCollection<IdentityRole> Roles { get; }
}
MongoDbContext from the AbpMongoDbContext class.ConnectionStringName attribute to the MongoDbContext class.interface for the MongoDbContext class. Example:[ConnectionStringName("AbpIdentity")]
public class AbpIdentityMongoDbContext : AbpMongoDbContext, IAbpIdentityMongoDbContext
{
public IMongoCollection<IdentityUser> Users => Collection<IdentityUser>();
public IMongoCollection<IdentityRole> Roles => Collection<IdentityRole>();
//code omitted for brevity
}
CollectionPrefix property to the DbContext class. Set default value from a constant. Example:public static string CollectionPrefix { get; set; } = AbpIdentityConsts.DefaultDbTablePrefix;
Used the same constant defined for the EF Core integration table prefix in this example.
CollectionPrefix value for a module to create unique collection names in a shared database. Abp collection prefix is reserved for ABP core modules.CreateModel method of the MongoDbContext. Example:protected override void CreateModel(IMongoModelBuilder modelBuilder)
{
base.CreateModel(modelBuilder);
modelBuilder.ConfigureIdentity();
}
CreateModel method. Instead, create an extension method for the IMongoModelBuilder. Use ConfigureModuleName as the method name. Example:public static class AbpIdentityMongoDbContextExtensions
{
public static void ConfigureIdentity(
this IMongoModelBuilder builder,
Action<IdentityMongoModelBuilderConfigurationOptions> optionsAction = null)
{
Check.NotNull(builder, nameof(builder));
builder.Entity<IdentityUser>(b =>
{
b.CollectionName = AbpIdentityDbProperties.DbTablePrefix + "Users";
});
builder.Entity<IdentityRole>(b =>
{
b.CollectionName = AbpIdentityDbProperties.DbTablePrefix + "Roles";
});
}
}
MongoDbRepository<TMongoDbContext, TEntity, TKey> class and implement the corresponding repository interface. Example:public class MongoIdentityUserRepository
: MongoDbRepository<IAbpIdentityMongoDbContext, IdentityUser, Guid>,
IIdentityUserRepository
{
public MongoIdentityUserRepository(
IMongoDbContextProvider<IAbpIdentityMongoDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
}
cancellationToken to the MongoDB Driver using the GetCancellationToken helper method. Example:public async Task<IdentityUser> FindByNormalizedUserNameAsync(
string normalizedUserName,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await (await GetQueryableAsync())
.FirstOrDefaultAsync(
u => u.NormalizedUserName == normalizedUserName,
GetCancellationToken(cancellationToken)
);
}
GetCancellationToken fallbacks to the ICancellationTokenProvider.Token to obtain the cancellation token if it is not provided by the caller code.
includeDetails parameters for the repository implementation since MongoDB loads the aggregate root as a whole (including sub collections) by default.GetQueryableAsync() method to obtain an IQueryable<TEntity> to perform queries wherever possible. Because;
GetQueryableAsync() method automatically uses the ApplyDataFilters method to filter the data based on the current data filters (like soft delete and multi-tenancy).IQueryable<TEntity> makes the code as much as similar to the EF Core repository implementation and easy to write and read.GetQueryableAsync() method.MongoDbContext to the IServiceCollection using the AddMongoDbContext<TMongoDbContext> method.AddMongoDbContext<TMongoDbContext> method. Example:[DependsOn(
typeof(AbpIdentityDomainModule),
typeof(AbpUsersMongoDbModule)
)]
public class AbpIdentityMongoDbModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddMongoDbContext<AbpIdentityMongoDbContext>(options =>
{
options.AddRepository<IdentityUser, MongoIdentityUserRepository>();
options.AddRepository<IdentityRole, MongoIdentityRoleRepository>();
});
}
}
Notice that this module class also calls the static BsonClassMap configuration method defined above.