2022-02-15 11:52:43 +00:00
|
|
|
|
namespace ASC.Core.Common.EF;
|
|
|
|
|
|
|
|
|
|
public class BaseDbContextManager<T> : OptionsManager<T>, IDisposable where T : class, IDisposable, IAsyncDisposable, new()
|
2019-11-29 12:26:53 +00:00
|
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
|
private Dictionary<string, T> _pairs;
|
|
|
|
|
private MigrationHistory _migrationHistory;
|
|
|
|
|
private List<T> _asyncList;
|
|
|
|
|
private IOptionsFactory<T> _factory;
|
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
|
|
|
|
|
|
public BaseDbContextManager(IOptionsFactory<T> factory, IConfiguration configuration,
|
|
|
|
|
MigrationHistory migrationHistory) : base(factory)
|
2019-11-29 12:26:53 +00:00
|
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
|
_pairs = new Dictionary<string, T>();
|
|
|
|
|
_asyncList = new List<T>();
|
|
|
|
|
_factory = factory;
|
|
|
|
|
_configuration = configuration;
|
|
|
|
|
_migrationHistory = migrationHistory;
|
|
|
|
|
}
|
2020-10-08 08:56:46 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
public override T Get(string name)
|
|
|
|
|
{
|
|
|
|
|
if (!_pairs.ContainsKey(name))
|
2019-11-29 12:26:53 +00:00
|
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
|
var t = base.Get(name);
|
|
|
|
|
_pairs.Add(name, t);
|
2019-11-29 12:26:53 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
if (t is BaseDbContext dbContext)
|
2019-11-29 12:26:53 +00:00
|
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
|
if (_configuration["migration:enabled"] == "true"
|
|
|
|
|
&& _migrationHistory.TryAddMigratedContext(t.GetType()))
|
2020-09-02 15:23:39 +00:00
|
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
|
dbContext.Migrate();
|
2020-09-02 15:23:39 +00:00
|
|
|
|
}
|
2019-11-29 12:26:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
return _pairs[name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetNew(string name = "default")
|
|
|
|
|
{
|
|
|
|
|
var result = _factory.Create(name);
|
2019-11-29 12:26:53 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
_asyncList.Add(result);
|
2019-11-29 12:26:53 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-11-29 12:26:53 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
foreach (var v in _pairs)
|
2019-11-29 12:26:53 +00:00
|
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
|
v.Value.Dispose();
|
|
|
|
|
}
|
2019-12-10 14:47:48 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
foreach (var v in _asyncList)
|
|
|
|
|
{
|
|
|
|
|
v.Dispose();
|
2019-11-29 12:26:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
}
|
2019-12-10 15:06:51 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
[Scope(typeof(ConfigureDbContext<>))]
|
|
|
|
|
public class DbContextManager<T> : BaseDbContextManager<T> where T : BaseDbContext, new()
|
|
|
|
|
{
|
|
|
|
|
public DbContextManager(IOptionsFactory<T> factory, IConfiguration configuration,
|
|
|
|
|
MigrationHistory migrationHistory) : base(factory, configuration, migrationHistory)
|
2019-12-16 14:55:59 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
}
|
2019-12-16 14:55:59 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
public class MultiRegionalDbContextManager<T> : BaseDbContextManager<MultiRegionalDbContext<T>> where T : BaseDbContext, new()
|
|
|
|
|
{
|
|
|
|
|
public MultiRegionalDbContextManager(IOptionsFactory<MultiRegionalDbContext<T>> factory, IConfiguration configuration,
|
|
|
|
|
MigrationHistory migrationHistory) : base(factory, configuration, migrationHistory)
|
2019-12-16 14:55:59 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
}
|
2019-12-16 14:55:59 +00:00
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
|
public static class DbContextManagerExtension
|
|
|
|
|
{
|
|
|
|
|
public static DIHelper AddDbContextManagerService<T>(this DIHelper services) where T : BaseDbContext, new()
|
2019-12-10 15:06:51 +00:00
|
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
|
//TODO
|
|
|
|
|
//services.TryAddScoped<MultiRegionalDbContextManager<T>>();
|
|
|
|
|
//services.TryAddScoped<IConfigureOptions<MultiRegionalDbContext<T>>, ConfigureMultiRegionalDbContext<T>>();
|
|
|
|
|
return services;
|
2019-12-10 15:06:51 +00:00
|
|
|
|
}
|
2019-11-29 12:26:53 +00:00
|
|
|
|
}
|