Jonathan Jenne b7fbb21076 09-01-2024
2024-01-09 11:01:33 +01:00

103 lines
3.2 KiB
C#

using DigitalData.Modules.Database;
using EnvelopeGenerator.Common;
namespace EnvelopeGenerator.Web.Services
{
public class DatabaseService: BaseService
{
public MSSQLServer MSSQL { 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 Config, LoggingService Logging) : base(Config, Logging)
{
logger = Logging.LogConfig.GetLogger();
logger.Debug("Establishing MSSQL Database connection..");
MSSQL = new MSSQLServer(logConfig, Config["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!");
}
}
/// <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
};
}
}
}