-
Notifications
You must be signed in to change notification settings - Fork 110
WIP: Add Marten Client #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
21e3d2e
WIP: Add Marten Client
Alirexaa 26cb06b
merge main to alirexaa/marten
Alirexaa 8ee8f57
revert removed package refrence
Alirexaa 717374d
Update namespaces and fix client registration
Alirexaa 8f5b6f1
add packge descripion
Alirexaa 7c4d722
Add some tests
Alirexaa a7bd21a
Merge branch 'main' into alirexaa/marten
aaronpowell 2f2a2e4
Merge branch 'main' into alirexaa/marten
Alirexaa ef3d13d
Add conformance tests
Alirexaa c573619
Add example project
Alirexaa 3f0f6a6
Add AppHost test
Alirexaa cab6de8
Make assembly experimental
Alirexaa 2d72650
Merge branch 'main' into alirexaa/marten
aaronpowell 54eb781
Address PR feedback
Alirexaa 592f169
Merge branch 'alirexaa/marten' of https://github.com/CommunityToolkit…
Alirexaa 9fcae03
Merge branch 'main' into alirexaa/marten
Alirexaa 9a9a88a
Merge branch 'main' into alirexaa/marten
Alirexaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Aspire.CommunityToolkit.Marten/Aspire.CommunityToolkit.Marten.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AdditionalPackageTags>postgres marten</AdditionalPackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" /> | ||
<PackageReference Include="Marten" /> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\HealthChecksExtensions.cs" Link="Utils\HealthChecksExtensions.cs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
181 changes: 181 additions & 0 deletions
181
src/Aspire.CommunityToolkit.Marten/MartenApplicationBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
using Aspire; | ||
using Aspire.CommunityToolkit.Marten; | ||
using HealthChecks.NpgSql; | ||
using Marten; | ||
using Microsoft.AspNetCore.DataProtection.KeyManagement; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Logging; | ||
using Npgsql; | ||
using Weasel.Core; | ||
|
||
namespace Microsoft.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// TODO | ||
/// </summary> | ||
public static class MartenApplicationBuilderExtensions | ||
{ | ||
private const string DefaultConfigSectionName = "Aspire:Marten"; | ||
|
||
/// <summary> | ||
/// TODO | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="connectionName"></param> | ||
/// <param name="configureSettings"></param> | ||
/// <param name="configureStoreOptions"></param> | ||
public static void AddMartenClient( | ||
this IHostApplicationBuilder builder, | ||
string connectionName, | ||
Action<MartenSettings>? configureSettings = null, | ||
Action<StoreOptions>? configureStoreOptions = null | ||
) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
ArgumentException.ThrowIfNullOrEmpty(connectionName); | ||
|
||
builder.AddMartenClient(DefaultConfigSectionName, configureSettings, configureStoreOptions, connectionName, serviceKey: null); | ||
} | ||
|
||
/// <summary> | ||
/// TOOD | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="name"></param> | ||
/// <param name="configureSettings"></param> | ||
/// <param name="configureStoreOptions"></param> | ||
public static void AddKeyedMartenClient( | ||
this IHostApplicationBuilder builder, | ||
string name, | ||
Action<MartenSettings>? configureSettings = null, | ||
Action<StoreOptions>? configureStoreOptions = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
ArgumentException.ThrowIfNullOrEmpty(name); | ||
|
||
builder.AddMartenClient( | ||
$"{DefaultConfigSectionName}:{name}", | ||
configureSettings, | ||
configureStoreOptions, | ||
connectionName: name, | ||
serviceKey: name); | ||
} | ||
|
||
private static void AddMartenClient( | ||
this IHostApplicationBuilder builder, | ||
string configurationSectionName, | ||
Action<MartenSettings>? configureSettings, | ||
Action<StoreOptions>? configureStoreOptions, | ||
string connectionName, | ||
object? serviceKey) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
|
||
var configSection = builder.Configuration.GetSection(configurationSectionName); | ||
|
||
MartenSettings settings = new(); | ||
configSection.Bind(settings); | ||
|
||
if (builder.Configuration.GetConnectionString(connectionName) is string connectionString) | ||
{ | ||
settings.ConnectionString = connectionString; | ||
} | ||
|
||
configureSettings?.Invoke(settings); | ||
|
||
builder.RegisterMartenServices(settings, configurationSectionName, connectionName, serviceKey, configureStoreOptions); | ||
|
||
|
||
if (!settings.DisableTracing) | ||
{ | ||
builder.Services.AddOpenTelemetry() | ||
.WithTracing(tracing => | ||
{ | ||
tracing.AddSource("Marten"); | ||
}) | ||
.WithMetrics(metrics => | ||
{ | ||
metrics.AddMeter("Marten"); | ||
}); | ||
} | ||
|
||
if (!settings.DisableMetrics) | ||
{ | ||
builder.Services.AddOpenTelemetry() | ||
.WithTracing(tracing => | ||
{ | ||
tracing.AddSource("Marten"); | ||
}); | ||
} | ||
|
||
if (!settings.DisableHealthChecks) | ||
{ | ||
builder.TryAddHealthCheck(new HealthCheckRegistration( | ||
serviceKey is null ? "Aspire.Marten" : $"Aspire.Marten_{connectionName}", | ||
sp => new NpgSqlHealthCheck( | ||
new NpgSqlHealthCheckOptions(serviceKey is null | ||
? sp.GetRequiredService<NpgsqlDataSource>() | ||
: sp.GetRequiredKeyedService<NpgsqlDataSource>(serviceKey))), | ||
failureStatus: default, | ||
tags: default, | ||
timeout: default)); | ||
} | ||
} | ||
|
||
private static void RegisterMartenServices(this IHostApplicationBuilder builder, MartenSettings settings, string configurationSectionName, string connectionName, object? serviceKey, Action<StoreOptions>? configureStoreOptions) | ||
{ | ||
|
||
builder.Services.AddKeyedSingleton<NpgsqlDataSource>(serviceKey is null ? "Aspire.Marten" : $"Aspire.Marten_{connectionName}", (serviceProvider, _) => | ||
{ | ||
ValidateConnectionString(settings.ConnectionString, connectionName, DefaultConfigSectionName); | ||
|
||
var dataSourceBuilder = new NpgsqlDataSourceBuilder(settings.ConnectionString); | ||
dataSourceBuilder.UseLoggerFactory(serviceProvider.GetService<ILoggerFactory>()); | ||
|
||
return dataSourceBuilder.Build(); | ||
}); | ||
|
||
if (serviceKey is null) | ||
{ | ||
builder.Services.AddMarten(storeOption => | ||
{ | ||
ValidateConnectionString(settings.ConnectionString, connectionName, DefaultConfigSectionName); | ||
|
||
configureStoreOptions?.Invoke(storeOption); | ||
storeOption.Connection(settings.ConnectionString!); | ||
}); | ||
} | ||
else | ||
{ | ||
builder.Services.AddKeyedSingleton<DocumentStore>(serviceKey, (sp, _) => | ||
{ | ||
var store = DocumentStore.For((storeOption) => | ||
{ | ||
ValidateConnectionString(settings.ConnectionString, connectionName, DefaultConfigSectionName); | ||
|
||
configureStoreOptions?.Invoke(storeOption); | ||
storeOption.Connection(settings.ConnectionString!); | ||
}); | ||
return store; | ||
}); | ||
builder.Services.AddKeyedSingleton<IDocumentSession>(serviceKey, (sp, _) => | ||
{ | ||
return sp.GetRequiredService<DocumentStore>().LightweightSession(); | ||
}); | ||
builder.Services.AddScoped<IQuerySession>((sp) => | ||
{ | ||
return sp.GetRequiredService<DocumentStore>().QuerySession(); | ||
}); | ||
} | ||
} | ||
|
||
private static void ValidateConnectionString(string? connectionString, string connectionName, string defaultConfigSectionName) | ||
{ | ||
if (string.IsNullOrWhiteSpace(connectionString)) | ||
{ | ||
throw new InvalidOperationException($"ConnectionString is missing. It should be provided in 'ConnectionStrings:{connectionName}' or under the 'ConnectionString' key in '{defaultConfigSectionName}' configuration section."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace Aspire.CommunityToolkit.Marten; | ||
|
||
/// <summary> | ||
/// Provides the client configuration settings for connecting to a PostgreSQL database using Marten. | ||
/// </summary> | ||
public sealed class MartenSettings | ||
{ | ||
/// <summary> | ||
/// The connection string of the PostgreSQL database to connect to. | ||
/// </summary> | ||
public string? ConnectionString { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a boolean value that indicates whether the database health check is disabled or not. | ||
/// </summary> | ||
/// <value> | ||
/// The default value is <see langword="false"/>. | ||
/// </value> | ||
public bool DisableHealthChecks { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a integer value that indicates the database health check timeout in milliseconds. | ||
/// </summary> | ||
public int? HealthCheckTimeout { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a boolean value that indicates whether the OpenTelemetry tracing is disabled or not. | ||
/// </summary> | ||
/// <value> | ||
/// The default value is <see langword="false"/>. | ||
/// </value> | ||
public bool DisableTracing { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a boolean value that indicates whether the OpenTelemetry metrics are disabled or not. | ||
/// </summary> | ||
/// <value> | ||
/// The default value is <see langword="false"/>. | ||
/// </value> | ||
public bool DisableMetrics { get; set; } | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.