Duda Active Record

3

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

    
asked by Gaston 20.03.2018 в 08:08
source

1 answer

0

Assuming the methods traerTodo() , obtenerConexion() and ejecutarConsulta() are the class ConexionImpl , then try sending the reference of the class to the method static to be able to make the queries:

public class ServicioCliente
{
  public static IEnumerable<Cliente> ObtenerClientes()
  {
     // accedes a los metodo del la clase conexion por medio a la propiedad static
     var conexion = Conexion.Instancia.obtenerTodos();
  }
}

Or you can create a static property that gives you the instance of the connection class:

public abstract class Conexion
{
   public abstract IEnumerable<object> traerTodo();

   //...
   public static Conexion Instancia {
        get{
            return new ConexionImp();
        }
   }
}

Then to access the serious connection:

public class ServicioCliente
{
  public static IEnumerable<Cliente> ObtenerClientes()
  {
     // accedes a los metodo del la clase conexion por medio a la propiedad static
     var conexion = Conexion.Instancia.obtenerTodos();
  }
}

Note that you use type ConexionImpl . This is because the base connection class is abstract and obviously you need a specific type to create an instance.

    
answered by 21.03.2018 в 14:47