From 06bd964adc3ebd320139a537423ca32cbd637b11 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Wed, 6 Mar 2024 11:36:56 -0500 Subject: [PATCH] use DBContextFactory --- .../OqtaneServiceCollectionExtensions.cs | 15 +++++++++++-- .../HtmlText/Repository/HtmlTextContext.cs | 2 -- .../HtmlText/Repository/HtmlTextRepository.cs | 22 ++++++++++++------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs b/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs index b935b374..4b4a3b99 100644 --- a/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs +++ b/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs @@ -21,7 +21,6 @@ using Microsoft.OpenApi.Models; using Oqtane.Infrastructure; using Oqtane.Infrastructure.Interfaces; using Oqtane.Managers; -using Oqtane.Models; using Oqtane.Modules; using Oqtane.Repository; using Oqtane.Security; @@ -45,7 +44,6 @@ namespace Microsoft.Extensions.DependencyInjection { services.AddDbContext(options => { }, ServiceLifetime.Transient); services.AddDbContext(options => { }, ServiceLifetime.Transient); - return services; } @@ -313,6 +311,19 @@ namespace Microsoft.Extensions.DependencyInjection serviceType = Type.GetType(serviceName); } services.AddTransient(serviceType ?? implementationType, implementationType); + + if (implementationType.BaseType == typeof(DBContextBase)) + { + if (implementationType.Name == "HtmlTextContext") + { + services.AddDbContextFactory(opt => { }, ServiceLifetime.Scoped); + } + // need a way to call AddDbContextFactory dynamically passing the implementationType + //typeof(IServiceCollection) + // .GetMethod("AddDbContextFactory") + // .MakeGenericMethod(implementationType) + // .Invoke(services, new object[] { new DbContextOptionsBuilder(), ServiceLifetime.Scoped }); + } } } diff --git a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextContext.cs b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextContext.cs index 9f7e93de..bdf14e74 100644 --- a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextContext.cs +++ b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextContext.cs @@ -1,7 +1,5 @@ -using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Oqtane.Documentation; -using Oqtane.Infrastructure; using Oqtane.Repository; using Oqtane.Repository.Databases.Interfaces; diff --git a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs index 21e0ffeb..51a0055d 100644 --- a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs +++ b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs @@ -13,12 +13,14 @@ namespace Oqtane.Modules.HtmlText.Repository public class HtmlTextRepository : IHtmlTextRepository, ITransientService { private readonly HtmlTextContext _db; + private readonly IDbContextFactory _factory; private readonly IMemoryCache _cache; private readonly SiteState _siteState; - public HtmlTextRepository(HtmlTextContext context, IMemoryCache cache, SiteState siteState) + public HtmlTextRepository(HtmlTextContext context, IDbContextFactory factory, IMemoryCache cache, SiteState siteState) { _db = context; + _factory = factory; _cache = cache; _siteState = siteState; } @@ -58,29 +60,33 @@ namespace Oqtane.Modules.HtmlText.Repository return await _cache.GetOrCreateAsync($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", async entry => { entry.SlidingExpiration = TimeSpan.FromMinutes(30); - return await _db.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync(); + using var ctx = _factory.CreateDbContext(); + return await ctx.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync(); }); } public async Task GetHtmlTextAsync(int htmlTextId) { - return await _db.HtmlText.FindAsync(htmlTextId); + using var ctx = _factory.CreateDbContext(); + return await ctx.HtmlText.FindAsync(htmlTextId); } public async Task AddHtmlTextAsync(Models.HtmlText htmlText) { - _db.HtmlText.Add(htmlText); - await _db.SaveChangesAsync(); + using var ctx = _factory.CreateDbContext(); + ctx.HtmlText.Add(htmlText); + await ctx.SaveChangesAsync(); ClearCache(htmlText.ModuleId); return htmlText; } public async Task DeleteHtmlTextAsync(int htmlTextId) { - Models.HtmlText htmlText = _db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId); - _db.HtmlText.Remove(htmlText); + using var ctx = _factory.CreateDbContext(); + Models.HtmlText htmlText = ctx.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId); + ctx.HtmlText.Remove(htmlText); ClearCache(htmlText.ModuleId); - await _db.SaveChangesAsync(); + await ctx.SaveChangesAsync(); } private void ClearCache(int moduleId)