De antemano les agradezco, se tomen el tiempo para ayudarme, la cuestión es la siguiente:
Estoy haciendo una API en Net Core 6 aplicando el patrón de diseño DI, no tengo mucho en esta tecnología estoy aprendiendo por lo que al ejecutar el proyecto me sale un error en la clase Program.cs:
Hasta este punto entiendo que el error es debido a que no le estoy indicando al contenedor de donde inyectar cierta clase en el constructor del controller, ahora si agrego:
builder.Services.AddScoped<IPaymentsProcesor, PaymentsProcessor>();
A la variable builder, donde IPaymentsProcesor es la interfaz que estoy inyectando al constructor de mi controlador, me sale el siguiente error:
En este punto es donde estoy atorado, sigo buscando soluciones, así mismo estoy buscando la maera de pasar por constructor a la clase PaymentsRepository.cs una conexión, similar a una que hice con net framework:
Adjunto el código del proyecto, agradeciendo nuevamente me puedan orientar.
Program.cs
builder.Services.AddControllers();
builder.Services.AddScoped<IPaymentsProcesor,PaymentsProcessor>();
builder.Services.AddEndpointsApiExplorer();
PaymetsController.cs
private readonly IPaymentsProcesor paymentsProcesor;
public PaymentsController(IPaymentsProcesor paymentsProcesorPr)
{
paymentsProcesor = paymentsProcesorPr;
}
[HttpGet]
public async Task<IActionResult> Get()
{
try
{
List<Payments> list = await paymentsProcesor.GetFullList();
if (list != null)
{
Respuesta.Estatus = EstatusRespuestaJSON.OK;
Respuesta.Data = list;
}
return new JsonResult(Respuesta);
}
catch (Exception ex)
{
Respuesta.Estatus = EstatusRespuestaJSON.ERROR;
Respuesta.Mensaje = ex.Message;
Respuesta.Data = null;
return new JsonResult(Respuesta);
}
}
IPaymentsProcesor.cs
public interface IPaymentsProcesor
{
string? Mensaje { get; set; }
Task<List<Payments>> GetFullList();
}
PaymentsProcessor.cs
public class PaymentsProcessor : IPaymentsProcesor
{
public string? Mensaje { get; set; }
public readonly IPaymentsRepository paymentsRepository;
public PaymentsProcessor(IPaymentsRepository paymentsRepositoryCnt)
{
paymentsRepository = paymentsRepositoryCnt;
}
public async Task<List<Payments>> GetFullList()
{
List<Payments> list = await paymentsRepository.GetAll();
if (paymentsRepository.Estatus == Estatus.SIN_RESULTADO)
Mensaje = "La busqueda no obtubo ningun resultado";
else if (paymentsRepository.Estatus == Estatus.ERROR)
Mensaje = "Ocurrió un error no controlado por el sistema, por favor contacte a soporte técnico.";
return list;
}
}
PaymentsRepository.cs
public class PaymentsRepository : IPaymentsRepository
{
public string? MensajeError { set; get; }
public Estatus Estatus { get; set; }
private SqlConnection Connection;
private bool IsValidConnection = false;
public PaymentsRepository(ServerConnection connection)
{
Connection = connection.SqlConnection;
IsValidConnection = connection.IsValidConnection;
}
public async Task<List<Payments>> GetAll()
{
try
{
if (!IsValidConnection)
throw new Exception("Connection no valid");
SqlCommand command = new("payments_ShowPaymentList", Connection);
await Connection.OpenAsync();
command.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = await command.ExecuteReaderAsync();
DataTable table = new();
table.Load(reader);
List<Payments> list = Helper.DataTableToList<Payments>(table);
if (list.Count > 0)
Estatus = Estatus.OK;
else
Estatus = Estatus.SIN_RESULTADO;
return list;
}
catch (Exception ex)
{
MensajeError = ex.Message;
Estatus = Estatus.ERROR;
return null;
}
finally
{
if (IsValidConnection && Connection.State == ConnectionState.Open)
Connection.Close();
}
}
}