| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ServiceCollectionExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 11 | | using MyNet.UI.Notifications.Processors; |
| | | 12 | | using MyNet.UI.Threading; |
| | | 13 | | |
| | | 14 | | #pragma warning disable IDE0130 |
| | | 15 | | namespace MyNet.UI.Notifications; |
| | | 16 | | #pragma warning restore IDE0130 |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Extension methods for registering notification services. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class ServiceCollectionExtensions |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Registers the built-in notification service and manager. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="services">Service collection.</param> |
| | | 27 | | /// <param name="configureProcessors">Optional callback to add processors applied before publication.</param> |
| | | 28 | | /// <returns>The same service collection for chaining.</returns> |
| | | 29 | | public static IServiceCollection AddNotifications( |
| | | 30 | | this IServiceCollection services, |
| | | 31 | | Action<IList<INotificationProcessor>>? configureProcessors = null) |
| | | 32 | | { |
| | 12 | 33 | | var processors = new List<INotificationProcessor>(); |
| | 12 | 34 | | configureProcessors?.Invoke(processors); |
| | | 35 | | |
| | 12 | 36 | | services.AddSchedulerProvider(); |
| | 12 | 37 | | services.TryAddSingleton<INotificationService>(_ => new NotificationService(processors)); |
| | 12 | 38 | | services.TryAddSingleton<INotificationPublisher>(x => x.GetRequiredService<INotificationService>()); |
| | 12 | 39 | | services.TryAddSingleton<INotificationsManager, NotificationsManager>(); |
| | | 40 | | |
| | 12 | 41 | | return services; |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |