This allows us to create loosely coupled services with automatic dependency resolution
95 lines
4.4 KiB
C#
95 lines
4.4 KiB
C#
namespace Bts.DataStream.Services;
|
|
|
|
using BCG;
|
|
|
|
using bts.api.core;
|
|
|
|
using Bts.PubSub;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using System;
|
|
using System.Linq;
|
|
|
|
// public static class DataStreamPublisherServices {
|
|
// /// <summary>
|
|
// /// Adds a singleton service only if a service of type <typeparamref name="T"/> hasn't been added
|
|
// /// already.
|
|
// /// </summary>
|
|
// /// <typeparam name="T"></typeparam>
|
|
// /// <param name="services"></param>
|
|
// /// <returns></returns>
|
|
// public static IServiceCollection EnsureSingleton<T>(this IServiceCollection services) where T : class =>
|
|
// services.HasService<T>()
|
|
// ? services
|
|
// : services.AddSingleton<T>();
|
|
|
|
// /// <summary>
|
|
// /// Adds a singleton service only if a service of type <typeparamref name="T"/> hasn't been added
|
|
// /// already.
|
|
// /// </summary>
|
|
// /// <typeparam name="T"></typeparam>
|
|
// /// <param name="services"></param>
|
|
// /// <returns></returns>
|
|
// public static IServiceCollection EnsureSingleton<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory) where T : class =>
|
|
// services.HasService<T>()
|
|
// ? services
|
|
// : services.AddSingleton(implementationFactory);
|
|
|
|
// /// <summary>
|
|
// /// Adds a singleton service only if a service of type <typeparamref name="T"/> hasn't been added
|
|
// /// already.
|
|
// /// </summary>
|
|
// /// <typeparam name="T">The type of the service to add.</typeparam>
|
|
// /// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
|
|
// /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
|
|
// /// <returns>A reference to this instance after the operation has completed.</returns>
|
|
// public static IServiceCollection EnsureSingleton<T, TImplementation>(this IServiceCollection services) where T : class where TImplementation : class, T =>
|
|
// services.HasService<T>()
|
|
// ? services
|
|
// : services.AddSingleton<T, TImplementation>();
|
|
|
|
// /// <summary>
|
|
// /// Adds a singleton service only if a service of type <typeparamref name="T"/> hasn't been added
|
|
// /// already.
|
|
// /// </summary>
|
|
// /// <typeparam name="T">The type of the service to add.</typeparam>
|
|
// /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
|
|
// /// <returns>A reference to this instance after the operation has completed.</returns>
|
|
// public static IServiceCollection EnsureKeyedSingleton<T>(this IServiceCollection services, object? key) where T : class =>
|
|
// services.HasKeyedService<T>(key)
|
|
// ? services
|
|
// : services.AddKeyedSingleton<T>(key);
|
|
|
|
// /// <summary>
|
|
// /// Adds a singleton service only if a service of type <typeparamref name="T"/> hasn't been added
|
|
// /// already.
|
|
// /// </summary>
|
|
// /// <typeparam name="T">The type of the service to add.</typeparam>
|
|
// /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
|
|
// /// <returns>A reference to this instance after the operation has completed.</returns>
|
|
// public static IServiceCollection EnsureHostedService<T>(this IServiceCollection services) where T : class, IHostedService =>
|
|
// services.HasService<T>()
|
|
// ? services
|
|
// : services.AddHostedService<T>();
|
|
|
|
// /// <summary>
|
|
// /// Adds a singleton service only if a service of type <typeparamref name="T"/> hasn't been added
|
|
// /// already.
|
|
// /// </summary>
|
|
// /// <typeparam name="T">The type of the service to add.</typeparam>
|
|
// /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
|
|
// /// <returns>A reference to this instance after the operation has completed.</returns>
|
|
// public static IServiceCollection EnsureHostedService<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory) where T : class, IHostedService =>
|
|
// services.HasService<T>()
|
|
// ? services
|
|
// : services.AddHostedService(implementationFactory);
|
|
|
|
// public static bool HasService<T>(this IServiceCollection services) =>
|
|
// services.Any(s => s.ServiceType.IsAssignableTo(typeof(T)));
|
|
|
|
// public static bool HasKeyedService<T>(this IServiceCollection services, object? key) =>
|
|
// services.Any(s => Equals(s.ServiceKey, key) && s.ServiceType.IsAssignableTo(typeof(T)));
|
|
// }
|