Repository pattern implementation of MongoDB collections using LINQ with optional Dependency Tracking in .NET Standard 2.0.
Connection string should be provided by using Connection
class. Inject this class in the following manner if you are using DI:
services.AddTransient(x => new Connection(Configuration.GetConnectionString("DefaultConnection")));
-
Global collection settings can be applied by using
Settings.Configure<T>
whereT
is collection model implementingIDbModel
. This method accepts:- Connection (for connection string)
- Expire After (for ExpiryAfterSeconds index)
- Create Collection Options ()
Use this method with a specific collection model, make sure this method is called once for each application run.
-
If you intend to use Dependency Tracking, you can specify commands to not track by setting the
Settings.NotTrackedCommands
, by default the following commands are not not tracked:- isMaster
- buildInfo
- getLastError
- saslStart
- saslContinue
- listIndexes
Create a collection model implementing IDbModel
interface for use in repository. The name of this model will be used as collection name in MongoDB. For example:
public class Activity : IDbModel
{
public ObjectId Id { get; set; }
public bool Deleted { get; set; }
public DateTime CreationDate { get; set; }
public DateTime? UpdationDate { get; set; }
public string Name { get; set; }
}
To use dependency tracking implement the IDependencyTracker
interface to meet your needs. For example:
public class DependencyTracker : IDependencyTracker
{
public void Dependency(string name, string description, bool success, TimeSpan duration)
{
Console.WriteLine($"{name}-{description}-{success}-{duration}");
}
}
Refer to TK.MongoDB.Test project for all Unit Tests and examples.