TK.MongoDB.Repository
2.1.0
See the version list below for details.
dotnet add package TK.MongoDB.Repository --version 2.1.0
NuGet\Install-Package TK.MongoDB.Repository -Version 2.1.0
<PackageReference Include="TK.MongoDB.Repository" Version="2.1.0" />
paket add TK.MongoDB.Repository --version 2.1.0
#r "nuget: TK.MongoDB.Repository, 2.1.0"
// Install TK.MongoDB.Repository as a Cake Addin #addin nuget:?package=TK.MongoDB.Repository&version=2.1.0 // Install TK.MongoDB.Repository as a Cake Tool #tool nuget:?package=TK.MongoDB.Repository&version=2.1.0
TK.MongoDB.Repository
Repository pattern implementation (using linq) of MongoDB in .NET Framework with optional Dependency Tracking implementation.
Usage
Settings
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 <u>not</u> 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 }
Models
Create a document model implementing $BaseEntity$ to use in repository. The name of this model will be used as collection name in MongoDB.
public class Activity : BaseFile
{
public string Name { get; set; }
}
Repository methods
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;
Dependency Tracking
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}");
}
}
Tests
Refer to TK.MongoDB.Test project for all Unit Tests.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- MongoDB.Driver (>= 2.10.1)
- SharpCompress (>= 0.24.0)
- System.Buffers (>= 4.5.1)
- System.Collections.Immutable (>= 1.7.1)
- System.Numerics.Vectors (>= 4.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.1.6 | 1,946 | 8/29/2022 |
2.1.5 | 1,008 | 1/17/2022 |
2.1.4 | 1,993 | 6/21/2021 |
2.1.3 | 1,026 | 2/24/2021 |
2.1.2 | 1,451 | 10/13/2020 |
2.1.1 | 1,210 | 8/20/2020 |
2.1.0 | 963 | 8/20/2020 |
1.0.10 | 1,046 | 8/16/2020 |
1.0.9 | 908 | 8/11/2020 |
1.0.8 | 999 | 8/10/2020 |
1.0.7 | 1,341 | 6/11/2020 |
1.0.6 | 1,107 | 2/5/2020 |
1.0.5 | 1,117 | 2/5/2020 |
1.0.4 | 942 | 2/4/2020 |
1.0.3 | 998 | 2/3/2020 |
1.0.2 | 1,062 | 2/2/2020 |
Optional Dependency Tracking added