Skip to content

Commit

Permalink
Imported Tingle.Extensions.MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Mar 7, 2024
1 parent d8d18af commit 9917db3
Show file tree
Hide file tree
Showing 51 changed files with 3,734 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- { name: 'Tingle.Extensions.Http' }
- { name: 'Tingle.Extensions.Http.Authentication' }
- { name: 'Tingle.Extensions.JsonPatch' }
- { name: 'Tingle.Extensions.MongoDB' }
- { name: 'Tingle.Extensions.PhoneValidators' }
- { name: 'Tingle.Extensions.Primitives' }
- { name: 'Tingle.Extensions.Processing' }
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"Bson",
"etag",
"Ksuid",
"libphonenumber",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This repository contains projects/libraries for adding useful functionality to d
|[`Tingle.AspNetCore.DataProtection.MongoDB`](https://www.nuget.org/packages/Tingle.AspNetCore.DataProtection.MongoDB/)|Data Protection store in [MongoDB](https://mongodb.com) for ASP.NET Core. See [docs](./src/Tingle.AspNetCore.DataProtection.MongoDB/README.md) and [sample](./samples/DataProtectionMongoDBSample).|
|[`Tingle.AspNetCore.JsonPatch.NewtonsoftJson`](https://www.nuget.org/packages/Tingle.AspNetCore.JsonPatch.NewtonsoftJson/)|Helpers for validation when working with JsonPatch in ASP.NET Core. See [docs](./src/Tingle.AspNetCore.JsonPatch.NewtonsoftJson/README.md) and [blog](https://maxwellweru.com/blog/2020-11-17-immutable-properties-with-json-patch-in-aspnet-core).|
|[`Tingle.AspNetCore.Tokens`](https://www.nuget.org/packages/Tingle.AspNetCore.Tokens/)|Support for generation of continuation tokens in ASP.NET Core with optional expiry. Useful for pagination, user invite tokens, expiring operation tokens, etc. This is availed through the `ContinuationToken<T>` and `TimedContinuationToken<T>` types. See [docs](./src/Tingle.AspNetCore.Tokens/README.md) and [sample](./samples/TokensSample).|
|[`Tingle.Extensions.Caching.MongoDB`](https://www.nuget.org/packages/Tingle.Extensions.Caching.MongoDB/)|Distributed caching implemented with [MongoDB](https://mongodb.com) on top of `IDistributedCache`, inspired by [CosmosCache](https://github.com/Azure/Microsoft.Extensions.Caching.Cosmos). See [docs](./src/Tingle.Extensions.Caching.MongoDB/README.md)and [sample](./samples/AspNetCoreSessionState)|
|[`Tingle.Extensions.Caching.MongoDB`](https://www.nuget.org/packages/Tingle.Extensions.Caching.MongoDB/)|Distributed caching implemented with [MongoDB](https://mongodb.com) on top of `IDistributedCache`, inspired by [CosmosCache](https://github.com/Azure/Microsoft.Extensions.Caching.Cosmos). See [docs](./src/Tingle.Extensions.Caching.MongoDB/README.md) and [sample](./samples/AspNetCoreSessionState)|
|[`Tingle.Extensions.DataAnnotations`](https://www.nuget.org/packages/Tingle.Extensions.DataAnnotations/)|Additional data validation attributes in the `System.ComponentModel.DataAnnotations` namespace. Some of this should have been present in the framework but are very specific to some use cases. For example `FiveStarRatingAttribute`. See [docs](./src/Tingle.Extensions.DataAnnotations/README.md).|
|[`Tingle.Extensions.Http`](https://www.nuget.org/packages/Tingle.Extensions.Http/)|Lightweight abstraction around `HttpClient` which can be used to build custom client with response wrapping semantics. See [docs](./src/Tingle.Extensions.Http/README.md).|
|[`Tingle.Extensions.Http.Authentication`](https://www.nuget.org/packages/Tingle.Extensions.Http.Authentication/)|Authentication providers for use with HttpClient and includes support for DI via `Microsoft.Extensions.Http`. See [docs](./src/Tingle.Extensions.Http.Authentication/README.md) and [sample](./samples/HttpAuthenticationSample).|
Expand Down
47 changes: 34 additions & 13 deletions Tingle.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.Http.Auth
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.JsonPatch", "src\Tingle.Extensions.JsonPatch\Tingle.Extensions.JsonPatch.csproj", "{913C0212-58AC-42B7-B555-F96B8E287E7F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.MongoDB", "src\Tingle.Extensions.MongoDB\Tingle.Extensions.MongoDB.csproj", "{17F58DAD-0789-4AD7-921E-24CEA09DC68B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.PhoneValidators", "src\Tingle.Extensions.PhoneValidators\Tingle.Extensions.PhoneValidators.csproj", "{F46ADD04-B716-4E9B-9799-7C47DDDB08FC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tingle.Extensions.Primitives", "src\Tingle.Extensions.Primitives\Tingle.Extensions.Primitives.csproj", "{3012AF4E-270B-4293-9C7B-51003F00A8D6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.Primitives", "src\Tingle.Extensions.Primitives\Tingle.Extensions.Primitives.csproj", "{3012AF4E-270B-4293-9C7B-51003F00A8D6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.Processing", "src\Tingle.Extensions.Processing\Tingle.Extensions.Processing.csproj", "{A803DE4B-B050-48F2-82A1-8E947D8FB96C}"
EndProject
Expand Down Expand Up @@ -63,9 +65,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.Http.Auth
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.JsonPatch.Tests", "tests\Tingle.Extensions.JsonPatch.Tests\Tingle.Extensions.JsonPatch.Tests.csproj", "{B82E2980-E145-4341-BAE0-8FAE1F110D0C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.MongoDB.Tests", "tests\Tingle.Extensions.MongoDB.Tests\Tingle.Extensions.MongoDB.Tests.csproj", "{D8BFE67A-C3A7-4523-8119-D9FCD5EF3E7F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.PhoneValidators.Tests", "tests\Tingle.Extensions.PhoneValidators.Tests\Tingle.Extensions.PhoneValidators.Tests.csproj", "{526B37AD-256A-445A-9A42-E5C53989B11E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tingle.Extensions.Primitives.Tests", "tests\Tingle.Extensions.Primitives.Tests\Tingle.Extensions.Primitives.Tests.csproj", "{33E2AC82-DC46-42CA-A41B-0D8D97019402}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.Primitives.Tests", "tests\Tingle.Extensions.Primitives.Tests\Tingle.Extensions.Primitives.Tests.csproj", "{33E2AC82-DC46-42CA-A41B-0D8D97019402}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tingle.Extensions.Processing.Tests", "tests\Tingle.Extensions.Processing.Tests\Tingle.Extensions.Processing.Tests.csproj", "{978023EA-2ED5-4A28-96AD-4BB914EF2BE5}"
EndProject
Expand All @@ -88,13 +92,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataProtectionMongoDBSample
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpAuthenticationSample", "samples\HttpAuthenticationSample\HttpAuthenticationSample.csproj", "{37ED98F0-3129-49B8-BF60-3BDEBBB34822}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDBSample", "samples\MongoDBSample\MongoDBSample.csproj", "{D68821D8-95A3-4DAB-9E90-676D9BE30AFB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerilogSample", "samples\SerilogSample\SerilogSample.csproj", "{CFEE0754-9DAF-4AA6-98F3-477F065D7DC4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TokensSample", "samples\TokensSample\TokensSample.csproj", "{AC04C113-8F75-43BA-8FEE-987475A87C58}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "generator", "generator", "{9071B0C9-DE4D-411D-A9D3-CB7326CBBD80}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StaticDataGenerator", "generator\StaticDataGenerator.csproj", "{009C5985-9DD4-45A8-A31E-4E6B7FE5EE78}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StaticDataGenerator", "generator\StaticDataGenerator.csproj", "{009C5985-9DD4-45A8-A31E-4E6B7FE5EE78}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -142,10 +148,18 @@ Global
{913C0212-58AC-42B7-B555-F96B8E287E7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{913C0212-58AC-42B7-B555-F96B8E287E7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{913C0212-58AC-42B7-B555-F96B8E287E7F}.Release|Any CPU.Build.0 = Release|Any CPU
{17F58DAD-0789-4AD7-921E-24CEA09DC68B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17F58DAD-0789-4AD7-921E-24CEA09DC68B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17F58DAD-0789-4AD7-921E-24CEA09DC68B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17F58DAD-0789-4AD7-921E-24CEA09DC68B}.Release|Any CPU.Build.0 = Release|Any CPU
{F46ADD04-B716-4E9B-9799-7C47DDDB08FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F46ADD04-B716-4E9B-9799-7C47DDDB08FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F46ADD04-B716-4E9B-9799-7C47DDDB08FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F46ADD04-B716-4E9B-9799-7C47DDDB08FC}.Release|Any CPU.Build.0 = Release|Any CPU
{3012AF4E-270B-4293-9C7B-51003F00A8D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3012AF4E-270B-4293-9C7B-51003F00A8D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3012AF4E-270B-4293-9C7B-51003F00A8D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3012AF4E-270B-4293-9C7B-51003F00A8D6}.Release|Any CPU.Build.0 = Release|Any CPU
{A803DE4B-B050-48F2-82A1-8E947D8FB96C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A803DE4B-B050-48F2-82A1-8E947D8FB96C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A803DE4B-B050-48F2-82A1-8E947D8FB96C}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -198,10 +212,18 @@ Global
{B82E2980-E145-4341-BAE0-8FAE1F110D0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B82E2980-E145-4341-BAE0-8FAE1F110D0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B82E2980-E145-4341-BAE0-8FAE1F110D0C}.Release|Any CPU.Build.0 = Release|Any CPU
{D8BFE67A-C3A7-4523-8119-D9FCD5EF3E7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8BFE67A-C3A7-4523-8119-D9FCD5EF3E7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8BFE67A-C3A7-4523-8119-D9FCD5EF3E7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8BFE67A-C3A7-4523-8119-D9FCD5EF3E7F}.Release|Any CPU.Build.0 = Release|Any CPU
{526B37AD-256A-445A-9A42-E5C53989B11E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{526B37AD-256A-445A-9A42-E5C53989B11E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{526B37AD-256A-445A-9A42-E5C53989B11E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{526B37AD-256A-445A-9A42-E5C53989B11E}.Release|Any CPU.Build.0 = Release|Any CPU
{33E2AC82-DC46-42CA-A41B-0D8D97019402}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{33E2AC82-DC46-42CA-A41B-0D8D97019402}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33E2AC82-DC46-42CA-A41B-0D8D97019402}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33E2AC82-DC46-42CA-A41B-0D8D97019402}.Release|Any CPU.Build.0 = Release|Any CPU
{978023EA-2ED5-4A28-96AD-4BB914EF2BE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{978023EA-2ED5-4A28-96AD-4BB914EF2BE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{978023EA-2ED5-4A28-96AD-4BB914EF2BE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -234,6 +256,10 @@ Global
{37ED98F0-3129-49B8-BF60-3BDEBBB34822}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37ED98F0-3129-49B8-BF60-3BDEBBB34822}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37ED98F0-3129-49B8-BF60-3BDEBBB34822}.Release|Any CPU.Build.0 = Release|Any CPU
{D68821D8-95A3-4DAB-9E90-676D9BE30AFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D68821D8-95A3-4DAB-9E90-676D9BE30AFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D68821D8-95A3-4DAB-9E90-676D9BE30AFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D68821D8-95A3-4DAB-9E90-676D9BE30AFB}.Release|Any CPU.Build.0 = Release|Any CPU
{CFEE0754-9DAF-4AA6-98F3-477F065D7DC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CFEE0754-9DAF-4AA6-98F3-477F065D7DC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFEE0754-9DAF-4AA6-98F3-477F065D7DC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -242,14 +268,6 @@ Global
{AC04C113-8F75-43BA-8FEE-987475A87C58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC04C113-8F75-43BA-8FEE-987475A87C58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC04C113-8F75-43BA-8FEE-987475A87C58}.Release|Any CPU.Build.0 = Release|Any CPU
{3012AF4E-270B-4293-9C7B-51003F00A8D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3012AF4E-270B-4293-9C7B-51003F00A8D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3012AF4E-270B-4293-9C7B-51003F00A8D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3012AF4E-270B-4293-9C7B-51003F00A8D6}.Release|Any CPU.Build.0 = Release|Any CPU
{33E2AC82-DC46-42CA-A41B-0D8D97019402}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{33E2AC82-DC46-42CA-A41B-0D8D97019402}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33E2AC82-DC46-42CA-A41B-0D8D97019402}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33E2AC82-DC46-42CA-A41B-0D8D97019402}.Release|Any CPU.Build.0 = Release|Any CPU
{009C5985-9DD4-45A8-A31E-4E6B7FE5EE78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{009C5985-9DD4-45A8-A31E-4E6B7FE5EE78}.Debug|Any CPU.Build.0 = Debug|Any CPU
{009C5985-9DD4-45A8-A31E-4E6B7FE5EE78}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -269,7 +287,9 @@ Global
{5BFAD4DB-D6A6-44F4-ACAB-B7B04E5A052E} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{47F95938-964A-47FE-A0D6-1EDD0893455B} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{913C0212-58AC-42B7-B555-F96B8E287E7F} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{17F58DAD-0789-4AD7-921E-24CEA09DC68B} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{F46ADD04-B716-4E9B-9799-7C47DDDB08FC} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{3012AF4E-270B-4293-9C7B-51003F00A8D6} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{A803DE4B-B050-48F2-82A1-8E947D8FB96C} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{6A1901A5-D01F-47E2-9ED5-2BE4CCE95100} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{29035EF2-2391-4441-AAC5-85AA43586EEB} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
Expand All @@ -283,7 +303,9 @@ Global
{41980843-7F99-4AD2-B9E9-B13FC349A150} = {815F0941-3B70-4705-A583-AF627559595C}
{D0C66D3A-ED1F-486E-AA19-BDBB19025368} = {815F0941-3B70-4705-A583-AF627559595C}
{B82E2980-E145-4341-BAE0-8FAE1F110D0C} = {815F0941-3B70-4705-A583-AF627559595C}
{D8BFE67A-C3A7-4523-8119-D9FCD5EF3E7F} = {815F0941-3B70-4705-A583-AF627559595C}
{526B37AD-256A-445A-9A42-E5C53989B11E} = {815F0941-3B70-4705-A583-AF627559595C}
{33E2AC82-DC46-42CA-A41B-0D8D97019402} = {815F0941-3B70-4705-A583-AF627559595C}
{978023EA-2ED5-4A28-96AD-4BB914EF2BE5} = {815F0941-3B70-4705-A583-AF627559595C}
{A8AB6597-DEBB-4828-AE1B-A11FD818E428} = {815F0941-3B70-4705-A583-AF627559595C}
{8E611861-09A3-4AE4-8392-E7CB9BE02B3B} = {815F0941-3B70-4705-A583-AF627559595C}
Expand All @@ -292,10 +314,9 @@ Global
{B97964EC-658A-4205-AA5A-BB814B191C35} = {CE7124E9-01B3-4AD6-8B8F-FB302E60FB7F}
{ACED6271-2F75-4E3B-BB79-9D40B74D6A51} = {CE7124E9-01B3-4AD6-8B8F-FB302E60FB7F}
{37ED98F0-3129-49B8-BF60-3BDEBBB34822} = {CE7124E9-01B3-4AD6-8B8F-FB302E60FB7F}
{D68821D8-95A3-4DAB-9E90-676D9BE30AFB} = {CE7124E9-01B3-4AD6-8B8F-FB302E60FB7F}
{CFEE0754-9DAF-4AA6-98F3-477F065D7DC4} = {CE7124E9-01B3-4AD6-8B8F-FB302E60FB7F}
{AC04C113-8F75-43BA-8FEE-987475A87C58} = {CE7124E9-01B3-4AD6-8B8F-FB302E60FB7F}
{3012AF4E-270B-4293-9C7B-51003F00A8D6} = {9546186D-D4E1-4EDB-956E-1F81C7F4DB72}
{33E2AC82-DC46-42CA-A41B-0D8D97019402} = {815F0941-3B70-4705-A583-AF627559595C}
{009C5985-9DD4-45A8-A31E-4E6B7FE5EE78} = {9071B0C9-DE4D-411D-A9D3-CB7326CBBD80}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
15 changes: 15 additions & 0 deletions samples/MongoDBSample/MongoDBSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<UserSecretsId>8bd0ef6d-24c8-4ae8-8397-32053d171b84</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Tingle.Extensions.MongoDB\Tingle.Extensions.MongoDB.csproj" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions samples/MongoDBSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

Console.WriteLine("Hello World!");

var dbName = Guid.NewGuid().ToString().Replace("_", "")[..10];
var connectionString = $"mongodb://localhost:27017/{dbName}";

var services = new ServiceCollection();
services.AddLogging();
services.AddMongoDbContext<SampleDbContext>(options =>
{
options.UseMongoConnectionString(connectionString);
});

var sp = services.BuildServiceProvider(validateScopes: true);

// resolving directly
using var scope = sp.CreateScope();
var provider = scope.ServiceProvider;

var context = provider.GetRequiredService<SampleDbContext>();
var persons = await context.Persons.AsQueryable().ToListAsync();
Console.WriteLine($"Found {persons.Count} persons");

class Person
{
[BsonId]
public string? Id { get; set; }

public string? Name { get; set; }
}

class SampleDbContext(MongoDbContextOptions<SampleDbContext> options) : MongoDbContext(options)
{
public IMongoCollection<Person> Persons => Collection<Person>("Persons");

protected override void OnConfiguring(MongoDbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
}
8 changes: 8 additions & 0 deletions samples/MongoDBSample/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
8 changes: 8 additions & 0 deletions samples/MongoDBSample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
42 changes: 42 additions & 0 deletions src/Tingle.Extensions.MongoDB/DatabaseSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;

namespace Tingle.Extensions.MongoDB;

/// <summary>
/// Helper for performing creation.
/// </summary>
/// <typeparam name="TContext">The type of context to be used.</typeparam>
internal class DatabaseSetup<TContext>(IServiceScopeFactory scopeFactory, ILogger<DatabaseSetup<TContext>> logger) : IHostedService where TContext : MongoDbContext
{
private readonly IServiceScopeFactory scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
private readonly ILogger logger = logger ?? throw new ArgumentNullException(nameof(logger));

public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = scopeFactory.CreateScope();
var provider = scope.ServiceProvider;

// Check if explicitly told to do creation
var environment = provider.GetRequiredService<IHostEnvironment>();
var configuration = provider.GetRequiredService<IConfiguration>();
if (bool.TryParse(configuration["MONGO_CREATE_DATABASE"], out var b) && b)
{
// Create database
logger.LogInformation("Creating MongoDB database ...");
var context = provider.GetRequiredService<TContext>();
await context.EnsureCreatedAsync(cancellationToken).ConfigureAwait(false);
logger.LogInformation("Completed MongoDB database creation.");
}
else
{
logger.LogDebug("Database creation skipped.");
return;
}
}

public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using MongoDB.Driver;

namespace Tingle.Extensions.MongoDB.Diagnostics;

internal class MongoDbContextHealthCheck<TContext> : IHealthCheck where TContext : MongoDbContext
{
private readonly IMongoDatabase database;
public MongoDbContextHealthCheck(TContext context)
{
database = context.Database ?? throw new ArgumentNullException(nameof(database));
}

/// <inheritdoc/>
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
using (await database.ListCollectionsAsync(cancellationToken: cancellationToken).ConfigureAwait(false))
{

}

return HealthCheckResult.Healthy();
}
catch (Exception ex)
{
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
}
Loading

0 comments on commit 9917db3

Please sign in to comment.