feat: Define data models and set up DB connection with Dapper
This commit is contained in:
		| @@ -0,0 +1,9 @@ | ||||
| using System.Data; | ||||
|  | ||||
| namespace Mercados.Infrastructure.Persistence | ||||
| { | ||||
|   public interface IDbConnectionFactory | ||||
|   { | ||||
|     IDbConnection CreateConnection(); | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,24 @@ | ||||
| using Mercados.Infrastructure.Persistence; | ||||
| using Microsoft.Data.SqlClient; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using System.Data; | ||||
|  | ||||
| namespace Mercados.Infrastructure | ||||
| { | ||||
|   public class SqlConnectionFactory : IDbConnectionFactory | ||||
|   { | ||||
|     private readonly string _connectionString; | ||||
|  | ||||
|     public SqlConnectionFactory(IConfiguration configuration) | ||||
|     { | ||||
|       _connectionString = configuration.GetConnectionString("DefaultConnection") | ||||
|           ?? throw new ArgumentNullException(nameof(configuration), "La cadena de conexión 'DefaultConnection' no fue encontrada."); | ||||
|     } | ||||
|  | ||||
|     public IDbConnection CreateConnection() | ||||
|     { | ||||
|       // Dapper se encargará de abrir y cerrar la conexión automáticamente. | ||||
|       return new SqlConnection(_connectionString); | ||||
|     } | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user