using DigitalData.Modules.Database; using DigitalData.Modules.Logging; using EnvelopeGenerator.Application; using EnvelopeGenerator.Common; namespace EnvelopeGenerator.Web.Services { public class DatabaseService { public MSSQLServer MSSQL { get; set; } ILogger _logger; 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(ILogger logger, IConfiguration config) { LogConfig logConfig = new LogConfig(LogConfig.PathType.CustomPath, config["NLog:variables:logDirectory"], null, "Digital Data", "ECM.EnvelopeGenerator.Web"); _logger = logger; _logger.LogInformation("Establishing MSSQL Database connection.."); MSSQL = new MSSQLServer(logConfig, config.GetConnectionString(Key.Default)); if (MSSQL.DBInitialized == true) { _logger.LogInformation("MSSQL Connection established: [{0}]", MSSQL.MaskedConnectionString); /// /// 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. /// State = new State { Database = MSSQL, LogConfig = logConfig, UserId = 0, DbConfig = null }; var configModel = new ConfigModel(State); State.DbConfig = configModel.LoadConfiguration(); Models = new(State); Services = new(State); } else { _logger.LogInformation("Connection could not be established!"); } } } }