Method To Receive Generic Values

2

Greetings.

I have a method that gets 3 paraments: collection, business, quantity

Collection: is a collection class that inherits from another class, eg, man, woman, senior ... Business: is a class that contains the methods that handle the queries before accessing the database (3-tier programming). Quantity: is just a value of the integer type.

Now imagine that my collection might be: manCollection, womanColection, elderlyCollection or childCollection.

Imagine my business class might be: men, Business, Business, Elderly, Business ...

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 could not solve the problem.

Follow the Method code:

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

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

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

    
asked by anonymous 09.05.2018 / 22:01

2 answers

2

The name of this is just " generic types

The method would look like this:

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

The usage would look like this:

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

In fact, since the colecao and negocios parameters are of the generic types, you can hide the generic type and let the compiler draw the type inference.

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

Note that for you to be able to call any method of negocio you will need this method to be defined within the generic method in some way, this is done using constraints (constraints) of 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 where TCol : INegocio part forces the generic type TCol to be an implementation of INegocio . Thus, the compiler can be sure of what methods exist in TCol .

    
09.05.2018 / 22:34
1

Agnaldo there are several ways to do this. In one of them you can create an interface and make all your collections implements 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 .

    
09.05.2018 / 22:31