use DBContextFactory
This commit is contained in:
@ -21,7 +21,6 @@ using Microsoft.OpenApi.Models;
|
|||||||
using Oqtane.Infrastructure;
|
using Oqtane.Infrastructure;
|
||||||
using Oqtane.Infrastructure.Interfaces;
|
using Oqtane.Infrastructure.Interfaces;
|
||||||
using Oqtane.Managers;
|
using Oqtane.Managers;
|
||||||
using Oqtane.Models;
|
|
||||||
using Oqtane.Modules;
|
using Oqtane.Modules;
|
||||||
using Oqtane.Repository;
|
using Oqtane.Repository;
|
||||||
using Oqtane.Security;
|
using Oqtane.Security;
|
||||||
@ -45,7 +44,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||||||
{
|
{
|
||||||
services.AddDbContext<MasterDBContext>(options => { }, ServiceLifetime.Transient);
|
services.AddDbContext<MasterDBContext>(options => { }, ServiceLifetime.Transient);
|
||||||
services.AddDbContext<TenantDBContext>(options => { }, ServiceLifetime.Transient);
|
services.AddDbContext<TenantDBContext>(options => { }, ServiceLifetime.Transient);
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -313,6 +311,19 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||||||
serviceType = Type.GetType(serviceName);
|
serviceType = Type.GetType(serviceName);
|
||||||
}
|
}
|
||||||
services.AddTransient(serviceType ?? implementationType, implementationType);
|
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 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Oqtane.Documentation;
|
using Oqtane.Documentation;
|
||||||
using Oqtane.Infrastructure;
|
|
||||||
using Oqtane.Repository;
|
using Oqtane.Repository;
|
||||||
using Oqtane.Repository.Databases.Interfaces;
|
using Oqtane.Repository.Databases.Interfaces;
|
||||||
|
|
||||||
|
@ -13,12 +13,14 @@ namespace Oqtane.Modules.HtmlText.Repository
|
|||||||
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
|
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
|
||||||
{
|
{
|
||||||
private readonly HtmlTextContext _db;
|
private readonly HtmlTextContext _db;
|
||||||
|
private readonly IDbContextFactory<HtmlTextContext> _factory;
|
||||||
private readonly IMemoryCache _cache;
|
private readonly IMemoryCache _cache;
|
||||||
private readonly SiteState _siteState;
|
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;
|
_db = context;
|
||||||
|
_factory = factory;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
_siteState = siteState;
|
_siteState = siteState;
|
||||||
}
|
}
|
||||||
@ -58,29 +60,33 @@ namespace Oqtane.Modules.HtmlText.Repository
|
|||||||
return await _cache.GetOrCreateAsync($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", async entry =>
|
return await _cache.GetOrCreateAsync($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", async entry =>
|
||||||
{
|
{
|
||||||
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
|
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)
|
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)
|
public async Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText)
|
||||||
{
|
{
|
||||||
_db.HtmlText.Add(htmlText);
|
using var ctx = _factory.CreateDbContext();
|
||||||
await _db.SaveChangesAsync();
|
ctx.HtmlText.Add(htmlText);
|
||||||
|
await ctx.SaveChangesAsync();
|
||||||
ClearCache(htmlText.ModuleId);
|
ClearCache(htmlText.ModuleId);
|
||||||
return htmlText;
|
return htmlText;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteHtmlTextAsync(int htmlTextId)
|
public async Task DeleteHtmlTextAsync(int htmlTextId)
|
||||||
{
|
{
|
||||||
Models.HtmlText htmlText = _db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
|
using var ctx = _factory.CreateDbContext();
|
||||||
_db.HtmlText.Remove(htmlText);
|
Models.HtmlText htmlText = ctx.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
|
||||||
|
ctx.HtmlText.Remove(htmlText);
|
||||||
ClearCache(htmlText.ModuleId);
|
ClearCache(htmlText.ModuleId);
|
||||||
await _db.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClearCache(int moduleId)
|
private void ClearCache(int moduleId)
|
||||||
|
Reference in New Issue
Block a user