How to make Generic with 2 C#classes

Asked

Viewed 121 times

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?

1 answer

1


Its generic type in the abstract service class must follow the same type defined by Model and in the concrete class must specify a class which is an inheritance of Dao<Persistant>.

A hint: In C# it is common to call generic types with the "T" at the beginning of the name so as not to confuse with class names.

Try the following signature for the classes in the package Service:

namespace Cobranca.pkgService
{
    public abstract class Service<Model, DAO> where Model : Persistant where DAO : Dao<Model>
    {
        //métodos
    }
}

namespace Cobranca.pkgService
{
    public class PlanoService : Service<Plano, PlanoDao>
    {
        //metodos
    }
}
  • When you say use the prefix T before the generic name, it would be like Tmodel, TDAO and so on?

  • 1

    That’s right. It usually just uses T when it’s a type. It looks something like Service<T>

  • 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 in Service. How to deal with it?

  • 1

    @Macario1983 create a new question with data from this other context so that other people can also help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.