Problem accessing methods of the concrete class using this reference in C #

-1

I implemented an abstract% class with Service and another using Generics being concrete.

Every design structure for each concrete class has an abstract.

My doubt seeing the following ...

When using the reference EmpresaService , it points to the method in the abstract class and not the concrete, and this is bad because it complicates when implementing individual methods in class this.Dao.ExisteRegistro(); for example.

Is my error being in class design?

PlanoService class

using System;
using System.Collections.Generic;
using Cobranca.pkgDao;
using Cobranca.pkgModel;

namespace Cobranca.pkgService
{
    public class PlanoService : Service<Plano>
    {

        public PlanoService()
        {
            this.Dao = new PlanoDao();
        }

        //método em questão que mostra o erro
        public bool ExisteRegistro()
        {
            return this.Dao.ExisteRegistro();
        }

        // métodos e mais métodos abaixo
    }
}

Class EnterpriseDao

using System;
using System.Collections.Generic;
using Cobranca.pkgModel;
using MySql.Data.MySqlClient;

namespace Cobranca.pkgDao
{
    class EmpresaDao : Dao<Empresa>
    {
        public bool ExistePlanos()
        {
            try
            {
                base.AbrirConexao();
                MySqlCommand cmd = base.Conexao.CreateCommand();
                cmd.CommandText = "SELECT COUNT(*) FROM planos";
                return Convert.ToInt32(cmd.ExecuteScalar()) > 0;
            }
            finally
            {
                base.FecharConexao();
            }
        }
    }
}

Dao class

using System;
using MySql.Data.MySqlClient;
using Cobranca.pkgModel;
using System.Collections.Generic;

namespace Cobranca.pkgDao
{

    public abstract class Dao<TModel> where TModel : Persistant
    {
        public abstract bool Inserir(TModel model);
        public abstract bool Editar(TModel model);
        public abstract bool Excluir(long codigo);

        public abstract List<TModel> CarregarDados();

    }
}


Classe PlanoDao

using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using Cobranca.pkgModel;

namespace Cobranca.pkgDao
{
    public class PlanoDao : Dao<Plano>
    {
        public bool ExisteRegistro()
        {
            try
            {
                base.AbrirConexao();
                MySqlCommand cmd = base.Conexao.CreateCommand();
                cmd.CommandText = "SELECT COUNT(*) FROM planos";
                return Convert.ToInt32(cmd.ExecuteScalar()) == 1;
            }
            finally
            {
                base.FecharConexao();
            }
        }
    }
}
    
asked by anonymous 11.05.2018 / 12:50

1 answer

2

"When using the reference this.Dao.ExistRegistry () ;, it points to the method in the abstract class"

The reserved word this always references the current class, to refer to the class it is extending, base is used.

From the MSDN documentation:

  

The this keyword refers to the current instance of the class and is also   used as a modifier of the first parameter of a   extension.

Reference: link

To demonstrate this, I made a small block of code:

public abstract class ClasseAbstrata
{
    public abstract bool Inserir(int x);
    public abstract bool Editar(int x);
    public abstract bool Excluir(int x);

    public string QuemSouEu()
    {
        return "Abstrata";
    }
}

public class Derivada : ClasseAbstrata
{
    public override bool  Inserir(int x) { return false; }
    public override bool Editar(int x) { return false; }
    public override bool Excluir(int x) { return false; }

    public string QuemSouEu()
    {
        return "Derivada";
    }

    public string QuemSouEuBase()
    {
        return base.QuemSouEu();
    }
    public string QuemSouEuThis()
    {
        return this.QuemSouEu();
    }
    public string QuemSouEuParseAbstrata()
    {
        return ((ClasseAbstrata)this).QuemSouEu();
    }
    public string QuemSouEuParseDerivada()
    {
        return ((Derivada)this).QuemSouEu();
    }
}

I took the above class example, and added a method to the abstract class: QuemSouEu() . Since the other methods are abstract and not implemented in the class, this will have no practical effect here, so let's see how the QuemSouEu method behaves using this code:

var d = new Derivada();
Console.WriteLine("QuemSouEu: " + d.QuemSouEu());
Console.WriteLine("QuemSouEuBase: " + d.QuemSouEuBase());
Console.WriteLine("QuemSouEuThis: " + d.QuemSouEuThis());
Console.WriteLine("QuemSouEuParseAbstrata: " + d.QuemSouEuParseAbstrata());
Console.WriteLine("QuemSouEuParseDerivada: " + d.QuemSouEuParseDerivada());

Results:

  

Who Are I: Derivative
  QuemSouEuBase: Abstrata
  QuemSouEuThis: Derivative
  WhoSoEuParseAbstrata: Abstract
  WhoShouldDerivate: Derivative

We can see that where was used (Who's This) returned from the derived class, not the abstract class, even overriding the method.

Other observations:
- this reference abstract class;
- it is possible to make base for any of the classes to have the expected result (Methods WHAT TO PUT OUT AND WHEN TO BE DERIVED)

Here's the fiddle: link

    
11.05.2018 / 13:28