using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.DependencyInjection; namespace StabilityMatrix.Avalonia.Services; public class ServiceManager { // Holds providers private readonly Dictionary> providers = new(); // Holds singleton instances private readonly Dictionary instances = new(); /// /// Register a new dialog view model (singleton instance) /// public ServiceManager Register(TService instance) where TService : T { if (instance is null) throw new ArgumentNullException(nameof(instance)); lock (instances) { if (instances.ContainsKey(typeof(TService)) || providers.ContainsKey(typeof(TService))) { throw new ArgumentException( $"Service of type {typeof(TService)} is already registered for {typeof(T)}"); } instances[instance.GetType()] = instance; } return this; } /// /// Register a new dialog view model provider action (called on each dialog creation) /// public ServiceManager Register(Func provider) where TService : T { lock (providers) { if (instances.ContainsKey(typeof(TService)) || providers.ContainsKey(typeof(TService))) { throw new ArgumentException( $"Service of type {typeof(TService)} is already registered for {typeof(T)}"); } // Return type is wrong during build with method group syntax // ReSharper disable once RedundantCast providers[typeof(TService)] = () => (TService) provider(); } return this; } /// /// Register a new dialog view model instance using a service provider /// Equal to Register[TService](serviceProvider.GetRequiredService[TService]) /// public ServiceManager RegisterProvider(IServiceProvider provider) where TService : notnull, T { lock (providers) { if (instances.ContainsKey(typeof(TService)) || providers.ContainsKey(typeof(TService))) { throw new ArgumentException( $"Service of type {typeof(TService)} is already registered for {typeof(T)}"); } // Return type is wrong during build with method group syntax // ReSharper disable once RedundantCast providers[typeof(TService)] = () => (TService) provider.GetRequiredService(); } return this; } /// /// Get a service instance /// [SuppressMessage("ReSharper", "InconsistentlySynchronizedField")] public T Get(Type type) { if (instances.TryGetValue(type, out var instance)) { if (instance is null) { throw new ArgumentException( $"Service of type {type} was registered as null"); } return instance; } if (providers.TryGetValue(type, out var provider)) { if (provider is null) { throw new ArgumentException( $"Service of type {type} was registered as null"); } var result = provider(); if (result is null) { throw new ArgumentException( $"Service provider for type {type} returned null"); } return result; } throw new ArgumentException( $"Service of type {type} is not registered in ServiceManager for {typeof(T)}"); } /// /// Get a view model instance /// [SuppressMessage("ReSharper", "InconsistentlySynchronizedField")] public TService Get() where TService : T { if (instances.TryGetValue(typeof(TService), out var instance)) { if (instance is null) { throw new ArgumentException( $"Service of type {typeof(TService)} was registered as null"); } return (TService) instance; } if (providers.TryGetValue(typeof(TService), out var provider)) { if (provider is null) { throw new ArgumentException( $"Service of type {typeof(TService)} was registered as null"); } var result = provider(); if (result is null) { throw new ArgumentException( $"Service provider for type {typeof(TService)} returned null"); } return (TService) result; } throw new ArgumentException( $"Service of type {typeof(TService)} is not registered for {typeof(T)}"); } /// /// Get a view model instance with an initializer parameter /// public TService Get(Func initializer) where TService : T { var instance = Get(); return initializer(instance); } /// /// Get a view model instance with an initializer for a mutable instance /// public TService Get(Action initializer) where TService : T { var instance = Get(); initializer(instance); return instance; } }