How to make Generic with 2 classes in C #

0

Good evening,

I'm having problems using Generics in C# , I'm programmer Java and I'm learning to use C# .

I want to use Generics in a class in the Service package, I'm implementing the class for the mapped object, which would be Plano and the PlanoDao class.

Classes in the package Model:

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 in doubt if I'm doing it right, in class PlanoService it generates error:

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 have already declared in the signature of class PlanoService so too:

public class PlanoService : Service<Plano, PlanoDao>

But it continues to generate another error:

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?

    
asked by anonymous 10.05.2018 / 02:49

1 answer

1

Your generic type in the abstract service class must match the same type defined by Model and in the concrete class you must specify a class that is inheritance of Dao<Persistant> .

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

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

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
    }
}
    
10.05.2018 / 13:09