controller responsibility

Asked

Viewed 128 times

0

I have my Model Bank

public class Banco {
   public int Id {get;set;}
   public string Nome {get;set;}
}

and my model Contabancaria

public class ContaBancaria 
{
  public int Id{get;set;}
  public string Nome {get;set;}
  public Banco Banco {get;set;}
}

Let’s see, for each of them I’ll have my controller

BancoController
ContaBancariaController

So I have a "Newbank" view, it’s part of it and all of your Insert/Edit/Delete actions are from my Account Controller. But in it I have a combo with list of banks

Whose responsibility it is to fetch and send me this information from the bank to this view of mine?

From Bancocontroller for having sole responsibility and when necessary call his method to return, or in Contabancariacontroller because these data are his, he have the role of listing and sending?

I don’t know if it’s clear, it’s an example that can happen, but there can also be more than one dependency for example...

1 answer

1

To solve this kind of question I usually create a layer of data access (Dal) and create in it the features that access data.

Following this model, we would have your classBancoDAO (Data Access Object) providing database data relating to the desired information relating to the model Banco, that can be easily consumed by the view.

If we wanted to make this data available on a ViewBag, we could do so:

public ActionResult Index()
{
    ViewBag.Bancos = new BancoDAO().ListarBancos(); // Busca os dados dos bancos

    return View();
}
  • Right, so in this example of yours, you get the bank listing sent from the controller that has dependency, in case the listing is done in the Account Controller

  • In my case I return a Jsonresult, so know whose responsibility it is to send the data from View

  • @Rod a calling for of the method is made in the ContaBancariaController. But the accountability of generating the data structure is Bancodon

  • @Rod makes more sense to store the data in a Viewbag than to create a new method to fetch data from another controller. Remember that you would be creating a controller method just to return a database listing.

  • Viewbag I can’t use in this case, since I’m using Select2, so I need a method that returns a json

  • @Rod in this case makes more sense to have a layer to provide this data (I would use Web API). It doesn’t make much sense to put this in a controller, in my opinion. I think a controller should just control the flow of navigation and direct requests and answers - even a waiter.

Show 1 more comment

Browser other questions tagged

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