Refactoring: lazy

This commit is contained in:
pavelbannov 2020-10-18 22:00:38 +03:00
parent 181ac04fd8
commit af1bf4a56e
7 changed files with 33 additions and 23 deletions

View File

@ -8,11 +8,15 @@ namespace ASC.Common.Logging
{
public class EFLoggerFactory : ILoggerFactory
{
Dictionary<string, ILogger> Loggers { get; set; }
Lazy<ILogger> Logger { get; set; }
ILoggerProvider LoggerProvider { get; set; }
public EFLoggerFactory(EFLoggerProvider loggerProvider)
{
LoggerProvider = loggerProvider;
Loggers = new Dictionary<string, ILogger>();
Logger = new Lazy<ILogger>(() => LoggerProvider.CreateLogger(""));
}
public void AddProvider(ILoggerProvider provider)
@ -22,7 +26,7 @@ namespace ASC.Common.Logging
public ILogger CreateLogger(string categoryName)
{
return LoggerProvider.CreateLogger(categoryName);
return Logger.Value;
}
public void Dispose()

View File

@ -112,7 +112,7 @@ namespace ASC.Core.Billing
Configure(options);
options.QuotaService = QuotaService.Get(name);
options.TenantService = TenantService.Get(name);
options.CoreDbContext = CoreDbContextManager.Get(name);
options.LazyCoreDbContext = new Lazy<CoreDbContext>(() => CoreDbContextManager.Get(name));
}
public void Configure(TariffService options)
@ -131,7 +131,7 @@ namespace ASC.Core.Billing
options.QuotaService = QuotaService.Value;
options.TenantService = TenantService.Value;
options.CoreDbContext = CoreDbContextManager.Value;
options.LazyCoreDbContext = new Lazy<CoreDbContext>(() => CoreDbContextManager.Value);
}
}
@ -155,7 +155,8 @@ namespace ASC.Core.Billing
internal CoreBaseSettings CoreBaseSettings { get; set; }
internal CoreSettings CoreSettings { get; set; }
internal IConfiguration Configuration { get; set; }
internal CoreDbContext CoreDbContext { get; set; }
internal CoreDbContext CoreDbContext { get => LazyCoreDbContext.Value; }
internal Lazy<CoreDbContext> LazyCoreDbContext { get; set; }
internal TariffServiceStorage TariffServiceStorage { get; set; }
internal IOptionsMonitor<ILog> Options { get; set; }
@ -191,7 +192,7 @@ namespace ASC.Core.Billing
Cache = TariffServiceStorage.Cache;
Notify = TariffServiceStorage.Notify;
CoreDbContext = coreDbContextManager.Value;
LazyCoreDbContext = new Lazy<CoreDbContext>(() => coreDbContextManager.Value);
}
public Tariff GetTariff(int tenantId, bool withRequestToPaymentSystem = true)

View File

@ -38,11 +38,12 @@ namespace ASC.Core.Data
{
public Expression<Func<Acl, AzRecord>> FromAclToAzRecord { get; set; }
private CoreDbContext CoreDbContext { get; set; }
private CoreDbContext CoreDbContext { get => LazyCoreDbContext.Value; }
private Lazy<CoreDbContext> LazyCoreDbContext { get; set; }
public DbAzService(DbContextManager<CoreDbContext> dbContextManager)
{
CoreDbContext = dbContextManager.Value;
LazyCoreDbContext = new Lazy<CoreDbContext>(() => dbContextManager.Value);
FromAclToAzRecord = r => new AzRecord
{
ActionId = r.Action,

View File

@ -48,12 +48,12 @@ namespace ASC.Core.Data
public void Configure(string name, DbQuotaService options)
{
options.CoreDbContext = DbContextManager.Get(name);
options.LazyCoreDbContext = new Lazy<CoreDbContext>(() => DbContextManager.Get(name));
}
public void Configure(DbQuotaService options)
{
options.CoreDbContext = DbContextManager.Value;
options.LazyCoreDbContext = new Lazy<CoreDbContext>(() => DbContextManager.Value);
}
}
@ -61,7 +61,8 @@ namespace ASC.Core.Data
{
private Expression<Func<DbQuota, TenantQuota>> FromDbQuotaToTenantQuota { get; set; }
private Expression<Func<DbQuotaRow, TenantQuotaRow>> FromDbQuotaRowToTenantQuotaRow { get; set; }
internal CoreDbContext CoreDbContext { get; set; }
internal CoreDbContext CoreDbContext { get => LazyCoreDbContext.Value; }
internal Lazy<CoreDbContext> LazyCoreDbContext { get; set; }
public DbQuotaService()
{
@ -90,7 +91,7 @@ namespace ASC.Core.Data
public DbQuotaService(DbContextManager<CoreDbContext> dbContextManager) : this()
{
CoreDbContext = dbContextManager.Value;
LazyCoreDbContext = new Lazy<CoreDbContext>(() => dbContextManager.Value);
}
public IEnumerable<TenantQuota> GetTenantQuotas()

View File

@ -91,7 +91,7 @@ namespace ASC.Core.Data
Configure(options);
options.TenantManager = TenantManager.Get(name);
options.WebstudioDbContext = DbContextManager.Get(name);
options.LazyWebstudioDbContext = new Lazy<WebstudioDbContext>(() => DbContextManager.Get(name));
}
public void Configure(DbSettingsManager options)
@ -102,7 +102,7 @@ namespace ASC.Core.Data
options.Log = ILog.CurrentValue;
options.TenantManager = TenantManager.Value;
options.WebstudioDbContext = DbContextManager.Value;
options.LazyWebstudioDbContext = new Lazy<WebstudioDbContext>(() => DbContextManager.Value);
}
}
@ -116,7 +116,8 @@ namespace ASC.Core.Data
internal DbSettingsManagerCache DbSettingsManagerCache { get; set; }
internal AuthContext AuthContext { get; set; }
internal TenantManager TenantManager { get; set; }
internal WebstudioDbContext WebstudioDbContext { get; set; }
internal WebstudioDbContext WebstudioDbContext { get => LazyWebstudioDbContext.Value; }
internal Lazy<WebstudioDbContext> LazyWebstudioDbContext { get; set; }
public DbSettingsManager()
{
@ -137,7 +138,7 @@ namespace ASC.Core.Data
TenantManager = tenantManager;
Cache = dbSettingsManagerCache.Cache;
Log = option.CurrentValue;
WebstudioDbContext = dbContextManager.Value;
LazyWebstudioDbContext = new Lazy<WebstudioDbContext>(() => dbContextManager.Value);
}
private int tenantID;

View File

@ -58,13 +58,13 @@ namespace ASC.Core.Data
public void Configure(string name, DbTenantService options)
{
Configure(options);
options.TenantDbContext = DbContextManager.Get(name);
options.LazyTenantDbContext = new Lazy<TenantDbContext>(() => DbContextManager.Get(name));
}
public void Configure(DbTenantService options)
{
options.TenantDomainValidator = TenantDomainValidator;
options.TenantDbContext = DbContextManager.Value;
options.LazyTenantDbContext = new Lazy<TenantDbContext>(() => DbContextManager.Value);
}
}
@ -74,7 +74,8 @@ namespace ASC.Core.Data
internal TenantDomainValidator TenantDomainValidator { get; set; }
public MachinePseudoKeys MachinePseudoKeys { get; }
internal TenantDbContext TenantDbContext { get; set; }
internal TenantDbContext TenantDbContext { get => LazyTenantDbContext.Value; }
internal Lazy<TenantDbContext> LazyTenantDbContext { get; set; }
public Expression<Func<DbTenant, Tenant>> FromDbTenantToTenant { get; set; }
public Expression<Func<TenantUserSecurity, Tenant>> FromTenantUserToTenant { get; set; }
@ -117,7 +118,7 @@ namespace ASC.Core.Data
MachinePseudoKeys machinePseudoKeys)
: this()
{
TenantDbContext = dbContextManager.Value;
LazyTenantDbContext = new Lazy<TenantDbContext>(() => dbContextManager.Value);
TenantDomainValidator = tenantDomainValidator;
MachinePseudoKeys = machinePseudoKeys;
}

View File

@ -52,13 +52,13 @@ namespace ASC.Core.Data
public void Configure(string name, EFUserService options)
{
DbId = name;
options.UserDbContext = DbContextManager.Get(name);
options.LazyUserDbContext = new Lazy<UserDbContext>(() => DbContextManager.Get(name));
options.UserDbContextManager = DbContextManager;
}
public void Configure(EFUserService options)
{
options.UserDbContext = DbContextManager.Value;
options.LazyUserDbContext = new Lazy<UserDbContext>(() => DbContextManager.Value);
options.UserDbContextManager = DbContextManager;
}
}
@ -73,7 +73,8 @@ namespace ASC.Core.Data
public Expression<Func<UserGroup, UserGroupRef>> FromUserGroupToUserGroupRef { get; set; }
public Func<UserGroupRef, UserGroup> FromUserGroupRefToUserGroup { get; set; }
internal UserDbContext UserDbContext { get; set; }
internal UserDbContext UserDbContext { get => LazyUserDbContext.Value; }
internal Lazy<UserDbContext> LazyUserDbContext { get; set; }
internal DbContextManager<UserDbContext> UserDbContextManager { get; set; }
private PasswordHasher PasswordHasher { get; }
public MachinePseudoKeys MachinePseudoKeys { get; }
@ -189,7 +190,7 @@ namespace ASC.Core.Data
UserDbContextManager = userDbContextManager;
PasswordHasher = passwordHasher;
MachinePseudoKeys = machinePseudoKeys;
UserDbContext = UserDbContextManager.Value;
LazyUserDbContext = new Lazy<UserDbContext>(() => UserDbContextManager.Value);
}
public Group GetGroup(int tenant, Guid id)