0
Good night,
I’m having trouble using Generics in C#, I’m a programmer Java and I’m learning to use C#.
I want to use Generics in a class in the package Service, I am implementing the class referring to the mapped object, which would be Plano and the class PlanoDao.
Classes in the Model package:
namespace Cobranca.pkgModel
{
public abstract class Persistant
{
private long id;
//getter e setter
}
}
namespace Cobranca.pkgModel {
public class Plano : Persistant
{
private string nome;
private string tipoCobranca;
private double valor;
//getter e setter
}
}
Classes in the DAO package:
namespace Cobranca.pkgDao
{
public abstract class Dao<Model> where Model : Persistant
{
//métodos
}
}
namespace Cobranca.pkgDao
{
public class PlanoDao : Dao<Plano>
{
//metodos;
}
}
Classes in the Service package:
namespace Cobranca.pkgService
{
public abstract class Service<Model, DAO> where Model : Persistant where DAO : Dao<Persistant>
{
//métodos
}
}
namespace Cobranca.pkgService
{
public class PlanoService : Service<Plano, PlanoDao<Plano>>
{
//metodos
}
}
I’m wondering if I’m doing it right in class PlanoService makes me wrong:
Gravidade Código Descrição Projeto Arquivo Linha Estado de Supressão
Erro CS0308 O tipo não genérico "PlanoDao" não pode ser usado como argumentos de tipo Cobranca C:\Users\Diego\source\repos\Cobranca\Cobranca\pkgService\PlanoService.cs 8 Ativo
What am I doing wrong?
I even declared in the class signature PlanoService so also:
public class PlanoService : Service<Plano, PlanoDao>
But it keeps generating another mistake:
Gravidade Código Descrição Projeto Arquivo Linha Estado de Supressão
Erro CS0311 O tipo "Cobranca.pkgDao.PlanoDao" não pode ser usado como parâmetro de tipo "DAO" no tipo ou método genérico "Service<Model, DAO>". Não há conversão de referência implícita de "Cobranca.pkgDao.PlanoDao" em "Cobranca.pkgDao.Dao<Cobranca.pkgModel.Persistant>". Cobranca C:\Users\Diego\source\repos\Cobranca\Cobranca\pkgService\PlanoService.cs 8 Ativo
Who could save me?
When you say use the prefix T before the generic name, it would be like
Tmodel,TDAOand so on?– Macario1983
That’s right. It usually just uses T when it’s a type. It looks something like
Service<T>– Pagotti
A question, in my case, if I want to implement a method in the
PlanoService, I won’t have access to it because it has to be implemented inService. How to deal with it?– Macario1983
@Macario1983 create a new question with data from this other context so that other people can also help you.
– Pagotti