DocSpace-buildtools/common/ASC.Core.Common/EF/Context/BaseDbContext.cs

170 lines
5.8 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
using DbContext = Microsoft.EntityFrameworkCore.DbContext;
2019-11-29 12:26:53 +00:00
2022-02-15 11:52:43 +00:00
namespace ASC.Core.Common.EF;
public enum Provider
2019-11-29 12:26:53 +00:00
{
2022-02-15 11:52:43 +00:00
PostgreSql,
MySql
}
2020-09-02 15:23:39 +00:00
2022-02-15 11:52:43 +00:00
public class BaseDbContext : DbContext
{
public ConnectionStringSettings ConnectionStringSettings { get; set; }
2022-07-24 13:02:00 +00:00
public string MigrateAssembly { get; set; }
2022-03-25 14:54:34 +00:00
internal ILoggerFactory LoggerFactory { get; set; }
2022-02-15 11:52:43 +00:00
public static readonly ServerVersion ServerVersion = ServerVersion.Parse("8.0.25");
2022-03-25 14:54:34 +00:00
protected Provider _provider;
public BaseDbContext() { }
public BaseDbContext(DbContextOptions options) : base(options) { }
2020-10-06 07:06:18 +00:00
2022-02-15 11:52:43 +00:00
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
2022-03-25 14:54:34 +00:00
optionsBuilder.UseLoggerFactory(LoggerFactory);
2022-02-15 11:52:43 +00:00
optionsBuilder.EnableSensitiveDataLogging();
2022-03-25 14:54:34 +00:00
_provider = GetProviderByConnectionString();
switch (_provider)
2019-11-29 12:26:53 +00:00
{
2022-02-15 11:52:43 +00:00
case Provider.MySql:
2022-07-22 19:31:42 +00:00
optionsBuilder.ReplaceService<IMigrationsSqlGenerator, CustomMySqlMigrationsSqlGenerator>();
optionsBuilder.UseMySql(ConnectionStringSettings.ConnectionString, ServerVersion, providerOptions =>
2022-02-15 11:52:43 +00:00
{
2022-03-25 14:54:34 +00:00
if (!string.IsNullOrEmpty(MigrateAssembly))
2020-12-25 10:48:04 +00:00
{
providerOptions.MigrationsAssembly(MigrateAssembly);
2022-02-15 11:52:43 +00:00
}
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
providerOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
2022-02-15 11:52:43 +00:00
});
break;
case Provider.PostgreSql:
optionsBuilder.UseNpgsql(ConnectionStringSettings.ConnectionString);
break;
2019-11-29 12:26:53 +00:00
}
2022-02-15 11:52:43 +00:00
}
2020-10-06 07:06:18 +00:00
2022-02-15 11:52:43 +00:00
public Provider GetProviderByConnectionString()
{
switch (ConnectionStringSettings.ProviderName)
2020-10-06 07:06:18 +00:00
{
2022-02-15 11:52:43 +00:00
case "MySql.Data.MySqlClient":
return Provider.MySql;
case "Npgsql":
return Provider.PostgreSql;
default:
break;
2020-10-06 07:06:18 +00:00
}
2022-02-15 11:52:43 +00:00
return Provider.MySql;
2019-11-29 12:26:53 +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 BaseDbContextExtension
{
public static T AddOrUpdate<T, TContext>(this TContext b, Expression<Func<TContext, DbSet<T>>> expressionDbSet, T entity) where T : BaseEntity where TContext : BaseDbContext
2019-12-17 13:01:59 +00:00
{
2022-02-15 11:52:43 +00:00
var dbSet = expressionDbSet.Compile().Invoke(b);
var existingBlog = dbSet.Find(entity.GetKeys());
if (existingBlog == null)
2019-12-17 13:01:59 +00:00
{
2022-02-15 11:52:43 +00:00
return dbSet.Add(entity).Entity;
}
else
{
b.Entry(existingBlog).CurrentValues.SetValues(entity);
return entity;
2019-12-17 13:01:59 +00:00
}
}
public static async Task<T> AddOrUpdateAsync<T, TContext>(this TContext b, Expression<Func<TContext, DbSet<T>>> expressionDbSet, T entity) where T : BaseEntity where TContext : BaseDbContext
2019-12-17 13:01:59 +00:00
{
var dbSet = expressionDbSet.Compile().Invoke(b);
var existingBlog = await dbSet.FindAsync(entity.GetKeys());
if (existingBlog == null)
{
var entityEntry = await dbSet.AddAsync(entity);
return entityEntry.Entity;
}
else
{
2022-02-15 11:52:43 +00:00
b.Entry(existingBlog).CurrentValues.SetValues(entity);
2022-02-15 11:52:43 +00:00
return entity;
2019-12-17 13:01:59 +00:00
}
}
2022-02-15 11:52:43 +00:00
}
2019-12-17 13:01:59 +00:00
2022-02-15 11:52:43 +00:00
public abstract class BaseEntity
{
public abstract object[] GetKeys();
}
2019-12-17 13:01:59 +00:00
2022-02-15 11:52:43 +00:00
public class MultiRegionalDbContext<T> : IDisposable, IAsyncDisposable where T : BaseDbContext, new()
{
public MultiRegionalDbContext() { }
2019-12-16 14:55:59 +00:00
2022-03-25 14:54:34 +00:00
internal List<T> Context { get; set; }
2019-12-16 14:55:59 +00:00
2022-02-15 11:52:43 +00:00
public void Dispose()
{
2022-03-25 14:54:34 +00:00
if (Context == null)
2019-12-16 14:55:59 +00:00
{
2022-02-15 11:52:43 +00:00
return;
}
2019-12-16 14:55:59 +00:00
2022-03-25 14:54:34 +00:00
foreach (var c in Context)
2022-02-15 11:52:43 +00:00
{
if (c != null)
2019-12-16 14:55:59 +00:00
{
2022-02-15 11:52:43 +00:00
c.Dispose();
2019-12-16 14:55:59 +00:00
}
}
2022-02-15 11:52:43 +00:00
}
2022-01-11 15:37:19 +00:00
2022-02-15 11:52:43 +00:00
public ValueTask DisposeAsync()
{
2022-03-25 14:54:34 +00:00
return Context == null ? ValueTask.CompletedTask : InternalDisposeAsync();
2022-02-15 11:52:43 +00:00
}
2022-01-11 15:37:19 +00:00
2022-02-15 11:52:43 +00:00
private async ValueTask InternalDisposeAsync()
{
2022-03-25 14:54:34 +00:00
foreach (var c in Context)
2022-01-11 15:37:19 +00:00
{
2022-02-15 11:52:43 +00:00
if (c != null)
2019-12-16 14:55:59 +00:00
{
2022-02-15 11:52:43 +00:00
await c.DisposeAsync();
2019-12-16 14:55:59 +00:00
}
}
}
2019-11-29 12:26:53 +00:00
}