我的控制器中有两个操作,它们共享负责选择视图的部分逻辑。我怎样才能让这部分成为共同的负担行动。示例:
控制器文件
1. if there is one document found and is type X, display it using OpenX View
2. if there is one document found and is type Y, display it using OpenY View
3. if there are more than documents found, display list using List View
4. if there are no documents found, display error using Error View
1. if there is one document found, display it using OpenMetaData View
2. if there are more than documents found, display list using List View
3. if there are no documents found, display error using Error View
如你所见,点3,4与2,3相同
我想创造出这样的
public DocumentController
{
public ActionResult Open( ... )
{
var dataFromWebService = service.GetData( ... );
return ViewSelector.GetLaunchView(dataFromWebService);
}
public ActionResult Open( ... )
{
var dataFromWebService = service.GetData( ... );
return ViewSelector.GetOpenMetaData(dataFromWebService);
}
}
public class ViewSelector
{
public static ActionResult GetLaunchView(DataFromWebService dataFromWebService)
{
if( dataFromWebService contains document type X)
return new ViewResult("OpenX",data);
if( dataFromWebService contains document type Y)
return new ViewResult("OpenY",data);
return CommonLogic(dataFromWebService);
}
public static ActionResult GetOpenMetaData(DataFromWebService dataFromWebService)
{
......
}
private static ActionResult CommonLogic(DataFromWebService dataFromWebService)
{
.... Common logic
}
}我想这样做是为了使我的主计长尽可能干净。
我可以创建ViewResults外部控制器,将数据附加到它们是在操作中返回它们吗?
这个设计是好的还是坏的?
也许有人会更好地处理这件事
发布于 2012-07-04 18:36:11
如果不需要访问控制器的任何上下文,则可以在控制器之外创建结果。在您的示例中,我将考虑使控制器的GetOpenMetaData()和GetLaunchView方法成为私有方法。
如果您需要在多个控制器上共享它,您还可以考虑将它放入一个抽象的BaseController中,并让控制器继承它。
https://stackoverflow.com/questions/11330671
复制相似问题