using System.Data; using Dapper; using Microsoft.Extensions.Options; namespace CrupestApi.Commons.Crud; // TODO: Implement and register this service. public class CrudService : IDisposable { protected readonly TableInfo _table; protected readonly IDbConnection _dbConnection; protected readonly IOptionsSnapshot _crupestApiOptions; private readonly ILogger> _logger; public CrudService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, IOptionsSnapshot crupestApiOptions, ILogger> logger) { _table = tableInfoFactory.Get(typeof(TEntity)); _dbConnection = dbConnectionFactory.Get(); _crupestApiOptions = crupestApiOptions; _logger = logger; 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(); } }