using System; using System.Collections.Generic; using ASC.Common.Threading.Progress; using ASC.Common.Threading.Workers; 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 bool TryAddScoped() where TService : class { var serviceName = $"{typeof(TService)}"; if (!Scoped.Contains(serviceName)) { Scoped.Add(serviceName); ServiceCollection.TryAddScoped(); return true; } return false; } 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; } private void AddToConfigured(string type, Action action) where TOptions : class { if (!Configured.Contains(type)) { Configured.Add(type); ServiceCollection.Configure(action); } } public DIHelper AddWorkerQueue(int workerCount, int waitInterval, bool stopAfterFinsih, int errorCount) { Action> action = (a) => { a.workerCount = workerCount; a.waitInterval = waitInterval; a.stopAfterFinsih = stopAfterFinsih; a.errorCount = errorCount; }; AddToConfigured($"{typeof(WorkerQueue)}", action); return this; } public DIHelper AddProgressQueue(int workerCount, int waitInterval, bool removeAfterCompleted, bool stopAfterFinsih, int errorCount) where T1 : class, IProgressItem { Action> action = (a) => { a.workerCount = workerCount; a.waitInterval = waitInterval; a.stopAfterFinsih = stopAfterFinsih; a.errorCount = errorCount; a.removeAfterCompleted = removeAfterCompleted; }; AddToConfigured($"{typeof(ProgressQueue)}", action); 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; } } }