using System; using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace ASC.Common { public class DIHelper { public List Singleton { get; set; } public List Scoped { get; set; } public List Transient { get; set; } public List Configured { get; set; } public IServiceCollection ServiceCollection { get; } public DIHelper(IServiceCollection serviceCollection) { Singleton = new List(); Scoped = new List(); Transient = new List(); Configured = new List(); ServiceCollection = serviceCollection; } public DIHelper TryAddScoped() where TService : class { var serviceName = $"{typeof(TService)}"; if (!Scoped.Contains(serviceName)) { Scoped.Add(serviceName); ServiceCollection.TryAddScoped(); } return this; } public DIHelper TryAddScoped() where TService : class where TImplementation : class, TService { var serviceName = $"{typeof(TService)}{typeof(TImplementation)}"; if (!Scoped.Contains(serviceName)) { Scoped.Add(serviceName); ServiceCollection.TryAddScoped(); } return this; } public DIHelper TryAddScoped(TService tservice, TImplementation tImplementation) where TService : Type where TImplementation : Type { var serviceName = $"{tservice}{tImplementation}"; if (!Scoped.Contains(serviceName)) { Scoped.Add(serviceName); ServiceCollection.TryAddScoped(tservice, tImplementation); } return this; } public DIHelper TryAddSingleton() where TService : class { var serviceName = $"{typeof(TService)}"; if (!Singleton.Contains(serviceName)) { Singleton.Add(serviceName); ServiceCollection.TryAddSingleton(); } return this; } public DIHelper TryAddSingleton(Func implementationFactory) where TService : class { var serviceName = $"{typeof(TService)}"; if (!Singleton.Contains(serviceName)) { Singleton.Add(serviceName); ServiceCollection.TryAddSingleton(implementationFactory); } return this; } public DIHelper TryAddSingleton(TService t) where TService : class { var serviceName = $"{typeof(TService)}"; if (!Singleton.Contains(serviceName)) { Singleton.Add(serviceName); ServiceCollection.TryAddSingleton(t); } return this; } public DIHelper TryAddSingleton() where TService : class where TImplementation : class, TService { var serviceName = $"{typeof(TService)}{typeof(TImplementation)}"; if (!Singleton.Contains(serviceName)) { Singleton.Add(serviceName); ServiceCollection.TryAddSingleton(); } return this; } public DIHelper AddSingleton() where TService : class where TImplementation : class, TService { var serviceName = $"{typeof(TService)}{typeof(TImplementation)}"; if (!Singleton.Contains(serviceName)) { Singleton.Add(serviceName); ServiceCollection.AddSingleton(); } return this; } public DIHelper TryAddSingleton(TService tservice, TImplementation tImplementation) where TService : Type where TImplementation : Type { var serviceName = $"{tservice}{tImplementation}"; if (!Singleton.Contains(serviceName)) { Singleton.Add(serviceName); ServiceCollection.TryAddSingleton(tservice, tImplementation); } return this; } public DIHelper TryAddTransient() where TService : class { var serviceName = $"{typeof(TService)}"; if (!Transient.Contains(serviceName)) { Transient.Add(serviceName); ServiceCollection.TryAddTransient(); } return this; } public DIHelper Configure(Action configureOptions) where TOptions : class { var serviceName = $"{typeof(TOptions)}"; if (!Configured.Contains(serviceName)) { Configured.Add(serviceName); ServiceCollection.Configure(configureOptions); } return this; } public DIHelper Configure(string name, Action configureOptions) where TOptions : class { var serviceName = $"{typeof(TOptions)}{name}"; if (!Configured.Contains(serviceName)) { Configured.Add(serviceName); ServiceCollection.Configure(name, configureOptions); } return this; } } }