控制器外的ActionResult [英] ActionResult outside the controller

查看:93
本文介绍了控制器外的ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们说控制器应该很薄,不应该包含商务逻辑,我试图实现瘦控制器。

但是假设我有现有的代码,例如,


They say controllers should be thin and should not contain buisness logic , Well I was trying to implement the thin controller.
But suppose I have existing code like ,

public class HomeController : BaseController
{
    public ActionResult HomeCapture(HomeModel model)
        {
            if (model.xprop == 1) {
                return RedirectToAction("ABC", "Help");
            } else if(model.yprop == 2){
                return RedirectToAction("Index", "Home");
            }else 
			{
				return RedirectToAction("Error", "Exit");
			}
			
        }
}





让这个控制器变薄,我是计划将此代码移动到另一个类(例如:HomeDecorator)并从控制器调用该方法

但是我该如何在该类中编写RedirectToAction?因为它只能写在控制器中。

如果我在该类(HomeDecorator)中使用新的RedirectToRouteResult并将其返回给控制器,它是否可以?

请帮助...



to make this controller thin , I am planning to move this code to another class(eg: HomeDecorator) and call that method from controller
But how am I supposed to write RedirectToAction in that class ? as it can only be written in controller.
is it ok if I take new RedirectToRouteResult in that class(HomeDecorator) and return it to controller?
Please help ...

推荐答案

该代码不是业务逻辑,它是处理来自视图的输入并处理从控制器输出的代码的代码,因此它应该保留在控制器中。当人们说你应该有业务逻辑时,他们指的是管理网站工作规则的代码。因此,如果您的站点是酒店管理站点,您的控制器应该具有检查给定日期的可用性的代码,然后使用结果来做出决定,例如房间的收费率。相反,您的控制器应该处理输入,例如日期和位置,然后将这些输入传递到模型(或其他类),该模型执行所有业务逻辑,然后将数据列表返回给控制器,并且数据是所有可用的房间,然后以相关的方式返回。
That code isn't business logic, it is code that deals with the inputs from the view and handles what is output from the controller so it should stay in the controller. When people say you should have business logic they mean code that governs the rules of how your site works. So if your site was a hotel management site your controllers should have code that checks the availability on a given date then uses the results to make decisions such as what rate the room is charged at. Instead your controller should handle the inputs such as the date and location, then pass those onto a model (or other class) that does all the business logic then returns to the controller a list of data and that data is all the available rooms which the controller then Returns in a relevant way.


如果您的代码不是很薄,那么每个开发人员编写的每个其他代码都非常厚。换句话说,您的代码不能比您现有的代码更薄。
If your code is not thin then every other code that every developer has written is very thick. In other words, your code cannot get thinner than what you already have.


这篇关于控制器外的ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆