ASP.NET MVC - 重用动作行为 [英] ASP.NET MVC - Reusing Action Behaviors

查看:116
本文介绍了ASP.NET MVC - 重用动作行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题主要与良好的设计。

假设我有像删除页面的控制器的操作,可在同一个控制器的两个单独的视图被调用。假设删除逻辑是不包含在行动本身,而是一些有条件的检查和调用正确的业务逻辑之类的,它没有任何意义重复删除操作的结构的时候,我可以代替有私人方法返回一个ActionResult这是我在这会导致删除这两个操作调用。我的问题是在哪里是把这样一个可重复使用的操作方法最好的地方?现在,我只是将它们标记私下和控制器类的区域坚持他们,但也许是一个密封的内部类将更有意义,这样的或于方法其他地方完全。

思考?

 公众的ActionResult EditPage(INT ID,的FormCollection的FormCollection)
{
VAR页= _pagesRepository.GetPage(ID);如果(页面== NULL)
返回视图(NOTFOUND);
如果(page.IsProtected)
返回视图(IllegalOperation);如果(的FormCollection [btnSave]!= NULL)
{
// ...
}
否则,如果(的FormCollection [btnDelete]!= NULL)
{
返回删除页面(页面);
}
返回RedirectToAction(「指数」);
}公众的ActionResult删除页面(INT ID)
{
VAR页= _pagesRepository.GetPage(ID);如果(页面== NULL)
返回视图(NOTFOUND);返回删除页面(页面);
}//动作可重复使用
私人RedirectToRouteResult删除页面(页页)
{
如果(页面= NULL&放大器;!&安培;!page.IsProtected)
{
_pagesRepository.Delete(页);
_pagesRepository.Save();FlashMessage(的String.Format(PageForms.PageDeleted,page.Name),MessageType.Success);返回RedirectToAction(「指数」);
}
返回RedirectToAction(「指数」);
}


解决方案

我不明白为什么你需要让你的可重复使用的方法的操作方法。为什么不返回void /布尔/等显示结果的私有方法保存,让你的公共行动方法返回RedirectToAction()?有效的是同样的结果,但我认为这是一个更清晰的方法。

 公众的ActionResult删除页面(INT ID)
{
        VAR页= _pagesRepository.GetPage(ID);        如果(页面== NULL)
                返回视图(NOTFOUND);        删除页面(页面);
        返回RedirectToAction(「指数」);
}//可重复使用的方法
私人无效删除页面(页页)
{
    //你相同的验证/保存在这里的逻辑
}

在未来你可能会考虑将这种私有方法删除页面进入执行验证和/或保存一个独立的服务类。返回的一个ActionResult铁定不会在这种情况下感觉,让我觉得这个例子是为您的方案更合适的方法。

This question pertains primarily to good design.

Suppose I have a controller action like DeletePage that can be invoked in two separate views of the same controller. Assuming the delete logic is not contained in the action itself, but rather some conditional checks and the like that call the correct business logic, it doesn't make sense to duplicate the structure of the delete action when I can instead have a private method that returns an ActionResult which I call in both actions which can cause a delete. My question is where is the best place to place a reusable action method like this? Right now I'm just marking them private and sticking them in a region of the controller class, but perhaps an sealed inner class would make more sense for such a method- or somewhere else entirely.

Thoughts?

public ActionResult EditPage(int id, FormCollection formCollection)
{
	var page = _pagesRepository.GetPage(id);

	if (page == null)
		return View("NotFound");
	if (page.IsProtected)
		return View("IllegalOperation");

	if (formCollection["btnSave"] != null)
	{
		//...
	}
	else if (formCollection["btnDelete"] != null)
	{
		return DeletePage(page);
	}
	return RedirectToAction("Index");
}

public ActionResult DeletePage(int id)
{
	var page = _pagesRepository.GetPage(id);

	if (page == null)
		return View("NotFound");

	return DeletePage(page);
}

// Reusable Action
private RedirectToRouteResult DeletePage(Page page)
{
	if(page != null && !page.IsProtected)
	{
		_pagesRepository.Delete(page);
		_pagesRepository.Save();

		FlashMessage(string.Format(PageForms.PageDeleted, page.Name), MessageType.Success);

		return RedirectToAction("Index");
	}
	return RedirectToAction("Index");
}

解决方案

I don't see why you need to make your reusable method an action method. Why not just a private method that returns void/bool/etc indicating the result of the save, and let your public action method return the RedirectToAction()? Effectively it's the same result, but I think it's a clearer approach.

public ActionResult DeletePage(int id)
{
        var page = _pagesRepository.GetPage(id);

        if (page == null)
                return View("NotFound");

        DeletePage(page);
        return RedirectToAction("Index");
}

//reusable method
private void DeletePage(Page page)
{
    //your same validation/save logic here
}

In the future you might consider moving this private DeletePage method into a separate service class that performs the validation and/or saving. Returning an an ActionResult would definitely not make sense in that case, so I think this example would be a more appropriate approach for your scenario.

这篇关于ASP.NET MVC - 重用动作行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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