tags:

views:

77

answers:

2

I have a controller that loads some dropdowns based on the user type. For example:

public ActionResult Index()
    {
      switch (SessionHelper.ViewLimit)
          {
            case "C":
              ViewData["CustDivision"] = LoadCustDivisions();
              ViewData["Customer"] = LoadCustomers();
              break;
            case "P":
              ViewData["Customer"] = LoadCustomers();
              ViewData["Employee"] = LoadEmployees();
              break;
            case "D":
              ViewData["Customer"] = LoadCustomers();
              ViewData["Division"] = LoadDivisions();
              break;
             default:
              return RedirectToAction("Logout", "Account");
          }
    return View()
    }

First of all, does the switch statement belong in the controller and second of all if so, where should I put LoadCustomers(), LoadDivisions(), LoadEmployees()?

+4  A: 

If they are only used in this controller, I would say leaving them private to the controller is okay. Once you find that you have a need for them elsewhere, then look to migrate them to your DAL or a helper class.

The larger question of your architecture -- using switch statements or strategy pattern, etc. -- is hard to answer from just this snippet. I'm not particularly offended by this switch statement, but you may want to have your SessionHelper return a strategy that will load the correct view data for you. In that case, the code for loading the view would go in the strategy class.

  DataStrategy strategy = SessionHelper.GetDataStrategy()
  if (strategy == null)
  {
       RedirectToAction("Logout","Account");
  }

  strategy.LoadViewData( ViewData );

  return View();
tvanfosson
+1  A: 

Because ASP.NET MVC favors convention over configuration any public methods on a class ending with Controller are assumed to be action methods. If they're private, they're not.

So it's completely OK to have private methods in a controller class.

Chad Moran