Jonathan Jenne 6131948f2a 16.01.24
2024-01-16 13:09:21 +01:00

110 lines
3.4 KiB
C#

using DigitalData.Modules.Database;
using EnvelopeGenerator.Common;
namespace EnvelopeGenerator.Web.Services
{
public class DatabaseService: BaseService
{
public MSSQLServer MSSQL { get; set; }
public IConfiguration Config { get; set; }
public State State { get; set; }
public class ServiceContainer
{
public ActionService actionService;
public EmailService emailService;
public ServiceContainer(State state)
{
actionService = new(state);
emailService = new(state);
}
}
public class ModelContainer
{
public EnvelopeModel envelopeModel;
public DocumentModel documentModel;
public ReceiverModel receiverModel;
public ElementModel elementModel;
public HistoryModel historyModel;
public DocumentStatusModel documentStatusModel;
public EmailModel emailModel;
public ConfigModel configModel;
public ModelContainer(State state)
{
envelopeModel = new(state);
documentModel = new(state);
receiverModel = new(state);
elementModel = new(state);
historyModel = new(state);
documentStatusModel = new(state);
emailModel = new(state);
configModel = new(state);
}
}
public readonly ModelContainer? Models;
public readonly ServiceContainer? Services;
public DatabaseService(IConfiguration pConfig, LoggingService pLogging) : base(pConfig, pLogging)
{
logger = pLogging.LogConfig.GetLogger();
Config = pConfig;
logger.Debug("Establishing MSSQL Database connection..");
MSSQL = new MSSQLServer(logConfig, pConfig["Config:ConnectionString"]);
if (MSSQL.DBInitialized == true)
{
logger.Debug("MSSQL Connection established: [{0}]", MSSQL.MaskedConnectionString);
var state = GetState();
Models = new(state);
Services = new(state);
State = state;
}
else
{
logger.Error("Connection could not be established!");
}
}
public string? GetAppSetting(string key)
{
return Config[key];
}
/// <summary>
/// There is a circular dependency between state and models
/// All models need a state object, including the config Model
/// The state object needs to be filled with the DbConfig property,
/// which is obtained by the config Model.
/// So first, the config model is initialized with an incomplete state object,
/// then all the other models with the DbConfig property filled.
/// </summary>
private State GetState()
{
var state = GetInitialState();
var configModel = new ConfigModel(state);
state.DbConfig = configModel.LoadConfiguration();
return state;
}
private State GetInitialState()
{
return new State
{
Database = MSSQL,
LogConfig = logConfig,
UserId = 0,
DbConfig = null
};
}
}
}