Repository pattern implementation (using linq) of MongoDB in .NET Framework with optional Dependency Tracking implementation.
-
Default
ConnectionStringSettingName
is set to "MongoDocConnection", but you can configure this by calling a static method as below:Settings.ConnectionStringSettingName = "MongoDocConnection";
-
You can configure
ExpiryAfterSeconds
index for a specific collection by calling a static method as below:Settings.Configure<Activity>(2592000);
-
If you intend to use Dependency Tracking, you can specify commands to not track by setting the NotTrackedCommands
IEnumerable<string>
:Settings.NotTrackedCommands = new List<string>() { "isMaster", "buildInfo", "getLastError", "saslStart", "saslContinue" };
-
Example:
public class RepoUnitTest { public RepoUnitTest() { Settings.ConnectionStringSettingName = "MongoDocConnection"; Settings.Configure<Activity>(2592000); } //.... other methods and properties }
Create a document model implementing
public class Activity : BaseFile
{
public string Name { get; set; }
}
-
Find asynchronous (using Linq Expression.)
Activity result = ActivityRepository.FindAsync(x => x.Id == new ObjectId("5e36997898d2c15a400f8968")).Result;
-
Get asynchronous (by Id)
Activity result = ActivityRepository.GetAsync(new ObjectId("5e36997898d2c15a400f8968")).Result;
-
Get asynchronous (using Linq Expression.)
Has paged records in a
Tuple<IEnumerable<T>, long>
of records and total count.var result = ActivityRepository.GetAsync(1, 10, x => x.Name.Contains("abc") && x.Deleted == false).Result; Console.WriteLine($"Output:\nTotal: {result.Item2}\n{JToken.Parse(JsonConvert.SerializeObject(result.Item1)).ToString(Formatting.Indented)}");
-
Get synchronous (using Linq Expression.)
Has nonpaged records.
var result = ActivityRepository.Get(x => x.Name.Contains("abc") && x.Deleted == false);
-
In synchronous (Get by
IN
filter. Nonpaged records)List<string> names = new List<string> { "abc", "def", "ghi" }; var result = ActivityRepository.In(x => x.Name, names);
-
Insert asynchronous
Activity activity = new Activity() { Name = "abc" }; Activity result = ActivityRepository.InsertAsync(activity).Result;
-
Update asynchronous
Activity activity = new Activity() { Id = new ObjectId("5e36998998d2c1540ca23894"), Name = "abc3" }; bool result = ActivityRepository.UpdateAsync(activity).Result;
-
Delete asynchronous (by Id)
bool result = ActivityRepository.DeleteAsync(new ObjectId("5e36998998d2c1540ca23894")).Result;
-
Count asynchronous
long result = ActivityRepository.CountAsync().Result;
-
Exists asynchronous (using Linq Expression)
bool result = ActivityRepository.ExistsAsync(x => x.Name == "abc").Result;
To use dependency tracking implement the IDependencyTracker
interface as below:
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.