Returning two objects from the Repository

Asked

Viewed 87 times

2

My application is divided as follows:

Repository --> Access Progress database
Controller --> Receives Repository return and sends to View
View --> Receives Controller Return.

Next, my Repository is like this :

public Projeto.Model.Projeto ObterProjetoPorEstabCcustoCodigo(Estabelecimento pEstabelecimento, CentroCusto pCentroCusto, Projeto.Model.Projeto pProjeto)
{
    using (var pim = new PIM(new infraProgress().buscaBroker(), "", "", ""))
    {
        pim.pim_busca_projeto_codigo(pEstabelecimento.Sigla, pCentroCusto.Id, pProjeto.Id, out string resumo, out string valPrevisto, out string valAlocado, out string valVariacao, out string valSaldo, out string tipoDeDocumento, out string msg);

        var projeto = new Projeto.Model.Projeto()
        {
            Descricao = resumo,
            ValPrevisto = Convert.ToDecimal(valPrevisto),
            ValorAlocado = Convert.ToDecimal(valAlocado),
            VariacaoLimite = Convert.ToDecimal(valVariacao),
            ValorLiberadoGastar = Convert.ToDecimal(valSaldo),
            Classificacao = tipoDeDocumento
        };

        return projeto;
    }
}
public Projeto.Model.Projeto ObterProjetoPorEstabCcustoCodigo(Estabelecimento pEstabelecimento, CentroCusto pCentroCusto, Projeto.Model.Projeto pProjeto)
{
    using (var pim = new PIM(new infraProgress().buscaBroker(), "", "", ""))
    {
        pim.pim_busca_projeto_codigo(pEstabelecimento.Sigla, pCentroCusto.Id, pProjeto.Id, out string resumo, out string valPrevisto, out string valAlocado, out string valVariacao, out string valSaldo, out string tipoDeDocumento, out string msg);

        var projeto = new Projeto.Model.Projeto()
        {
            Descricao = resumo,
            ValPrevisto = Convert.ToDecimal(valPrevisto),
            ValorAlocado = Convert.ToDecimal(valAlocado),
            VariacaoLimite = Convert.ToDecimal(valVariacao),
            ValorLiberadoGastar = Convert.ToDecimal(valSaldo),
            Classificacao = tipoDeDocumento
        };

        return projeto;
    }
}

Notice that the process pim_search_project_code returns the information of my Project object and also an out msg parameter.

That’s my problem, I need to return the object Projeto and also the msg.

This is because the msg returns some error conditions that I need to demonstrate to the user.

1 answer

2


There are several ways to solve this. Using tuples could be a good thing, but I think this mechanism is better in C# 7 and I find it difficult that you are using this version of the language. In addition, there is the whole "cultural" question of using tuples as return methods, since very little are we accustomed to this type of approach.

My tip is to create a class wrapper involving a Projeto and that message.

Something like:

public class ProjetoMensagem
{
    public Projeto Projeto { get; set; }
    public string Mensagem { get; set; }
}

Or, if you want to make a generic class

public class ModelMensagem<T> where T : class
{
    public T Model { get; set; }
    public string Mensagem { get; set; }
}
public ProjetoMensagem ObterProjetoPorEstabCcustoCodigo(Estabelecimento pEstabelecimento, CentroCusto pCentroCusto, Projeto.Model.Projeto pProjeto)
{
    using (var pim = new PIM(new infraProgress().buscaBroker(), "", "", ""))
    {
        pim.pim_busca_projeto_codigo(pEstabelecimento.Sigla, pCentroCusto.Id, pProjeto.Id, out string resumo, out string valPrevisto, out string valAlocado, out string valVariacao, out string valSaldo, out string tipoDeDocumento, out string msg);

        var projeto = new Projeto.Model.Projeto()
        {
            Descricao = resumo,
            ValPrevisto = Convert.ToDecimal(valPrevisto),
            ValorAlocado = Convert.ToDecimal(valAlocado),
            VariacaoLimite = Convert.ToDecimal(valVariacao),
            ValorLiberadoGastar = Convert.ToDecimal(valSaldo),
            Classificacao = tipoDeDocumento
        };

        return new ProjetoMensagem
        {
            Projeto = projeto,
            Mensagem = msg
        };
    }
}
  • I like your idea.

  • In this case I have several routines that go through this kind of problem? How can I create something more generic? In this case, the name of my application is PIM, I could create a Pimmensage template and within this model will add the classes as needed?

  • 1

    It depends on the case. If you will always have a model and a message, you can create a generic class that contains a generic object and a string. This way you avoid having to create a class for each model.

  • 1

    I left an example, @Andersonapdodesouza

  • Thank you, thank you very much. Problem solved.

Browser other questions tagged

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