From bbc77f81cab59c176fcf21381ba9340df0f7dd20 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Thu, 21 Mar 2024 15:25:17 -0400 Subject: [PATCH] use DbContextFactory in all Html/Text methods --- .../HtmlText/Repository/HtmlTextRepository.cs | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs index 2ed63f69..8d6de983 100644 --- a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs +++ b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs @@ -12,14 +12,12 @@ namespace Oqtane.Modules.HtmlText.Repository [PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")] 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, IDbContextFactory factory, IMemoryCache cache, SiteState siteState) + public HtmlTextRepository(IDbContextFactory factory, IMemoryCache cache, SiteState siteState) { - _db = context; _factory = factory; _cache = cache; _siteState = siteState; @@ -30,29 +28,33 @@ namespace Oqtane.Modules.HtmlText.Repository return _cache.GetOrCreate($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", entry => { entry.SlidingExpiration = TimeSpan.FromMinutes(30); - return _db.HtmlText.Where(item => item.ModuleId == moduleId).ToList(); + using var db = _factory.CreateDbContext(); + return db.HtmlText.Where(item => item.ModuleId == moduleId).ToList(); }); } public Models.HtmlText GetHtmlText(int htmlTextId) { - return _db.HtmlText.Find(htmlTextId); + using var db = _factory.CreateDbContext(); + return db.HtmlText.Find(htmlTextId); } public Models.HtmlText AddHtmlText(Models.HtmlText htmlText) { - _db.HtmlText.Add(htmlText); - _db.SaveChanges(); + using var db = _factory.CreateDbContext(); + db.HtmlText.Add(htmlText); + db.SaveChanges(); ClearCache(htmlText.ModuleId); return htmlText; } public void DeleteHtmlText(int htmlTextId) { - Models.HtmlText htmlText = _db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId); - if (htmlText != null) _db.HtmlText.Remove(htmlText); + using var db = _factory.CreateDbContext(); + Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId); + if (htmlText != null) db.HtmlText.Remove(htmlText); ClearCache(htmlText.ModuleId); - _db.SaveChanges(); + db.SaveChanges(); } public async Task> GetHtmlTextsAsync(int moduleId) @@ -60,33 +62,33 @@ namespace Oqtane.Modules.HtmlText.Repository return await _cache.GetOrCreateAsync($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", async entry => { entry.SlidingExpiration = TimeSpan.FromMinutes(30); - using var ctx = _factory.CreateDbContext(); - return await ctx.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync(); + using var db = _factory.CreateDbContext(); + return await db.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync(); }); } public async Task GetHtmlTextAsync(int htmlTextId) { - using var ctx = _factory.CreateDbContext(); - return await ctx.HtmlText.FindAsync(htmlTextId); + using var db = _factory.CreateDbContext(); + return await db.HtmlText.FindAsync(htmlTextId); } public async Task AddHtmlTextAsync(Models.HtmlText htmlText) { - using var ctx = _factory.CreateDbContext(); - ctx.HtmlText.Add(htmlText); - await ctx.SaveChangesAsync(); + using var db = _factory.CreateDbContext(); + db.HtmlText.Add(htmlText); + await db.SaveChangesAsync(); ClearCache(htmlText.ModuleId); return htmlText; } public async Task DeleteHtmlTextAsync(int htmlTextId) { - using var ctx = _factory.CreateDbContext(); - Models.HtmlText htmlText = ctx.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId); - ctx.HtmlText.Remove(htmlText); + using var db = _factory.CreateDbContext(); + Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId); + db.HtmlText.Remove(htmlText); ClearCache(htmlText.ModuleId); - await ctx.SaveChangesAsync(); + await db.SaveChangesAsync(); } private void ClearCache(int moduleId)