DocSpace-client/common/ASC.Core.Common/EF/Context/DbContextManager.cs

100 lines
3.2 KiB
C#
Raw Normal View History

2019-11-29 12:26:53 +00:00
using System;
using System.Collections.Generic;
2020-10-08 08:56:46 +00:00
2020-02-17 08:58:14 +00:00
using ASC.Common;
2020-10-08 08:56:46 +00:00
2020-10-07 12:20:06 +00:00
using Microsoft.Extensions.Configuration;
2019-11-29 12:26:53 +00:00
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF
{
2019-12-16 14:55:59 +00:00
public class BaseDbContextManager<T> : OptionsManager<T>, IDisposable where T : class, IDisposable, IAsyncDisposable, new()
2019-11-29 12:26:53 +00:00
{
private Dictionary<string, T> Pairs { get; set; }
2021-10-12 09:04:24 +00:00
private MigrationHistory MigrationHistory { get; set; }
2019-11-29 12:26:53 +00:00
private List<T> AsyncList { get; set; }
2020-10-08 08:56:46 +00:00
private IOptionsFactory<T> Factory { get; }
private IConfiguration Configuration { get; }
2021-10-12 09:04:24 +00:00
public BaseDbContextManager(IOptionsFactory<T> factory, IConfiguration configuration,
MigrationHistory migrationHistory) : base(factory)
2019-11-29 12:26:53 +00:00
{
Pairs = new Dictionary<string, T>();
AsyncList = new List<T>();
Factory = factory;
2020-10-08 08:56:46 +00:00
Configuration = configuration;
2021-10-12 09:04:24 +00:00
MigrationHistory = migrationHistory;
2019-11-29 12:26:53 +00:00
}
public override T Get(string name)
{
if (!Pairs.ContainsKey(name))
{
2020-09-02 15:23:39 +00:00
var t = base.Get(name);
Pairs.Add(name, t);
if (t is BaseDbContext dbContext)
{
2021-10-12 09:04:24 +00:00
if (Configuration["migration:enabled"] == "true"
2021-10-22 10:08:09 +00:00
&& MigrationHistory.TryAddMigratedContext(t.GetType()))
2020-10-07 12:20:06 +00:00
{
dbContext.Migrate();
}
2020-09-02 15:23:39 +00:00
}
2019-11-29 12:26:53 +00:00
}
2019-12-16 14:55:59 +00:00
return Pairs[name];
2019-11-29 12:26:53 +00:00
}
public T GetNew(string name = "default")
{
var result = Factory.Create(name);
AsyncList.Add(result);
return result;
}
public void Dispose()
{
foreach (var v in Pairs)
{
v.Value.Dispose();
}
2019-12-10 14:47:48 +00:00
foreach (var v in AsyncList)
{
v.Dispose();
}
2019-11-29 12:26:53 +00:00
}
}
2019-12-10 15:06:51 +00:00
2020-10-20 13:24:46 +00:00
[Scope(typeof(ConfigureDbContext<>))]
2019-12-16 14:55:59 +00:00
public class DbContextManager<T> : BaseDbContextManager<T> where T : BaseDbContext, new()
{
2021-10-12 09:04:24 +00:00
public DbContextManager(IOptionsFactory<T> factory, IConfiguration configuration,
MigrationHistory migrationHistory) : base(factory, configuration, migrationHistory)
2019-12-16 14:55:59 +00:00
{
}
}
public class MultiRegionalDbContextManager<T> : BaseDbContextManager<MultiRegionalDbContext<T>> where T : BaseDbContext, new()
{
2021-10-12 09:04:24 +00:00
public MultiRegionalDbContextManager(IOptionsFactory<MultiRegionalDbContext<T>> factory, IConfiguration configuration,
MigrationHistory migrationHistory) : base(factory, configuration, migrationHistory)
2019-12-16 14:55:59 +00:00
{
}
}
2019-12-10 15:06:51 +00:00
public static class DbContextManagerExtension
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddDbContextManagerService<T>(this DIHelper services) where T : BaseDbContext, new()
2019-12-10 15:06:51 +00:00
{
2020-10-28 20:02:03 +00:00
//TODO
//services.TryAddScoped<MultiRegionalDbContextManager<T>>();
//services.TryAddScoped<IConfigureOptions<MultiRegionalDbContext<T>>, ConfigureMultiRegionalDbContext<T>>();
2020-07-17 10:52:28 +00:00
return services;
2019-12-10 15:06:51 +00:00
}
}
2019-11-29 12:26:53 +00:00
}