Merge pull request #3959 from sbwalker/dev

use DBContextFactory
This commit is contained in:
Shaun Walker 2024-03-06 11:37:10 -05:00 committed by GitHub
commit 64cf02bc45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 12 deletions

View File

@ -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<MasterDBContext>(options => { }, ServiceLifetime.Transient);
services.AddDbContext<TenantDBContext>(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<Oqtane.Modules.HtmlText.Repository.HtmlTextContext>(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 });
}
}
}

View File

@ -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;

View File

@ -13,12 +13,14 @@ namespace Oqtane.Modules.HtmlText.Repository
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
{
private readonly HtmlTextContext _db;
private readonly IDbContextFactory<HtmlTextContext> _factory;
private readonly IMemoryCache _cache;
private readonly SiteState _siteState;
public HtmlTextRepository(HtmlTextContext context, IMemoryCache cache, SiteState siteState)
public HtmlTextRepository(HtmlTextContext context, IDbContextFactory<HtmlTextContext> 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<Models.HtmlText> GetHtmlTextAsync(int htmlTextId)
{
return await _db.HtmlText.FindAsync(htmlTextId);
using var ctx = _factory.CreateDbContext();
return await ctx.HtmlText.FindAsync(htmlTextId);
}
public async Task<Models.HtmlText> 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)