From 1d2f65ba12e26ab1064517dfd9f77c2975e138a1 Mon Sep 17 00:00:00 2001 From: pavelbannov Date: Tue, 20 Oct 2020 23:10:17 +0300 Subject: [PATCH] Refactoring: scope --- common/ASC.Common/DIHelper.cs | 53 ++++++++++++------- .../Threading/Workers/WorkerQueue.cs | 3 +- common/ASC.Core.Common/HostedSolution.cs | 16 +----- .../Users/DisplayUserSettings.cs | 2 +- .../Encryption/EncryptionFactory.cs | 11 +++- .../Core/Core/Dao/Interfaces/IDaoFactory.cs | 2 +- .../Core/Core/Dao/Interfaces/IFileDao.cs | 8 +-- .../Core/Core/Dao/Interfaces/IFolderDao.cs | 8 +-- .../Core/Core/Dao/Interfaces/IProviderDao.cs | 2 +- .../Core/Core/Dao/Interfaces/ITagDao.cs | 9 ++-- .../Core/Core/Dao/TeamlabDao/DaoFactory.cs | 30 ++++++++++- .../Core/Core/Dao/TeamlabDao/FileDao.cs | 10 ---- .../Core/Core/Dao/TeamlabDao/FolderDao.cs | 10 +--- .../Core/Core/Dao/TeamlabDao/SecurityDao.cs | 10 ---- .../Core/Core/Dao/TeamlabDao/TagDao.cs | 9 ---- .../Core/Core/Security/ISecurityDao.cs | 5 +- .../Core/Thirdparty/ProviderAccountDao.cs | 17 +++++- .../Thirdparty/ProviderDao/ProviderFileDao.cs | 13 +---- .../ProviderDao/ProviderFolderDao.cs | 10 ---- .../ProviderDao/ProviderSecutiryDao.cs | 16 +----- .../Thirdparty/ProviderDao/ProviderTagDao.cs | 16 +----- 21 files changed, 122 insertions(+), 138 deletions(-) diff --git a/common/ASC.Common/DIHelper.cs b/common/ASC.Common/DIHelper.cs index 80c3de5792..9f09edb52f 100644 --- a/common/ASC.Common/DIHelper.cs +++ b/common/ASC.Common/DIHelper.cs @@ -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 Added { get; set; } public List Singleton { get; set; } public List Scoped { get; set; } public List Transient { get; set; } @@ -96,6 +92,7 @@ namespace ASC.Common public DIHelper() { + Added = new List(); Singleton = new List(); Scoped = new List(); Transient = new List(); @@ -129,13 +126,28 @@ namespace ASC.Common return TryAdd(typeof(TService)); } + public bool TryAdd() 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() : service.GetCustomAttribute(); 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); } } } diff --git a/common/ASC.Common/Threading/Workers/WorkerQueue.cs b/common/ASC.Common/Threading/Workers/WorkerQueue.cs index 44d2241470..c7dfc71750 100644 --- a/common/ASC.Common/Threading/Workers/WorkerQueue.cs +++ b/common/ASC.Common/Threading/Workers/WorkerQueue.cs @@ -42,7 +42,8 @@ namespace ASC.Common.Threading.Workers { } } - + + [Singletone] public class ConfigureWorkerQueue : IConfigureOptions> { public ConfigureWorkerQueue(IOptionsMonitor log) diff --git a/common/ASC.Core.Common/HostedSolution.cs b/common/ASC.Core.Common/HostedSolution.cs index 2d0c210d8d..57c662712a 100644 --- a/common/ASC.Core.Common/HostedSolution.cs +++ b/common/ASC.Core.Common/HostedSolution.cs @@ -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, ConfigureHostedSolution>()) - { - return services; - } - - return services; - } } } \ No newline at end of file diff --git a/common/ASC.Core.Common/Users/DisplayUserSettings.cs b/common/ASC.Core.Common/Users/DisplayUserSettings.cs index 987116fe18..3824883d51 100644 --- a/common/ASC.Core.Common/Users/DisplayUserSettings.cs +++ b/common/ASC.Core.Common/Users/DisplayUserSettings.cs @@ -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); diff --git a/common/ASC.Data.Storage/Encryption/EncryptionFactory.cs b/common/ASC.Data.Storage/Encryption/EncryptionFactory.cs index 5fa4ced3b0..3e382460ac 100644 --- a/common/ASC.Data.Storage/Encryption/EncryptionFactory.cs +++ b/common/ASC.Data.Storage/Encryption/EncryptionFactory.cs @@ -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(); } + } + + public class EncryptionFactoryExtension + { + public static void Register(DIHelper services) + { + services.TryAdd(); + services.TryAdd(); + } } } diff --git a/products/ASC.Files/Core/Core/Dao/Interfaces/IDaoFactory.cs b/products/ASC.Files/Core/Core/Dao/Interfaces/IDaoFactory.cs index 23f74532f4..de630293e2 100644 --- a/products/ASC.Files/Core/Core/Dao/Interfaces/IDaoFactory.cs +++ b/products/ASC.Files/Core/Core/Dao/Interfaces/IDaoFactory.cs @@ -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 GetFolderDao(); diff --git a/products/ASC.Files/Core/Core/Dao/Interfaces/IFileDao.cs b/products/ASC.Files/Core/Core/Dao/Interfaces/IFileDao.cs index bf66e5a99a..4052d296b9 100644 --- a/products/ASC.Files/Core/Core/Dao/Interfaces/IFileDao.cs +++ b/products/ASC.Files/Core/Core/Dao/Interfaces/IFileDao.cs @@ -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 { /// diff --git a/products/ASC.Files/Core/Core/Dao/Interfaces/IFolderDao.cs b/products/ASC.Files/Core/Core/Dao/Interfaces/IFolderDao.cs index 91433800a3..3c2f491199 100644 --- a/products/ASC.Files/Core/Core/Dao/Interfaces/IFolderDao.cs +++ b/products/ASC.Files/Core/Core/Dao/Interfaces/IFolderDao.cs @@ -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 { /// diff --git a/products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs b/products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs index 2a2786bb6f..008687347c 100644 --- a/products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs +++ b/products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs @@ -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); diff --git a/products/ASC.Files/Core/Core/Dao/Interfaces/ITagDao.cs b/products/ASC.Files/Core/Core/Dao/Interfaces/ITagDao.cs index 8028aac522..fb9dd11006 100644 --- a/products/ASC.Files/Core/Core/Dao/Interfaces/ITagDao.cs +++ b/products/ASC.Files/Core/Core/Dao/Interfaces/ITagDao.cs @@ -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 { IEnumerable GetTags(Guid subject, TagType tagType, IEnumerable> fileEntries); diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/DaoFactory.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/DaoFactory.cs index 8681758748..402b8afa44 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/DaoFactory.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/DaoFactory.cs @@ -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 GetFolderDao() { return ServiceProvider.GetService>(); - } + } + public ITagDao GetTagDao() { return ServiceProvider.GetService>(); @@ -63,5 +65,29 @@ namespace ASC.Files.Core.Data { return ServiceProvider.GetService>(); } + } + + public class DaoFactoryExtension + { + public static void Register(DIHelper services) + { + services.TryAdd>(); + services.TryAdd, FileDao>(); + + services.TryAdd>(); + services.TryAdd, ProviderFileDao>(); + + services.TryAdd>(); + services.TryAdd, FolderDao>(); + + services.TryAdd>(); + services.TryAdd, ProviderFolderDao>(); + + services.TryAdd, SecurityDao>(); + services.TryAdd, ProviderSecurityDao>(); + + services.TryAdd, TagDao>(); + services.TryAdd, ProviderTagDao>(); + } } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs index 44e7103721..83513647d0 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs @@ -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, FileDao>(); - - return services; - } } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs index 3954095189..7c21278e73 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs @@ -53,6 +53,7 @@ using Microsoft.Extensions.DependencyInjection; namespace ASC.Files.Core.Data { + [Scope] internal class FolderDao : AbstractDao, IFolderDao { 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, FolderDao>(); - return services; - } } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs index e0482cee04..34e2804244 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs @@ -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, SecurityDao>(); - - return services; - } - } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs index 2f4f3418cf..f3a0527ac3 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs @@ -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, TagDao>(); - - return services; - } - } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Security/ISecurityDao.cs b/products/ASC.Files/Core/Core/Security/ISecurityDao.cs index bdbba675a0..6d74bbdd7f 100644 --- a/products/ASC.Files/Core/Core/Security/ISecurityDao.cs +++ b/products/ASC.Files/Core/Core/Security/ISecurityDao.cs @@ -27,8 +27,11 @@ using System; using System.Collections.Generic; +using ASC.Common; + namespace ASC.Files.Core.Security -{ +{ + [Scope] public interface ISecurityDao { void SetShare(FileShareRecord r); diff --git a/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs b/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs index bd6a2283ec..bccd76ce06 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs @@ -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(); + services.TryAdd(); + services.TryAdd(); + services.TryAdd(); + services.TryAdd(); + services.TryAdd(); + } } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFileDao.cs b/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFileDao.cs index 0393c7eea3..15aa87f6db 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFileDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFileDao.cs @@ -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 { 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, ProviderFileDao>(); - - return services; - } - } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFolderDao.cs b/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFolderDao.cs index 9ff0768ead..fa18a26062 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFolderDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFolderDao.cs @@ -446,14 +446,4 @@ filterType, subjectGroup, subjectID, searchText, searchSubfolders, checkShare); #endregion } - - public static class ProviderFolderDaoExtention - { - public static DIHelper AddProviderFolderDaoService(this DIHelper services) - { - services.TryAddScoped, ProviderFolderDao>(); - - return services; - } - } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs b/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs index d6d6ce8532..e0ab914871 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs @@ -38,7 +38,8 @@ using ASC.Files.Core.Thirdparty; using Microsoft.Extensions.DependencyInjection; namespace ASC.Files.Thirdparty.ProviderDao -{ +{ + [Scope] internal class ProviderSecurityDao : ProviderDaoBase, ISecurityDao { 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, ProviderSecurityDao>()) - { - return services; - } - - return services; - } - } } \ No newline at end of file diff --git a/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderTagDao.cs b/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderTagDao.cs index 0c5a22157d..3f086aa41e 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderTagDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderTagDao.cs @@ -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 { 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, ProviderTagDao>()) - { - return services; - } - - return services; - } - } } \ No newline at end of file