[성현모] TRA GetDBContext 기능 추가

This commit is contained in:
SHM
2025-08-29 14:37:57 +09:00
parent e6f0f6d575
commit adc66987c0
5 changed files with 148 additions and 14 deletions

Binary file not shown.

View File

@ -12,7 +12,7 @@ namespace SystemX.Core.Services
{
public class DbContextProvider
{
private Dictionary<string, DataBase> DBDictionary = new Dictionary<string, DataBase>();
public Dictionary<string, DataBase> DBDictionary { get; private set; } = new Dictionary<string, DataBase>();
public void SetDBList(IList<DataBase>? DBList)
{

View File

@ -1,7 +1,12 @@
@page "/tra"
@using Web.Tra.Services
@inject CPXV2LogService CPXV2LogService
<h3>TRA</h3>
@code {
protected override async Task OnInitializedAsync()
{
CPXV2LogService.Test();
}
}

View File

@ -4,6 +4,7 @@ using SystemX.Core.Config.Model;
using SystemX.Core.DB;
using SystemX.Core.Services;
using Web.Tra.Components;
using Web.Tra.Services;
using WebClient.Library.Config;
using WebClient.Library.Model;
@ -26,6 +27,9 @@ var builder = WebApplication.CreateBuilder(args);
//singleton
builder.Services.AddSingleton<ConfigService<WebClientConfig>>();
//scoped
builder.Services.AddScoped<CPXV2LogService>();
//db
builder.Services.AddSingleton<DbContextProvider>(); // Generic <20><><EFBFBD><EFBFBD>
@ -74,12 +78,17 @@ if (configService?.OpenConfig($@"{configDir}/{configFileName}") == true)
var longTermDB = getDbList.Where(x => x.Name.ToLower().Contains("longterm")).ToList();
List<DataBase> logDb = new List<DataBase>();
foreach (var db in longTermDB.Select((value,index) => (value, index)))
foreach (var db in longTermDB)
{
try
{
var year = db.Name.Split("_")[1];
if(Int32.TryParse(year, out var index))
{
logDb.Add(new DataBase
{
DBID = db.index,
DBName = db.value.Name,
DBID = index,
DBName = db.Name,
IP = "127.0.0.1",
Port = 1433,
UserID = "Alis",
@ -87,6 +96,13 @@ if (configService?.OpenConfig($@"{configDir}/{configFileName}") == true)
DBContext = ""
});
}
}
catch(Exception e)
{
LogXnet.WriteLine("DB Register Exception");
LogXnet.WriteLine(e);
}
}
dbProvider.SetDBList(logDb);
}

View File

@ -0,0 +1,113 @@
using Azure.Core;
using Microsoft.EntityFrameworkCore;
using System.Data;
using SystemX.Core.DB;
using SystemX.Core.Services;
using WebClient.Library.Config;
namespace Web.Tra.Services
{
public class CPXV2LogService
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ConfigService<WebClientConfig>? _configService;
public CPXV2LogService(IServiceProvider serviceProvider, IServiceScopeFactory scopeFactory, ConfigService<WebClientConfig> configService)
{
_scopeFactory = scopeFactory;
_configService = configService;
}
public async Task Test()
{
using (var scope = _scopeFactory.CreateScope())
{
var provider = scope.ServiceProvider.GetRequiredService<DbContextProvider>();
using (var context = GetDBContext<CPXV2>(provider, 1))
{
if (context is not null)
{
}
}
}
using (var scope = _scopeFactory.CreateScope())
{
var provider = scope.ServiceProvider.GetRequiredService<DbContextProvider>();
using (var context = GetDBContext<CPXV2Log>(provider, 2023))
{
if (context is not null)
{
}
}
}
using (var scope = _scopeFactory.CreateScope())
{
var provider = scope.ServiceProvider.GetRequiredService<DbContextProvider>();
using (var context = GetDBContext<CPXV2Log>(provider, 2024))
{
if (context is not null)
{
}
}
}
}
private T? GetDBContext<T>(DbContextProvider provider, int dbID) where T : DbContext
{
var findDB = provider.DBDictionary.Keys.First(x => x.Contains(dbID.ToString()));
if(string.IsNullOrEmpty(findDB) == false)
{
if (provider.DBDictionary.TryGetValue(findDB, out var dbContext))
{
return provider?.GetDBContext<T>($"{dbContext.DBName}");
}
}
return null;
}
//public async Task<Response_SelectUniqueKy> Request_SelectUniqueKey(Request_SelectUniqueKey request, string guid = "")
//{
// Response_SelectUniqueKy response = new Response_SelectUniqueKy();
// if (request != null)
// {
// response.Identity = request.Identity;
// using (var scope = _scopeFactory.CreateScope())
// {
// var provider = scope.ServiceProvider.GetRequiredService<DbContextProvider>();
// using (var context = GetUniqueKeyDBContext(provider, 1))
// {
// if (context is not null)
// {
// try
// {
// using (var transaction = await context.CreateTransactionAsync(IsolationLevel.ReadUncommitted))
// {
// var data = await context.tUniqueKeyStorages.AsNoTracking().FirstOrDefaultAsync(x => x.cIdentity == request.Identity);
// await context.CloseTransactionAsync(transaction);
// if (data != null)
// {
// response.Data1 = data.cData1;
// response.Data2 = data.cData2;
// response.Data3 = data.cData3;
// response.Data4 = data.cData4;
// response.Data5 = data.cData5;
// }
// }
// }
// catch (Exception e)
// {
// LogXnet.WriteLine($"Select Unique Key Transaction Error::{guid}", LogXLabel.Error);
// LogXnet.WriteLine(e);
// }
// }
// }
// }
// }
// return response;
//}
}
}