75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using DigitalData.Modules.Database;
|
|
using EnvelopeGenerator.Common;
|
|
|
|
namespace EnvelopeGenerator.Web.Services
|
|
{
|
|
public class DatabaseService: BaseService
|
|
{
|
|
public MSSQLServer MSSQL { get; set; }
|
|
|
|
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 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: [{0}]", MSSQL.CurrentConnectionString);
|
|
|
|
// 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.
|
|
var state = GetState();
|
|
var configModel = new ConfigModel(state);
|
|
state.DbConfig = configModel.LoadConfiguration();
|
|
|
|
Models = new(state);
|
|
}
|
|
else
|
|
{
|
|
logger.Error("Connection could not be established!");
|
|
}
|
|
}
|
|
|
|
public State GetState()
|
|
{
|
|
return new State
|
|
{
|
|
Database = MSSQL,
|
|
LogConfig = logConfig,
|
|
UserId = 2 // TODO
|
|
};
|
|
}
|
|
}
|
|
}
|