This is mongo repository. It has the following types of repository:
ReadOnlyRepository
↙ ↘
CachedReadOnlyRepository ReadWriteRepository
↘
ReadWriteWithAuditRepository
* Multi-tenant with prefix or suffix
* Lazy loading for database/collection/index
* Azure Application Insight Telemetry
* Support Read/Write Separation Replica sets
* Support pagination results
* Support auditing
* Support cache
* Support Queryable
* Auto index lazy check/creation
* Support Single Field Index
* Support Compound Index
* Support Geospatial Index
* Support Time-to-live Index (TTL)
* Support Nested Index
* Support Expression Index
* Support Partial Index
* Support write your Own customized Index (Eager index builder)
Install this nuget package to use it.
- create options class to extend MongoDbOptions for setting up MongoDB connection string
// Your Options class
public class SampleOptions : MongoDbOptions {}
// Add to your appsettings.json
"SampleOptions": {
"ReadWriteConnection": "mongodb://localhost:27017", // ReadWrite endpoint.
"ReadOnlyConnection": "mongodb://localhost:27017", // Read replica endpoint
"DefaultDatabase": "Sample" //if not using attribute define database.
}
- in your entity class, implement
IEntity<KeyType>eg.IEntity<ObjectId>,IEntity<string>etc.. then add class level attribute
[EntityDatabase("SampleDatabase")]
[EntityCollection("SampleCollection")]
public class SampleEntity : IEntity<ObjectId>
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string ToDescription()
{
return $"This is a sample with {Id}";
}
}
- Define your Repository.
public interface ISampleReadWriteRepository : IReadWriteRepository<SampleEntity, ObjectId> {}
public class SampleRepository : ReadWriteRepository<SampleEntity, ObjectId>, ISampleReadWriteRepository
{
public SampleRepository(IOptions<SampleOptions> mongoOptions, IMongoClientFactory factory)
: base(mongoOptions, factory) {}
}
- Set start up.
builder.Services.Configure<SampleOptions>(builder.Configuration.GetSection("SampleOptions"));
builder.Services.AddMongoClientFactory();
builder.Services.AddScoped<ISampleReadWriteRepository, SampleRepository>();
- Remove the nuget package of
YSMongoRepositoryfrom your project. - In your project,
add project reference, addYSMongoRepositoryinto your project. - Happy debugging 🤞
Database level multi-tenant is supported by providing ITenantIdentificationService.
In this ITenantIdentificationService, you need to implement your own logic to provide suffix or prefix.
This repository will automaticaly create that database, collection and index for you with lazy-loading.
Recommended way is to have an interface inherit from the ITenantIdentificationService, then implement your logic and pass into repository. (see example of MultiTenantIdentificationService.cs in Sample project)
This repository will only Create/check the resource that you going to invoke. What are the benefits?
- You don't need know the whole DB structure.
- Your service will not touch the DB you don't use.
- It will automatically create
Database,Collectionandindexfor you if resource is not there, especially in multi-tenant scenario.
In some scenarios below, you may be interested in what query you are using.
- Would like to know what query is using to fetch data.
- Would like to know how the query hit the index.
- Would like to optimize the query. Then you can do the following step:
- -> Go into your targeting azure application insight
- -> Go into Transaction search ( you can also search in Logs )
- -> Find the dependency record for your mongo db transaction
- -> View the detail in the
commandsession. - -> Grab the
filterpart to yourcompassthat you can useExplainto explore it.
To use Read/Write Separation, simplely just update your connection string:
"SampleOptions": {
"ReadWriteConnection": "mongodb://localhost:27017", // ReadWrite endpoint.
"ReadOnlyConnection": "mongodb://localhost:27017", // Read replica endpoint
"DefaultDatabase": "Sample" //if not using attribute define database.
}
This repository has built-in Pagination Result. Check out PaginatedResult.cs.
To use auditing feature,