C# 可以将私有方法放在我的控制器中,还是应该使用 asp.net mvc 将它们分成某种类型的帮助程序类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/645794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
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?
提问by Mike Roosa
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() 放在哪里?
回答by tvanfosson
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. 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.
架构的更大问题——使用 switch 语句或策略模式等——很难仅从这个片段中回答。我对这个 switch 语句并没有特别生气,但是您可能希望让 SessionHelper 返回一个策略来为您加载正确的视图数据。在这种情况下,加载视图的代码将放在策略类中。
DataStrategy strategy = SessionHelper.GetDataStrategy()
if (strategy == null)
{
RedirectToAction("Logout","Account");
}
strategy.LoadViewData( ViewData );
return View();
回答by Chad Moran
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.
因为 ASP.NET MVC 倾向于约定而不是配置,所以在以 Controller 结尾的类上的任何公共方法都被假定为操作方法。如果他们是私人的,他们就不是。
So it's completely OK to have private methods in a controller class.
所以在控制器类中有私有方法是完全可以的。
回答by Justus Burger
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.
一个辅助类,即使只有 1 个方法,也更容易测试和模拟。它还创建了更强的逻辑关注点分离。这使得在调试时更容易处理。
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.
但实际上,这是您可以为永恒争论两种方式的情况之一。但这归结为您所追求的工艺水平,或者更准确地说,您愿意接受的工艺水平。