From d5e3281dcf65d5ede4515289a42b363a08c98c85 Mon Sep 17 00:00:00 2001 From: Allan Ritchie Date: Tue, 20 May 2025 10:59:56 -0400 Subject: [PATCH 1/2] Proposal implementation --- ...s.cs => SentryMauiAppBuilderExtensions.cs} | 9 ++--- .../MauiElementEventBinderRegistration.cs | 15 --------- src/Sentry.Maui/SentryMauiAppBuilder.cs | 20 +++++++++++ .../SentryMauiAppBuilderExtensions.cs | 33 +++++++++++-------- src/Sentry.Maui/SentryMauiOptions.cs | 12 ------- 5 files changed, 42 insertions(+), 47 deletions(-) rename src/Sentry.Maui.CommunityToolkit.Mvvm/{SentryOptionsExtensions.cs => SentryMauiAppBuilderExtensions.cs} (50%) delete mode 100644 src/Sentry.Maui/MauiElementEventBinderRegistration.cs create mode 100644 src/Sentry.Maui/SentryMauiAppBuilder.cs diff --git a/src/Sentry.Maui.CommunityToolkit.Mvvm/SentryOptionsExtensions.cs b/src/Sentry.Maui.CommunityToolkit.Mvvm/SentryMauiAppBuilderExtensions.cs similarity index 50% rename from src/Sentry.Maui.CommunityToolkit.Mvvm/SentryOptionsExtensions.cs rename to src/Sentry.Maui.CommunityToolkit.Mvvm/SentryMauiAppBuilderExtensions.cs index 5f38609d16..9cdf5124fb 100644 --- a/src/Sentry.Maui.CommunityToolkit.Mvvm/SentryOptionsExtensions.cs +++ b/src/Sentry.Maui.CommunityToolkit.Mvvm/SentryMauiAppBuilderExtensions.cs @@ -5,14 +5,11 @@ namespace Sentry.Maui; /// /// Methods to hook into MAUI and CommunityToolkit.Mvvm /// -public static class SentryOptionsExtensions +public static class SentryMauiAppBuilderExtensions { /// /// Automatically create traces for CommunityToolkit.Mvvm commands /// - public static SentryMauiOptions AddCommunityToolkitIntegration(this SentryMauiOptions options) - { - options.AddDefaultEventBinder(); - return options; - } + public static SentryMauiAppBuilder AddCommunityToolkitIntegration(this SentryMauiAppBuilder builder) + => builder.AddMauiElementBinder(); } diff --git a/src/Sentry.Maui/MauiElementEventBinderRegistration.cs b/src/Sentry.Maui/MauiElementEventBinderRegistration.cs deleted file mode 100644 index 71fbfa216e..0000000000 --- a/src/Sentry.Maui/MauiElementEventBinderRegistration.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Sentry.Maui; - -internal interface IMauiElementEventBinderRegistration -{ - public void Register(IServiceCollection services); -} - -internal class MauiElementEventBinderRegistration<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TEventBinder> : IMauiElementEventBinderRegistration - where TEventBinder : class, IMauiElementEventBinder -{ - public void Register(IServiceCollection services) - { - services.AddSingleton(); - } -} diff --git a/src/Sentry.Maui/SentryMauiAppBuilder.cs b/src/Sentry.Maui/SentryMauiAppBuilder.cs new file mode 100644 index 0000000000..2709547929 --- /dev/null +++ b/src/Sentry.Maui/SentryMauiAppBuilder.cs @@ -0,0 +1,20 @@ +namespace Sentry.Maui; + +/// +/// A builder for configuring Sentry in a .NET MAUI application. +/// +public class SentryMauiAppBuilder(IServiceCollection services) +{ + public IServiceCollection Services => services; + + /// + /// Configures the application by adding a binding for a Maui element of the specified implementation type. + /// + /// The type of implementation for the Maui element binder to be added. + /// The current instance of to allow method chaining. + public SentryMauiAppBuilder AddMauiElementBinder() where TImpl : class, IMauiElementEventBinder + { + services.AddSingleton(); + return this; + } +} diff --git a/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs b/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs index 83e1bd7740..f5f01e665b 100644 --- a/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs +++ b/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs @@ -22,8 +22,8 @@ public static class SentryMauiAppBuilderExtensions /// /// The builder. /// The . - public static MauiAppBuilder UseSentry(this MauiAppBuilder builder) - => UseSentry(builder, (Action?)null); + public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, Action? configureAppBuilder = null) + => UseSentry(builder, (Action?)null, configureAppBuilder); /// /// Uses Sentry integration. @@ -31,8 +31,8 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder) /// The builder. /// The DSN. /// The . - public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, string dsn) - => builder.UseSentry(o => o.Dsn = dsn); + public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, string dsn, Action? configureAppBuilder = null) + => builder.UseSentry(o => o.Dsn = dsn, configureAppBuilder: configureAppBuilder); /// /// Uses Sentry integration. @@ -40,8 +40,11 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, string dsn) /// The builder. /// An action to configure the options. /// The . - public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, - Action? configureOptions) + public static MauiAppBuilder UseSentry( + this MauiAppBuilder builder, + Action? configureOptions, + Action? configureAppBuilder = null + ) { var services = builder.Services; @@ -56,20 +59,22 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, services.AddSingleton, SentryMauiOptionsSetup>(); services.AddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); // Resolve the configured options and register any element event binders from these - IServiceProvider serviceProvider = services.BuildServiceProvider(); - var options = serviceProvider.GetRequiredService>().Value; - services.TryAddSingleton(options); // Ensure this doesn't get resolved again in AddSentry - foreach (var eventBinder in options.DefaultEventBinders) - { - eventBinder.Register(services); - } // This is ultimately the class that enables all the MauiElementEventBinders above services.TryAddSingleton(); - services.AddSentry(); + if (configureAppBuilder != null) + { + var builder = new SentryMauiAppBuilder(services); + configureAppBuilder.Invoke(builder); + } + builder.RegisterMauiEventsBinder(); return builder; diff --git a/src/Sentry.Maui/SentryMauiOptions.cs b/src/Sentry.Maui/SentryMauiOptions.cs index f152744ecf..dc9ac14d05 100644 --- a/src/Sentry.Maui/SentryMauiOptions.cs +++ b/src/Sentry.Maui/SentryMauiOptions.cs @@ -23,18 +23,6 @@ public SentryMauiOptions() #if !PLATFORM_NEUTRAL CacheDirectoryPath = Microsoft.Maui.Storage.FileSystem.CacheDirectory; #endif - AddDefaultEventBinder(); - AddDefaultEventBinder(); - AddDefaultEventBinder(); - AddDefaultEventBinder(); - } - - internal List DefaultEventBinders { get; } = []; - - internal void AddDefaultEventBinder<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TEventBinder>() - where TEventBinder : class, IMauiElementEventBinder - { - DefaultEventBinders.Add(new MauiElementEventBinderRegistration()); } /// From bd3f2f9e9107e51cf7cd598845f680309ec02e5b Mon Sep 17 00:00:00 2001 From: Allan Ritchie Date: Tue, 20 May 2025 13:28:05 -0400 Subject: [PATCH 2/2] Add back AOT attributes and docs --- src/Sentry.Maui/SentryMauiAppBuilder.cs | 9 ++++++--- src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Sentry.Maui/SentryMauiAppBuilder.cs b/src/Sentry.Maui/SentryMauiAppBuilder.cs index 2709547929..336f5f20d6 100644 --- a/src/Sentry.Maui/SentryMauiAppBuilder.cs +++ b/src/Sentry.Maui/SentryMauiAppBuilder.cs @@ -5,16 +5,19 @@ namespace Sentry.Maui; /// public class SentryMauiAppBuilder(IServiceCollection services) { + /// + /// Access the current service collection + /// public IServiceCollection Services => services; /// /// Configures the application by adding a binding for a Maui element of the specified implementation type. /// - /// The type of implementation for the Maui element binder to be added. + /// The type of implementation for the Maui element binder to be added. /// The current instance of to allow method chaining. - public SentryMauiAppBuilder AddMauiElementBinder() where TImpl : class, IMauiElementEventBinder + public SentryMauiAppBuilder AddMauiElementBinder<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TEventBinder>() where TEventBinder : class, IMauiElementEventBinder { - services.AddSingleton(); + Services.AddSingleton(); return this; } } diff --git a/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs b/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs index f5f01e665b..0cac862c22 100644 --- a/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs +++ b/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs @@ -39,6 +39,7 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, string dsn, /// /// The builder. /// An action to configure the options. + /// Optional parameter that allows you to configure and add additional sentry services like MAUI element event binders /// The . public static MauiAppBuilder UseSentry( this MauiAppBuilder builder, @@ -71,8 +72,8 @@ public static MauiAppBuilder UseSentry( if (configureAppBuilder != null) { - var builder = new SentryMauiAppBuilder(services); - configureAppBuilder.Invoke(builder); + var appBuilder = new SentryMauiAppBuilder(services); + configureAppBuilder.Invoke(appBuilder); } builder.RegisterMauiEventsBinder();