DocSpace-client/common/ASC.Common/DIHelper.cs

413 lines
13 KiB
C#
Raw Normal View History

2020-02-17 08:58:14 +00:00
using System;
using System.Collections.Generic;
2020-10-18 16:03:14 +00:00
using System.Linq;
using System.Reflection;
2020-07-17 10:52:28 +00:00
using ASC.Common.Threading.Progress;
using ASC.Common.Threading.Workers;
2020-07-17 10:52:28 +00:00
2020-02-17 08:58:14 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
2020-10-18 16:03:14 +00:00
using Microsoft.Extensions.Options;
2020-02-17 08:58:14 +00:00
namespace ASC.Common
{
2020-10-18 16:03:14 +00:00
public class ScopeAttribute : DIAttribute
{
public ScopeAttribute()
{
}
public ScopeAttribute(Type type) : base(type)
{
}
public ScopeAttribute(Type type, Type cachedType) : base(type, cachedType)
{
}
}
public class SingletoneAttribute : DIAttribute
{
public SingletoneAttribute()
{
}
public SingletoneAttribute(Type type) : base(type)
{
}
public SingletoneAttribute(Type type, Type cachedType) : base(type, cachedType)
{
}
}
public class DIAttribute : Attribute
{
public Type CachedType { get; }
public Type Type { get; }
public DIAttribute()
{
}
public DIAttribute(Type type)
{
Type = type;
}
public DIAttribute(Type type, Type cachedType)
{
CachedType = cachedType;
Type = type;
}
}
2020-02-17 08:58:14 +00:00
public class DIHelper
{
public List<string> Singleton { get; set; }
public List<string> Scoped { get; set; }
public List<string> Transient { get; set; }
2020-02-17 12:13:40 +00:00
public List<string> Configured { get; set; }
2020-10-18 16:34:30 +00:00
public IServiceCollection ServiceCollection { get; private set; }
2020-02-17 08:58:14 +00:00
2020-10-18 16:34:30 +00:00
public DIHelper()
2020-02-17 08:58:14 +00:00
{
Singleton = new List<string>();
Scoped = new List<string>();
Transient = new List<string>();
2020-02-17 12:13:40 +00:00
Configured = new List<string>();
2020-10-18 16:34:30 +00:00
}
public DIHelper(IServiceCollection serviceCollection)
{
ServiceCollection = serviceCollection;
}
public void Configure(IServiceCollection serviceCollection)
{
2020-02-17 08:58:14 +00:00
ServiceCollection = serviceCollection;
}
2020-07-17 10:52:28 +00:00
public bool TryAddScoped<TService>() where TService : class
2020-02-17 08:58:14 +00:00
{
var serviceName = $"{typeof(TService)}";
if (!Scoped.Contains(serviceName))
{
Scoped.Add(serviceName);
ServiceCollection.TryAddScoped<TService>();
2020-07-17 10:52:28 +00:00
return true;
2020-02-17 08:58:14 +00:00
}
2020-07-17 10:52:28 +00:00
return false;
2020-02-17 08:58:14 +00:00
}
2020-10-18 16:03:14 +00:00
public bool TryAdd<TService>() where TService : class
{
return TryAdd(typeof(TService));
}
public bool TryAdd(Type service, Type implementation = null)
{
2020-10-19 07:19:32 +00:00
var di = service.IsGenericType && service.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) && implementation != null ? implementation.GetCustomAttribute<DIAttribute>() : service.GetCustomAttribute<DIAttribute>();
2020-10-18 16:03:14 +00:00
var isnew = false;
if (di != null)
{
2020-10-19 07:19:32 +00:00
if (!service.IsInterface || implementation != null)
2020-10-18 16:03:14 +00:00
{
2020-10-19 07:19:32 +00:00
isnew = implementation != null ? Register(service, implementation) : Register(service);
}
if (service.IsInterface && implementation == null || !service.IsInterface)
{
if (di.Type != null)
2020-10-18 16:03:14 +00:00
{
2020-10-19 07:19:32 +00:00
var a = di.Type.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IConfigureOptions<>));
if (a != null)
{
var b = a.GetGenericArguments();
foreach (var g in b)
{
TryAdd(g);
}
2020-10-18 16:03:14 +00:00
2020-10-19 07:19:32 +00:00
TryAdd(a, di.Type);
}
else
2020-10-18 16:03:14 +00:00
{
2020-10-19 07:19:32 +00:00
Register(service, di.Type);
2020-10-18 16:03:14 +00:00
}
}
if (di.CachedType != null)
{
2020-10-19 07:19:32 +00:00
var a = di.CachedType.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IConfigureOptions<>));
2020-10-18 16:03:14 +00:00
if (a != null)
{
var b = a.GetGenericArguments();
foreach (var g in b)
{
TryAdd(service, g);
}
TryAdd(a, di.CachedType);
}
2020-10-19 07:19:32 +00:00
else
{
Register(service, di.CachedType);
}
2020-10-18 16:03:14 +00:00
}
}
}
if (isnew)
{
var props = service.GetConstructors();
foreach (var p in props)
{
var par = p.GetParameters();
foreach (var p1 in par)
{
TryAdd(p1.ParameterType);
}
}
}
return isnew;
}
private bool Register(Type service)
{
var c = service.GetCustomAttribute<DIAttribute>();
var serviceName = $"{service}";
if (c is ScopeAttribute)
{
if (!Scoped.Contains(serviceName))
{
Scoped.Add(serviceName);
ServiceCollection.TryAddScoped(service);
return true;
}
}
else if (c is SingletoneAttribute)
{
if (!Singleton.Contains(serviceName))
{
Singleton.Add(serviceName);
ServiceCollection.TryAddSingleton(service);
return true;
}
}
return false;
}
private bool Register(Type service, Type implementation)
{
2020-10-19 07:19:32 +00:00
var c = service.IsGenericType && service.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) && implementation != null ? implementation.GetCustomAttribute<DIAttribute>() : service.GetCustomAttribute<DIAttribute>();
2020-10-18 16:03:14 +00:00
var serviceName = $"{service}{implementation}";
if (c is ScopeAttribute)
{
if (!Scoped.Contains(serviceName))
{
Scoped.Add(serviceName);
ServiceCollection.TryAddScoped(service, implementation);
return true;
}
}
else if (c is SingletoneAttribute)
{
if (!Singleton.Contains(serviceName))
{
Singleton.Add(serviceName);
ServiceCollection.TryAddSingleton(service, implementation);
return true;
}
}
return false;
}
2020-10-08 08:36:47 +00:00
public bool TryAddScoped<TService, TImplementation>() where TService : class where TImplementation : class, TService
2020-02-17 08:58:14 +00:00
{
var serviceName = $"{typeof(TService)}{typeof(TImplementation)}";
if (!Scoped.Contains(serviceName))
{
Scoped.Add(serviceName);
ServiceCollection.TryAddScoped<TService, TImplementation>();
2020-10-08 08:36:47 +00:00
return true;
2020-02-17 08:58:14 +00:00
}
2020-10-08 08:36:47 +00:00
return false;
2020-02-17 08:58:14 +00:00
}
2020-10-06 07:06:18 +00:00
public bool TryAddScoped<TService, TImplementation>(TService tservice, TImplementation tImplementation) where TService : Type where TImplementation : Type
2020-02-17 08:58:14 +00:00
{
var serviceName = $"{tservice}{tImplementation}";
if (!Scoped.Contains(serviceName))
{
Scoped.Add(serviceName);
ServiceCollection.TryAddScoped(tservice, tImplementation);
2020-10-06 07:06:18 +00:00
return true;
2020-02-17 08:58:14 +00:00
}
2020-10-06 07:06:18 +00:00
return false;
2020-02-17 08:58:14 +00:00
}
2020-06-03 09:15:05 +00:00
2020-02-17 08:58:14 +00:00
public DIHelper TryAddSingleton<TService>() where TService : class
{
var serviceName = $"{typeof(TService)}";
if (!Singleton.Contains(serviceName))
{
Singleton.Add(serviceName);
ServiceCollection.TryAddSingleton<TService>();
}
return this;
}
public DIHelper TryAddSingleton<TService>(Func<IServiceProvider, TService> implementationFactory) where TService : class
{
var serviceName = $"{typeof(TService)}";
if (!Singleton.Contains(serviceName))
{
Singleton.Add(serviceName);
ServiceCollection.TryAddSingleton(implementationFactory);
}
return this;
}
public DIHelper TryAddSingleton<TService>(TService t) where TService : class
{
var serviceName = $"{typeof(TService)}";
if (!Singleton.Contains(serviceName))
{
Singleton.Add(serviceName);
ServiceCollection.TryAddSingleton(t);
}
return this;
}
public DIHelper TryAddSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
var serviceName = $"{typeof(TService)}{typeof(TImplementation)}";
if (!Singleton.Contains(serviceName))
{
Singleton.Add(serviceName);
ServiceCollection.TryAddSingleton<TService, TImplementation>();
}
return this;
}
public DIHelper AddSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
var serviceName = $"{typeof(TService)}{typeof(TImplementation)}";
if (!Singleton.Contains(serviceName))
{
Singleton.Add(serviceName);
2020-10-12 19:39:23 +00:00
ServiceCollection.AddSingleton<TService, TImplementation>();
2020-02-17 08:58:14 +00:00
}
return this;
}
public DIHelper TryAddSingleton<TService, TImplementation>(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<TService>() where TService : class
{
var serviceName = $"{typeof(TService)}";
if (!Transient.Contains(serviceName))
{
Transient.Add(serviceName);
ServiceCollection.TryAddTransient<TService>();
}
return this;
}
public DIHelper Configure<TOptions>(Action<TOptions> configureOptions) where TOptions : class
{
2020-02-17 12:13:40 +00:00
var serviceName = $"{typeof(TOptions)}";
if (!Configured.Contains(serviceName))
{
Configured.Add(serviceName);
2020-10-12 19:39:23 +00:00
ServiceCollection.Configure(configureOptions);
2020-02-17 12:13:40 +00:00
}
2020-02-17 08:58:14 +00:00
return this;
}
private void AddToConfigured<TOptions>(string type, Action<TOptions> action) where TOptions : class
{
if (!Configured.Contains(type))
{
Configured.Add(type);
2020-10-12 19:39:23 +00:00
ServiceCollection.Configure(action);
}
}
2020-07-17 10:52:28 +00:00
public DIHelper AddWorkerQueue<T1>(int workerCount, int waitInterval, bool stopAfterFinsih, int errorCount)
{
2020-10-12 13:52:31 +00:00
void action(WorkerQueue<T1> a)
{
a.workerCount = workerCount;
a.waitInterval = waitInterval;
a.stopAfterFinsih = stopAfterFinsih;
a.errorCount = errorCount;
2020-10-12 13:52:31 +00:00
}
AddToConfigured($"{typeof(WorkerQueue<T1>)}", (Action<WorkerQueue<T1>>)action);
return this;
}
public DIHelper AddProgressQueue<T1>(int workerCount, int waitInterval, bool removeAfterCompleted, bool stopAfterFinsih, int errorCount) where T1 : class, IProgressItem
{
2020-10-12 13:52:31 +00:00
void action(ProgressQueue<T1> a)
{
a.workerCount = workerCount;
a.waitInterval = waitInterval;
a.stopAfterFinsih = stopAfterFinsih;
a.errorCount = errorCount;
a.removeAfterCompleted = removeAfterCompleted;
2020-10-12 13:52:31 +00:00
}
AddToConfigured($"{typeof(ProgressQueue<T1>)}", (Action<ProgressQueue<T1>>)action);
return this;
}
2020-02-17 08:58:14 +00:00
public DIHelper Configure<TOptions>(string name, Action<TOptions> configureOptions) where TOptions : class
{
2020-02-17 12:13:40 +00:00
var serviceName = $"{typeof(TOptions)}{name}";
if (!Configured.Contains(serviceName))
{
Configured.Add(serviceName);
2020-10-12 19:39:23 +00:00
ServiceCollection.Configure(name, configureOptions);
2020-02-17 12:13:40 +00:00
}
2020-02-17 08:58:14 +00:00
return this;
}
}
}