I am doing a job for the university they are introducing us in the "Active Record" pattern in C #. The thing is that I have a class called Persistente
, let's say the base class from which all the others inherit. It contains methods such as obtenerConexion
, ejecutarConsulta
, traerTodo
, etc ..
I leave them below to guide you.
public abstract IEnumerable<object> traerTodo();
public SqlConnection obtenerConexion()
{
SqlConnection conexion = new SqlConnection(Persistente.strConnection);
if (conexion.State == ConnectionState.Closed)
conexion.Open();
return conexion;
}
public SqlDataReader ejecutarConsulta(SqlConnection conexion, string sql, List<SqlParameter> parametros, CommandType tipoComando, SqlTransaction transaction = null)
{
SqlDataReader ret = null;
try
{
if (conexion != null)
{
SqlCommand comando = new SqlCommand(sql, conexion);
if(parametros != null)
{
comando.Parameters.AddRange(parametros.ToArray());
}
comando.CommandType = tipoComando;
if (transaction != null)
{
comando.Transaction = transaction;
}
if (conexion.State != ConnectionState.Open)
{
conexion.Open();
}
ret = comando.ExecuteReader();
}
}
catch
{
throw;
}
return ret;
}
For example, I use these methods in this way: For the case of wanting to add a client, I create the object, it has a save method that is a override
of the base class Persistente
.
In its logic I create the string
sql, I get the connection, I create the list of parameters and I call the ejecutarNoQuery
method of the base class, which is the same as the one that leaves them above only changes the return and little thing else (I tell you this so you have an idea, what I'm talking about).
But in this case I want to bring all the clients that I have in my database, the issue is that to do this method it should be class, because the class is the one that manages all those objects Cliente
, but if I do class ( static
) I lose contact with all the methods described above
ejecutarConsulta
and obtenerConexion
. Then I honestly do not know how to do it and I'm going crazy, sorry for my ignorance is that I'm just learning, if you need more info or something please let me know and thank you very much