using System.Data; using System.Text.Json; using Dapper; namespace CrupestApi.Commons.Crud; // TODO: Register this. public class CrudService : IDisposable where TEntity : class { protected readonly TableInfo _table; protected readonly string? _connectionName; protected readonly IDbConnection _dbConnection; protected readonly EntityJsonHelper _jsonHelper; private readonly ILogger> _logger; public CrudService(string? connectionName, ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, EntityJsonHelper jsonHelper, ILoggerFactory loggerFactory) { _connectionName = connectionName; _table = tableInfoFactory.Get(typeof(TEntity)); _dbConnection = dbConnectionFactory.Get(_connectionName); _jsonHelper = jsonHelper; _logger = loggerFactory.CreateLogger>(); if (!_table.CheckExistence(_dbConnection)) { DoInitializeDatabase(_dbConnection); } } public virtual void DoInitializeDatabase(IDbConnection connection) { using var transaction = connection.BeginTransaction(); connection.Execute(_table.GenerateCreateTableSql(), transaction: transaction); transaction.Commit(); } public void Dispose() { _dbConnection.Dispose(); } }