Refactoring: scope

This commit is contained in:
pavelbannov 2020-10-20 23:10:17 +03:00
parent bff9629389
commit 1d2f65ba12
21 changed files with 122 additions and 138 deletions

View File

@ -12,11 +12,6 @@ using Microsoft.Extensions.Options;
namespace ASC.Common
{
public interface IAdditionalDI
{
void Register(DIHelper services);
}
public class TransientAttribute : DIAttribute
{
public TransientAttribute()
@ -88,6 +83,7 @@ namespace ASC.Common
public class DIHelper
{
public List<string> Added { get; set; }
public List<string> Singleton { get; set; }
public List<string> Scoped { get; set; }
public List<string> Transient { get; set; }
@ -96,6 +92,7 @@ namespace ASC.Common
public DIHelper()
{
Added = new List<string>();
Singleton = new List<string>();
Scoped = new List<string>();
Transient = new List<string>();
@ -129,13 +126,28 @@ namespace ASC.Common
return TryAdd(typeof(TService));
}
public bool TryAdd<TService, TImplementation>() where TService : class
{
return TryAdd(typeof(TService), typeof(TImplementation));
}
public bool TryAdd(Type service, Type implementation = null)
{
var serviceName = $"{service}{implementation}";
if (Added.Contains(serviceName)) return false;
Added.Add(serviceName);
var di = service.IsGenericType && service.GetGenericTypeDefinition() == typeof(IConfigureOptions<>) && implementation != null ? implementation.GetCustomAttribute<DIAttribute>() : service.GetCustomAttribute<DIAttribute>();
var isnew = false;
if (di != null)
{
if (di.Additional != null)
{
var m = di.Additional.GetMethod("Register", BindingFlags.Public | BindingFlags.Static);
m.Invoke(null, new[] { this });
}
if (!service.IsInterface || implementation != null)
{
isnew = implementation != null ? Register(service, implementation) : Register(service);
@ -169,7 +181,20 @@ namespace ASC.Common
}
else
{
TryAdd(a.GetGenericTypeDefinition().MakeGenericType(service.GetGenericArguments()), di.Service.MakeGenericType(service.GetGenericArguments()));
Type c = null;
var a1 = a.GetGenericTypeDefinition();
var b = a.GetGenericArguments().FirstOrDefault();
if (b != null && b.IsGenericType)
{
c = a1.MakeGenericType(b.GetGenericTypeDefinition().MakeGenericType(service.GetGenericArguments()));
}
else
{
c = a1.MakeGenericType(service.GetGenericArguments());
}
TryAdd(c, di.Service.MakeGenericType(service.GetGenericArguments()));
//a, di.Service
}
}
@ -206,12 +231,6 @@ namespace ASC.Common
}
}
}
if (di.Additional != null)
{
var m = di.Additional.GetMethod("Register", BindingFlags.Public | BindingFlags.Static);
m.Invoke(null, new[] { this });
}
}
if (isnew)
@ -233,14 +252,10 @@ namespace ASC.Common
if (props != null)
{
foreach (var p in props)
var par = props.SelectMany(r => r.GetParameters()).Distinct();
foreach (var p1 in par)
{
var par = p.GetParameters();
foreach (var p1 in par)
{
TryAdd(p1.ParameterType);
}
TryAdd(p1.ParameterType);
}
}
}

View File

@ -42,7 +42,8 @@ namespace ASC.Common.Threading.Workers
{
}
}
[Singletone]
public class ConfigureWorkerQueue<T> : IConfigureOptions<WorkerQueue<T>>
{
public ConfigureWorkerQueue(IOptionsMonitor<ILog> log)

View File

@ -103,7 +103,8 @@ namespace ASC.Core
hostedSolution.CoreSettings = CoreSettings.Get(name);
}
}
[Scope(typeof(ConfigureHostedSolution))]
public class HostedSolution
{
internal ITenantService TenantService { get; set; }
@ -283,18 +284,5 @@ namespace ASC.Core
}
return tenant;
}
}
public static class HostedSolutionExtension
{
public static DIHelper AddHostedSolutionService(this DIHelper services)
{
if (services.TryAddScoped<IConfigureOptions<HostedSolution>, ConfigureHostedSolution>())
{
return services;
}
return services;
}
}
}

View File

@ -90,7 +90,7 @@ namespace ASC.Web.Core.Users
{
try
{
var resourceType = Type.GetType("Resources.Resource, ASC.Web.Studio");
var resourceType = Type.GetType("ASC.Web.Core.PublicResources.Resources.Resource, ASC.Web.Core");
var resourceProperty = resourceType.GetProperty("ProfileRemoved", BindingFlags.Static | BindingFlags.Public);
var resourceValue = (string)resourceProperty.GetValue(null);

View File

@ -32,7 +32,7 @@ using Microsoft.Extensions.DependencyInjection;
namespace ASC.Data.Storage.Encryption
{
[Singletone]
[Singletone(Additional = typeof(EncryptionFactoryExtension))]
public class EncryptionFactory
{
private IServiceProvider ServiceProvider { get; }
@ -53,5 +53,14 @@ namespace ASC.Data.Storage.Encryption
{
return ServiceProvider.GetService<Metadata>();
}
}
public class EncryptionFactoryExtension
{
public static void Register(DIHelper services)
{
services.TryAdd<Crypt>();
services.TryAdd<Metadata>();
}
}
}

View File

@ -30,7 +30,7 @@ using ASC.Files.Core.Security;
namespace ASC.Files.Core
{
[Scope(typeof(DaoFactory))]
[Scope(typeof(DaoFactory), Additional = typeof(DaoFactoryExtension))]
public interface IDaoFactory
{
IFolderDao<T> GetFolderDao<T>();

View File

@ -26,13 +26,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO;
using ASC.Common;
using ASC.Files.Core.Security;
using ASC.Web.Files.Services.DocumentService;
namespace ASC.Files.Core
{
{
[Scope]
public interface IFileDao<T>
{
/// <summary>

View File

@ -26,12 +26,14 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading;
using ASC.Common;
using ASC.Files.Core.Security;
namespace ASC.Files.Core
{
{
[Scope]
public interface IFolderDao<T>
{
/// <summary>

View File

@ -32,7 +32,7 @@ using ASC.Files.Thirdparty;
namespace ASC.Files.Core
{
[Scope(typeof(ProviderAccountDao))]
[Scope(typeof(ProviderAccountDao), Additional = typeof(ProviderAccountDaoExtension))]
public interface IProviderDao
{
IProviderInfo GetProviderInfo(int linkId);

View File

@ -25,10 +25,13 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using ASC.Common;
namespace ASC.Files.Core
{
{
[Scope]
public interface ITagDao<T>
{
IEnumerable<Tag> GetTags(Guid subject, TagType tagType, IEnumerable<FileEntry<T>> fileEntries);

View File

@ -28,7 +28,8 @@ using System;
using ASC.Common;
using ASC.Files.Core.Security;
using ASC.Files.Thirdparty.ProviderDao;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Core.Data
@ -53,7 +54,8 @@ namespace ASC.Files.Core.Data
public IFolderDao<T> GetFolderDao<T>()
{
return ServiceProvider.GetService<IFolderDao<T>>();
}
}
public ITagDao<T> GetTagDao<T>()
{
return ServiceProvider.GetService<ITagDao<T>>();
@ -63,5 +65,29 @@ namespace ASC.Files.Core.Data
{
return ServiceProvider.GetService<ISecurityDao<T>>();
}
}
public class DaoFactoryExtension
{
public static void Register(DIHelper services)
{
services.TryAdd<File<int>>();
services.TryAdd<IFileDao<int>, FileDao>();
services.TryAdd<File<string>>();
services.TryAdd<IFileDao<string>, ProviderFileDao>();
services.TryAdd<Folder<int>>();
services.TryAdd<IFolderDao<int>, FolderDao>();
services.TryAdd<Folder<string>>();
services.TryAdd<IFolderDao<string>, ProviderFolderDao>();
services.TryAdd<ISecurityDao<int>, SecurityDao<int>>();
services.TryAdd<ISecurityDao<string>, ProviderSecurityDao>();
services.TryAdd<ITagDao<int>, TagDao<int>>();
services.TryAdd<ITagDao<string>, ProviderTagDao>();
}
}
}

View File

@ -1436,15 +1436,5 @@ namespace ASC.Files.Core.Data
{
public DbFileQuery DbFileQuery { get; set; }
public DbFilesSecurity Security { get; set; }
}
public static class FileDaoExtention
{
public static DIHelper AddFileDaoService(this DIHelper services)
{
services.TryAddScoped<IFileDao<int>, FileDao>();
return services;
}
}
}

View File

@ -53,6 +53,7 @@ using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Core.Data
{
[Scope]
internal class FolderDao : AbstractDao, IFolderDao<int>
{
private const string my = "my";
@ -1279,14 +1280,5 @@ namespace ASC.Files.Core.Data
{
public DbFolderQuery DbFolderQuery { get; set; }
public DbFilesSecurity Security { get; set; }
}
public static class FolderDaoExtention
{
public static DIHelper AddFolderDaoService(this DIHelper services)
{
services.TryAddScoped<IFolderDao<int>, FolderDao>();
return services;
}
}
}

View File

@ -373,14 +373,4 @@ namespace ASC.Files.Core.Data
public DbFilesSecurity DbFilesSecurity { get; set; }
public DbFolderTree DbFolderTree { get; set; }
}
public static class SecurityDaoExtention
{
public static DIHelper AddSecurityDaoService(this DIHelper services)
{
services.TryAddScoped<ISecurityDao<int>, SecurityDao<int>>();
return services;
}
}
}

View File

@ -696,13 +696,4 @@ namespace ASC.Files.Core.Data
public DbFilesTagLink Link { get; set; }
}
public static class TagDaoExtention
{
public static DIHelper AddTagDaoService(this DIHelper services)
{
services.TryAddScoped<ITagDao<int>, TagDao<int>>();
return services;
}
}
}

View File

@ -27,8 +27,11 @@
using System;
using System.Collections.Generic;
using ASC.Common;
namespace ASC.Files.Core.Security
{
{
[Scope]
public interface ISecurityDao<T>
{
void SetShare(FileShareRecord r);

View File

@ -28,6 +28,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using ASC.Common;
using ASC.Common.Logging;
using ASC.Core;
using ASC.Core.Common.Configuration;
@ -70,7 +71,8 @@ namespace ASC.Files.Thirdparty
kDrive,
Yandex,
}
[Scope]
internal class ProviderAccountDao : IProviderDao
{
private int tenantID;
@ -581,5 +583,18 @@ namespace ASC.Files.Thirdparty
return token ?? "";
}
}
}
public class ProviderAccountDaoExtension
{
public static void Register(DIHelper services)
{
services.TryAdd<BoxProviderInfo>();
services.TryAdd<DropboxProviderInfo>();
services.TryAdd<SharePointProviderInfo>();
services.TryAdd<GoogleDriveProviderInfo>();
services.TryAdd<OneDriveProviderInfo>();
services.TryAdd<SharpBoxProviderInfo>();
}
}
}

View File

@ -38,7 +38,8 @@ using ASC.Files.Core.Thirdparty;
using ASC.Web.Files.Services.DocumentService;
namespace ASC.Files.Thirdparty.ProviderDao
{
{
[Scope]
internal class ProviderFileDao : ProviderDaoBase, IFileDao<string>
{
public ProviderFileDao(
@ -522,14 +523,4 @@ namespace ASC.Files.Thirdparty.ProviderDao
#endregion
}
public static class ProviderFileDaoExtention
{
public static DIHelper AddProviderFileDaoService(this DIHelper services)
{
services.TryAddScoped<IFileDao<string>, ProviderFileDao>();
return services;
}
}
}

View File

@ -446,14 +446,4 @@ filterType, subjectGroup, subjectID, searchText, searchSubfolders, checkShare);
#endregion
}
public static class ProviderFolderDaoExtention
{
public static DIHelper AddProviderFolderDaoService(this DIHelper services)
{
services.TryAddScoped<IFolderDao<string>, ProviderFolderDao>();
return services;
}
}
}

View File

@ -38,7 +38,8 @@ using ASC.Files.Core.Thirdparty;
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Files.Thirdparty.ProviderDao
{
{
[Scope]
internal class ProviderSecurityDao : ProviderDaoBase, ISecurityDao<string>
{
public ProviderSecurityDao(
@ -196,17 +197,4 @@ namespace ASC.Files.Thirdparty.ProviderDao
return SecurityDao.IsShared(entryId, type);
}
}
public static class ProviderSecurityDaoExtention
{
public static DIHelper AddProviderSecurityDaoService(this DIHelper services)
{
if (services.TryAddScoped<ISecurityDao<string>, ProviderSecurityDao>())
{
return services;
}
return services;
}
}
}

View File

@ -34,7 +34,8 @@ using ASC.Files.Core.Data;
using ASC.Files.Core.Thirdparty;
namespace ASC.Files.Thirdparty.ProviderDao
{
{
[Scope]
internal class ProviderTagDao : ProviderDaoBase, ITagDao<string>
{
public ProviderTagDao(
@ -130,17 +131,4 @@ namespace ASC.Files.Thirdparty.ProviderDao
#endregion
}
public static class ProviderTagDaoExtention
{
public static DIHelper AddProviderTagDaoService(this DIHelper services)
{
if (services.TryAddScoped<ITagDao<string>, ProviderTagDao>())
{
return services;
}
return services;
}
}
}