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..336f5f20d6 --- /dev/null +++ b/src/Sentry.Maui/SentryMauiAppBuilder.cs @@ -0,0 +1,23 @@ +namespace Sentry.Maui; + +/// +/// A builder for configuring Sentry in a .NET MAUI application. +/// +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 current instance of to allow method chaining. + public SentryMauiAppBuilder AddMauiElementBinder<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TEventBinder>() where TEventBinder : class, IMauiElementEventBinder + { + Services.AddSingleton(); + return this; + } +} diff --git a/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs b/src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs index 83e1bd7740..0cac862c22 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,17 +31,21 @@ 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. /// /// 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, - Action? configureOptions) + public static MauiAppBuilder UseSentry( + this MauiAppBuilder builder, + Action? configureOptions, + Action? configureAppBuilder = null + ) { var services = builder.Services; @@ -56,20 +60,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 appBuilder = new SentryMauiAppBuilder(services); + configureAppBuilder.Invoke(appBuilder); + } + 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()); } ///