Method to Receive Generic Values

Asked

Viewed 93 times

2

Greetings.

I have a method that receives 3 paramentrics: collection, business, quantity

Collection: is a class Collection who inherits another class, ex: man, woman, old... Business: is a class that contains the methods that make the negotiations before accessing the database (programming in 3 layers).
Amount: is only an integer type value.

Now imagine that my collection could be: homemCollection, womenColection, elderlyCollection or childrenCollection.

Imagine my business class could be: homeNegos, Womens, ElderlyGoodies, children...

What I want is for my method to receive any of these values and so I will use it for any of these classes.

I researched generic methods, generic classes, Linq, but I couldn’t solve the problem.

Follows the code of the Method:

private void MeuMetodo(HomemCollection colecao, HomemNegocios negocios, int quantidade)
{
   string retorno = negocios.nomeDoMetodoDaCamadaNegocios(colecao, negocios, quantidade)
}

This above method can only receive the collection and the business of man, I want to let this method receiving any other class.

If anyone has any link or material for me to study this situation, I am very grateful.

  • The classes Collection and Negocios, they inherit from a unique type, or implement some interface?

2 answers

2


The name of it is just "generic types"

The method would look like this:

private void MeuMetodo<TCol, TNeg>(TCol colecao, TNeg negocios, int quantidade) { }

The use would be like this:

MeuMetodo<HomemCollection, HomemNegocio>(collection, negocio, qtd);

Actually, like the parameters colecao and negocios are of the generic types, it is possible to hide the generic type and let the compiler make the type inference.

MeuMetodo(collection, negocio, qtd); 
// já que collection é do tipo HomemCollection
// e negocio é do tipo HomemNegocio

Notice that for you to be able to call some method of negocio will need this method to be defined within the generic method somehow, this is done using constraints (restrictions) type. For example:

interface INegocio
{
    MetodoDaCamadaNegocios();
}

class MinhaClasse
{
    private void MeuMetodo<TCol, TNeg>(TCol colecao, TNeg negocios, int quantidade) where TCol : INegocio
    {
        negocio.MetodoDaCamadaNegocios();
    }
}

The part where TCol : INegocio obliges the generic type TCol is an implementation of INegocio. Thus, the compiler can be sure of what methods exist in TCol.

  • Then I try to improve the answer, explain better and cover other cases.

  • thanks for your reply, I did the way you mentioned, I created an interface and put the business method within this interface, it worked ok... My question is : The only way to call these methods is to pass them to the interface?

  • @Agnaldo Yes, because the compiler needs to know that the method exists beforehand. By the way, I don’t know if you know, but you can choose an answer to your questions (only one), just mark the V on the left side of the response.

  • Thank you. I thought I could make both. hehe

1

Agnaldo there are several ways to do this. In one of them you can create an interface and make all your Collections implement them, for example IMinhaInterface

public interface IMinhaInterface {
    public string Nome { get; set; }
    /// outras properties
}

Classes :

public class HomemCollection : IMinhaInterface {
    /// propriedades unicas desta collection
}

public class MulherCollection : IMinhaInterface {
    /// propriedades unicas desta collection
}

public class CriancaCollection : IMinhaInterface {
    /// propriedades unicas desta collection
}

Then your method would be:

private void MeuMetodo(IMinhaInterface colecao, HomemNegocios negocios, int quantidade)
{
   string retorno = negocios.nomeDoMetodoDaCamadaNegocios(colecao, negocios, quantidade)
}

The same applies to the other cases mentioned. This is called polymorphism.

  • It’s a little more complex, you could present the most complete implementation?

  • 1

    @Fabri Damazio, Thank you for the reply, I will study on polymorphism. In his reply I was left with a doubt and for that reason I could not yet implement. My Homemcollection Class already inherits from the Man class. How I Make Her Inherit From The Man Class And The Interface?

  • @Agnaldo she does not inherit from the interface but rather imples the interface. In case it would be Homemcollection : Man, Iminhainterface

  • @Fabri Damazio - I understand, thank you very much for your patience and explanations.

Browser other questions tagged

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