Skip to content

Commit

Permalink
Merge pull request #303 from ONLYOFFICE/feature/context-optimize
Browse files Browse the repository at this point in the history
DbContext: optimization
  • Loading branch information
pavelbannov authored Jul 15, 2021
2 parents b184ad7 + 2d75554 commit 0b49e4b
Show file tree
Hide file tree
Showing 24 changed files with 97 additions and 71 deletions.
6 changes: 4 additions & 2 deletions common/ASC.Core.Common/Data/DbSubscriptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ class DbSubscriptionService : ISubscriptionService
{
private Expression<Func<Subscription, SubscriptionRecord>> FromSubscriptionToSubscriptionRecord { get; set; }
private Expression<Func<DbSubscriptionMethod, SubscriptionMethod>> FromDbSubscriptionMethodToSubscriptionMethod { get; set; }
private UserDbContext UserDbContext { get; set; }
private Lazy<UserDbContext> LazyUserDbContext { get; }
private UserDbContext UserDbContext { get => LazyUserDbContext.Value; }

public DbSubscriptionService(DbContextManager<UserDbContext> dbContextManager)
{
UserDbContext = dbContextManager.Value;
LazyUserDbContext = new Lazy<UserDbContext>(() => dbContextManager.Value);

FromSubscriptionToSubscriptionRecord = r => new SubscriptionRecord
{
Expand Down
5 changes: 3 additions & 2 deletions common/ASC.Feed/Data/FeedAggregateDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public class FeedAggregateDataProvider
private AuthContext AuthContext { get; }
private TenantManager TenantManager { get; }
private TenantUtil TenantUtil { get; }
private FeedDbContext FeedDbContext { get; }
private Lazy<FeedDbContext> LazyFeedDbContext { get; }
private FeedDbContext FeedDbContext { get => LazyFeedDbContext.Value; }

public FeedAggregateDataProvider(AuthContext authContext, TenantManager tenantManager, TenantUtil tenantUtil, DbContextManager<FeedDbContext> dbContextManager)
: this(authContext, tenantManager, tenantUtil)
{
FeedDbContext = dbContextManager.Get(Constants.FeedDbId);
LazyFeedDbContext = new Lazy<FeedDbContext>(() => dbContextManager.Get(Constants.FeedDbId));
}

public FeedAggregateDataProvider(AuthContext authContext, TenantManager tenantManager, TenantUtil tenantUtil)
Expand Down
5 changes: 3 additions & 2 deletions common/ASC.Feed/Data/FeedReadedDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ public class FeedReadedDataProvider

private AuthContext AuthContext { get; }
private TenantManager TenantManager { get; }
private FeedDbContext FeedDbContext { get; }
private Lazy<FeedDbContext> LazyFeedDbContext { get; }
private FeedDbContext FeedDbContext { get => LazyFeedDbContext.Value; }

public FeedReadedDataProvider(AuthContext authContext, TenantManager tenantManager, DbContextManager<FeedDbContext> dbContextManager)
{
AuthContext = authContext;
TenantManager = tenantManager;
FeedDbContext = dbContextManager.Get(dbId);
LazyFeedDbContext = new Lazy<FeedDbContext>(() => dbContextManager.Get(dbId));
}

public DateTime GetTimeReaded()
Expand Down
6 changes: 4 additions & 2 deletions common/ASC.IPSecurity/IPRestrictionsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/


using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -39,11 +40,12 @@ public class IPRestrictionsRepository
{
private const string dbId = "core";

private TenantDbContext TenantDbContext { get; }
private Lazy<TenantDbContext> LazyTenantDbContext { get; }
private TenantDbContext TenantDbContext { get => LazyTenantDbContext.Value; }

public IPRestrictionsRepository(DbContextManager<TenantDbContext> dbContextManager)
{
TenantDbContext = dbContextManager.Get(dbId);
LazyTenantDbContext = new Lazy<TenantDbContext>(() => dbContextManager.Get(dbId));
}

public List<IPRestriction> Get(int tenant)
Expand Down
5 changes: 3 additions & 2 deletions common/ASC.VoipService/Dao/AbstractDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ public class AbstractDao
{
private readonly string dbid = "default";

protected VoipDbContext VoipDbContext { get; set; }
private Lazy<VoipDbContext> LazyVoipDbContext { get; }
protected VoipDbContext VoipDbContext { get => LazyVoipDbContext.Value; }

protected AbstractDao(DbContextManager<VoipDbContext> dbOptions, TenantManager tenantManager)
{
VoipDbContext = dbOptions.Get(dbid);
LazyVoipDbContext = new Lazy<VoipDbContext>(() => dbOptions.Get(dbid));
TenantID = tenantManager.GetCurrentTenant().TenantId;
}

Expand Down
5 changes: 3 additions & 2 deletions common/services/ASC.AuditTrail/AuditEventsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public class AuditEventsRepository
{
private MessageTarget MessageTarget { get; set; }
private UserFormatter UserFormatter { get; set; }
private AuditTrailContext AuditTrailContext { get; }
private Lazy<AuditTrailContext> LazyAuditTrailContext { get; }
private AuditTrailContext AuditTrailContext { get => LazyAuditTrailContext.Value; }
private AuditActionMapper AuditActionMapper { get; }

public AuditEventsRepository(MessageTarget messageTarget, UserFormatter userFormatter, DbContextManager<AuditTrailContext> dbContextManager, AuditActionMapper auditActionMapper)
{
MessageTarget = messageTarget;
UserFormatter = userFormatter;
AuditTrailContext = dbContextManager.Value;
LazyAuditTrailContext = new Lazy<AuditTrailContext>(() => dbContextManager.Value );
AuditActionMapper = auditActionMapper;
}

Expand Down
7 changes: 4 additions & 3 deletions common/services/ASC.AuditTrail/LoginEventsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ namespace ASC.AuditTrail.Data
[Scope]
public class LoginEventsRepository
{
private UserFormatter UserFormatter { get; }
private AuditTrailContext AuditTrailContext { get; }
private UserFormatter UserFormatter { get; }
private Lazy<AuditTrailContext> LazyAuditTrailContext { get; }
private AuditTrailContext AuditTrailContext { get => LazyAuditTrailContext.Value; }
private AuditActionMapper AuditActionMapper { get; }

public LoginEventsRepository(UserFormatter userFormatter, DbContextManager<AuditTrailContext> dbContextManager, AuditActionMapper auditActionMapper)
{
UserFormatter = userFormatter;
AuditTrailContext = dbContextManager.Value;
LazyAuditTrailContext = new Lazy<AuditTrailContext>(() => dbContextManager.Value);
AuditActionMapper = auditActionMapper;
}

Expand Down
7 changes: 4 additions & 3 deletions common/services/ASC.Data.Backup/Storage/BackupRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ namespace ASC.Data.Backup.Storage
{
[Scope]
public class BackupRepository : IBackupRepository
{
private BackupsContext BackupContext { get; }
{
private Lazy<BackupsContext> LazyBackupsContext { get; }
private BackupsContext BackupContext { get => LazyBackupsContext.Value; }

public BackupRepository(DbContextManager<BackupsContext> backupContext)
{
BackupContext = backupContext.Value;
LazyBackupsContext = new Lazy<BackupsContext>(() => backupContext.Value);
}

public void SaveBackupRecord(BackupRecord backup)
Expand Down
5 changes: 3 additions & 2 deletions common/services/ASC.Data.Backup/Tasks/BackupPortalTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ public class BackupPortalTask : PortalTaskBase
private bool Dump { get; set; }
private TenantManager TenantManager { get; set; }
private TempStream TempStream { get; }
private BackupsContext BackupRecordContext { get; set; }
private Lazy<BackupsContext> LazyBackupsContext { get; }
private BackupsContext BackupRecordContext { get => LazyBackupsContext.Value; }

public BackupPortalTask(DbFactory dbFactory, DbContextManager<BackupsContext> dbContextManager, IOptionsMonitor<ILog> options, TenantManager tenantManager, CoreBaseSettings coreBaseSettings, StorageFactory storageFactory, StorageFactoryConfig storageFactoryConfig, ModuleProvider moduleProvider, TempStream tempStream)
: base(dbFactory, options, storageFactory, storageFactoryConfig, moduleProvider)
{
Dump = coreBaseSettings.Standalone;
TenantManager = tenantManager;
TempStream = tempStream;
BackupRecordContext = dbContextManager.Get(DbFactory.ConnectionStringSettings.ConnectionString);
LazyBackupsContext = new Lazy<BackupsContext>(() => dbContextManager.Get(DbFactory.ConnectionStringSettings.ConnectionString));
}

public void Init(int tenantId, string fromConfigPath, string toFilePath, int limit)
Expand Down
7 changes: 4 additions & 3 deletions common/services/ASC.ElasticSearch/Engine/BaseIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ public class BaseIndexer<T> where T : class, ISearchItem
private TenantManager TenantManager { get; }
private BaseIndexerHelper BaseIndexerHelper { get; }
private Settings Settings { get; }
private IServiceProvider ServiceProvider { get; }
private WebstudioDbContext WebstudioDbContext { get; }
private IServiceProvider ServiceProvider { get; }
private Lazy<WebstudioDbContext> LazyWebstudioDbContext { get; }
private WebstudioDbContext WebstudioDbContext { get => LazyWebstudioDbContext.Value; }

public BaseIndexer(
Client client,
Expand All @@ -110,7 +111,7 @@ public BaseIndexer(
BaseIndexerHelper = baseIndexerHelper;
Settings = settings;
ServiceProvider = serviceProvider;
WebstudioDbContext = dbContextManager.Value;
LazyWebstudioDbContext = new Lazy<WebstudioDbContext>(() => dbContextManager.Value);
}

internal void Index(T data, bool immediately = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
using ASC.Common;
using ASC.Common.Web;
using ASC.Core;
using ASC.Core.Common.EF;
using ASC.Core.Common.EF;
using ASC.CRM.Resources;
using ASC.Files.Core.EF;
using ASC.Web.Core;
Expand All @@ -44,16 +44,18 @@ namespace ASC.Web.CRM.Configuration
[Scope]
public class CrmSpaceUsageStatManager : SpaceUsageStatManager
{
private int _tenantId;
private FilesDbContext _filesDbContext;
private int _tenantId;
private Lazy<FilesDbContext> LazyFilesDbContext { get; }
private FilesDbContext _filesDbContext { get => LazyFilesDbContext.Value; }

private PathProvider _pathProvider;

public CrmSpaceUsageStatManager(DbContextManager<FilesDbContext> filesDbContext,
PathProvider pathProvider,
TenantManager tenantManager)
{
_pathProvider = pathProvider;
_filesDbContext = filesDbContext.Value;
LazyFilesDbContext = new Lazy<FilesDbContext>(() => filesDbContext.Value);
_tenantId = tenantManager.GetCurrentTenant().TenantId;
}

Expand Down
9 changes: 6 additions & 3 deletions products/ASC.CRM/Server/Core/Dao/AbstractDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ namespace ASC.CRM.Core.Dao
{
public class AbstractDao
{
protected readonly List<EntityType> _supportedEntityType = new List<EntityType>();
public CrmDbContext CrmDbContext { get; }
protected readonly List<EntityType> _supportedEntityType = new List<EntityType>();

private Lazy<CrmDbContext> LazyCrmDbContext { get; }
public CrmDbContext CrmDbContext { get => LazyCrmDbContext.Value; }

protected readonly SecurityContext _securityContext;
protected readonly ICache _cache;
protected ILog _logger;
Expand All @@ -66,7 +69,7 @@ IMapper mapper

_cache = ascCache;

CrmDbContext = dbContextManager.Get(CrmConstants.DatabaseId);
LazyCrmDbContext = new Lazy<CrmDbContext>(() => dbContextManager.Get(CrmConstants.DatabaseId));

TenantID = tenantManager.GetCurrentTenant().TenantId;
_securityContext = securityContext;
Expand Down
6 changes: 1 addition & 5 deletions products/ASC.CRM/Server/Core/Dao/ReportDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ public class ReportDao : AbstractDao
private CurrencyInfo _defaultCurrency;
private TenantUtil _tenantUtil;
private DaoFactory _daoFactory;
private UserDbContext _userDbContext;
private DisplayUserSettingsHelper _displayUserSettings;

#region Constructor

public ReportDao(DbContextManager<CrmDbContext> dbContextManager,
DbContextManager<UserDbContext> dbUserContextManager,
TenantManager tenantManager,
SecurityContext securityContext,
FilesIntegration filesIntegration,
Expand Down Expand Up @@ -112,8 +110,6 @@ public ReportDao(DbContextManager<CrmDbContext> dbContextManager,

var crmSettings = settingsManager.Load<CrmSettings>();

_userDbContext = dbUserContextManager.Get(CrmConstants.DatabaseId);

_defaultCurrency = currencyProvider.Get(crmSettings.DefaultCurrency);

_displayUserSettings = displayUserSettingsHelper;
Expand Down Expand Up @@ -313,7 +309,7 @@ public List<string> GetMissingRates(string defaultCurrency)

var file = _daoFactory.GetFileDao().SaveFile(document, stream);

SaveFile((int)file.ID, -1);
SaveFile(file.ID, -1);

result.Add(file);
}
Expand Down
6 changes: 4 additions & 2 deletions products/ASC.CRM/Server/Core/Dao/TaskDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ namespace ASC.CRM.Core.Dao
[Scope]
public class TaskDao : AbstractDao
{
private readonly UserDbContext _userDbContext;
private readonly FactoryIndexerTask _factoryIndexer;
private readonly TenantUtil _tenantUtil;
private readonly CrmSecurity _crmSecurity;

private Lazy<UserDbContext> LazyUserDbContext { get; }
private UserDbContext _userDbContext { get => LazyUserDbContext.Value; }

public TaskDao(DbContextManager<CrmDbContext> dbContextManager,
TenantManager tenantManager,
Expand All @@ -77,7 +79,7 @@ public TaskDao(DbContextManager<CrmDbContext> dbContextManager,
_crmSecurity = crmSecurity;
_tenantUtil = tenantUtil;
_factoryIndexer = factoryIndexer;
_userDbContext = userDbContext.Value;
LazyUserDbContext = new Lazy<UserDbContext>(() => userDbContext.Value);
_mapper = mapper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ namespace ASC.Web.Files
{
[Scope]
public class FilesSpaceUsageStatManager : SpaceUsageStatManager
{
private ASC.Files.Core.EF.FilesDbContext FilesDbContext { get; }
{
private Lazy<ASC.Files.Core.EF.FilesDbContext> LazyFilesDbContext { get; }
private ASC.Files.Core.EF.FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; }
private TenantManager TenantManager { get; }
private UserManager UserManager { get; }
private UserPhotoManager UserPhotoManager { get; }
Expand All @@ -64,7 +65,7 @@ public FilesSpaceUsageStatManager(
GlobalFolderHelper globalFolderHelper,
PathProvider pathProvider)
{
FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId);
LazyFilesDbContext = new Lazy<ASC.Files.Core.EF.FilesDbContext>(() => dbContextManager.Get(FileConstant.DatabaseId));
TenantManager = tenantManager;
UserManager = userManager;
UserPhotoManager = userPhotoManager;
Expand Down Expand Up @@ -126,15 +127,16 @@ public override List<UsageSpaceStatItem> GetStatData()

[Scope]
public class FilesUserSpaceUsage : IUserSpaceUsage
{
private ASC.Files.Core.EF.FilesDbContext FilesDbContext { get; }
{
private Lazy<ASC.Files.Core.EF.FilesDbContext> LazyFilesDbContext { get; }
private ASC.Files.Core.EF.FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; }
private TenantManager TenantManager { get; }

public FilesUserSpaceUsage(
DbContextManager<ASC.Files.Core.EF.FilesDbContext> dbContextManager,
TenantManager tenantManager)
{
FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId);
LazyFilesDbContext = new Lazy<ASC.Files.Core.EF.FilesDbContext>(() => dbContextManager.Get(FileConstant.DatabaseId));
TenantManager = tenantManager;
}

Expand Down
9 changes: 5 additions & 4 deletions products/ASC.Files/Core/Core/Dao/TeamlabDao/AbstractDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ namespace ASC.Files.Core.Data
{
public class AbstractDao
{
protected readonly ICache cache;

public FilesDbContext FilesDbContext { get; }
protected readonly ICache cache;

private Lazy<FilesDbContext> LazyFilesDbContext { get; }
public FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; }

private int tenantID;
protected internal int TenantID { get => tenantID != 0 ? tenantID : (tenantID = TenantManager.GetCurrentTenant().TenantId); }
Expand Down Expand Up @@ -83,7 +84,7 @@ protected AbstractDao(
ICache cache)
{
this.cache = cache;
FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId);
LazyFilesDbContext = new Lazy<FilesDbContext>(() => dbContextManager.Get(FileConstant.DatabaseId));
UserManager = userManager;
TenantManager = tenantManager;
TenantUtil = tenantUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ internal abstract class ThirdPartyProviderDao<T> : ThirdPartyProviderDao, IDispo
public int TenantID { get; private set; }
protected IServiceProvider ServiceProvider { get; }
protected UserManager UserManager { get; }
protected TenantUtil TenantUtil { get; }
protected FilesDbContext FilesDbContext { get; }
protected TenantUtil TenantUtil { get; }
private Lazy<FilesDbContext> LazyFilesDbContext { get; }
protected FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; }
protected SetupInfo SetupInfo { get; }
protected ILog Log { get; }
protected FileUtility FileUtility { get; }
Expand All @@ -237,7 +238,7 @@ public ThirdPartyProviderDao(
ServiceProvider = serviceProvider;
UserManager = userManager;
TenantUtil = tenantUtil;
FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId);
LazyFilesDbContext = new Lazy<FilesDbContext>(() => dbContextManager.Get(FileConstant.DatabaseId));
SetupInfo = setupInfo;
Log = monitor.CurrentValue;
FileUtility = fileUtility;
Expand Down
7 changes: 4 additions & 3 deletions products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public enum ProviderTypes
internal class ProviderAccountDao : IProviderDao
{
private int tenantID;
protected int TenantID { get => tenantID != 0 ? tenantID : (tenantID = TenantManager.GetCurrentTenant().TenantId); }
private FilesDbContext FilesDbContext { get; }
protected int TenantID { get => tenantID != 0 ? tenantID : (tenantID = TenantManager.GetCurrentTenant().TenantId); }
private Lazy<FilesDbContext> LazyFilesDbContext { get; }
private FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; }
public ILog Logger { get; }
private IServiceProvider ServiceProvider { get; }
private TenantUtil TenantUtil { get; }
Expand All @@ -102,7 +103,7 @@ public ProviderAccountDao(
DbContextManager<FilesDbContext> dbContextManager,
IOptionsMonitor<ILog> options)
{
FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId);
LazyFilesDbContext = new Lazy<FilesDbContext>(() => dbContextManager.Get(FileConstant.DatabaseId));
Logger = options.Get("ASC.Files");
ServiceProvider = serviceProvider;
TenantUtil = tenantUtil;
Expand Down
Loading

0 comments on commit 0b49e4b

Please sign in to comment.