简体   繁体   English

可以将私有方法放在我的控制器中,还是应该将它们分成某种类型的asp.net mvc辅助类?

[英]Is it okay to put private methods in my controller or should I separate them out into some type of helper class with asp.net mvc?

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()? 首先,switch语句是否属于控制器,如果是,那么我应该在哪里放置LoadCustomers(),LoadDivisions(),LoadEmployees()?

I feel NO - private method in a controller creates more problem than they solve. 我觉得没有 - 控制器中的私有方法会产生比他们解决的问题更多的问题。 Here are my reasons: 这是我的理由:

By the time you feel like creating a private method in a controller, you have identified a piece of code that is ether a bit "down and dirty" or repetitive. 当您想要在控制器中创建私有方法时,您已经识别出一段代码,这些代码有点“潦倒”或重复。 This is enough reason to create a separate helper class or move the code down the stack. 这是创建单独的帮助程序类或将代码向下移动的充分理由。

A helper class, even with just 1 method, is much easier to test and mock. 一个助手类,即使只有一个方法,也更容易测试和模拟。 Also it creates a stronger logical separation of concern. 它还创造了一个更强大的逻辑分离关注点。 This makes it easier to deal with when debugging. 这使得在调试时更容易处理。

I also agree with tvanfosson on using a strategy pattern in aid of not reinventing the wheel and demonstrating a more mature understanding of software development. 我也同意tvanfosson使用策略模式来帮助不重新发明轮子并展示对软件开发的更成熟的理解。

But in actual fact, this is one of those situations where you can argue both ways for eternity. 但实际上,这是你可以争论两种永恒方式的情况之一。 But it comes down to the level of craftsmanship you're aiming for, or more accurately, willing to settle for. 但它归结为你所瞄准的工艺水平,或更准确地说,愿意满足。

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. 一旦您发现其他地方需要它们,那么请将它们迁移到DAL或帮助程序类。

The larger question of your architecture -- using switch statements or strategy pattern, etc. -- is hard to answer from just this snippet. 您的体系结构的更大问题 - 使用switch语句或策略模式等 - 很难从这个片段中回答。 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. 我并没有特别被这个switch语句所冒犯,但您可能希望让SessionHelper返回一个策略,为您加载正确的视图数据。 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();

Because ASP.NET MVC favors convention over configuration any public methods on a class ending with Controller are assumed to be action methods. 因为ASP.NET MVC倾向于约定优于配置,所以任何以Controller结尾的类的公共方法都被认为是动作方法。 If they're private, they're not. 如果他们是私人的,他们就不是。

So it's completely OK to have private methods in a controller class. 因此,在控制器类中使用私有方法是完全可以的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net MVC Pager助手和视图无法一起使用-我应该在视图的第一行中添加什么 - ASP.Net MVC Pager helper and view not working together - what should I put in my views first line 如何在ASP.NET MVC中单元测试专用控制器方法? - How do I unit test private controller methods in ASP.NET MVC? 我应该将常见的实用程序方法放在 Asp.Net Core MVC 项目中的什么位置? - Where should I put common utility methods in an Asp.Net Core MVC-project? 如何对具有私有方法的ASP.Net MVC控制器进行单元测试? - How to unit test ASP.Net MVC controller that has private methods in the action? 引发空引用异常的 ASP.NET MVC 帮助程序方法 - ASP.NET MVC Helper Methods Throwing Null Reference Exception 我如何告诉 MVC 中间件我的 class 是 ASP.NET Core 中的一个 Controller? - How do I tell the MVC middleware that my class is a Controller in ASP.NET Core? 通过具体的类或接口对ASP.NET MVC Controller方法进行单元测试 - Unit testing ASP.NET MVC Controller methods through concrete class or interface 在ASP.NET MVC控制器中使用临时私有变量 - Use of temporary private variable in ASP.NET MVC controller 我是否应该通过 asp.net 核心 3.1 web ZDB974238D704A 中的所有 class 方法检测取消令牌? - Should I plumb Cancellation Tokens through all of my class methods in an asp.net core 3.1 web API? 如何在asp.net MVC中创建自定义帮助程序? - How to create my custom helper in asp.net MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM