开发者

Workflows in ASP.NET MVC

As a learning exercise, I want to migrate an 开发者_如何学编程existing flash application to ASP.NET MVC. It has around 20 forms, and although it's mostly linear, there is some degree of alternate flows based on user decisions or data returned.

Could anyone point me in the right direction of how a controller would handle this? I don't want my views to have to figure out where they go to next.

Update

I think I may not be understanding the correct way of building this. I saw each controller as taking care of a different section of the app, and having a main controller being responsible for workflow.

If this is not the approach I should be taking, what is the best way of doing this?

Update 2

Would Areas in ASP.NET MVC 2 take care of this sectioning of the app? I really don't like the idea of having too many actions in one controller...


In general:

Controllers are usually a collection of actions that deal with a logically coherent chunk of the application ( hence why you often see UserController / OrderController etc etc ).

MVC applications should be built using PRG ( post - redirect - get ) which means you will have 2 actions for each form, one that will display the form and the second, with the same name but decorated with [AcceptPost], that will process the form and redirect the user to the appropriate location based on the result.

The simplest way to learn how this works and migrate your app over is model each form as a simple dto with no logic, build a view for each form and 2 actions.

Once you have the logic working in the controller, you may want to migrate it out into some form of service that can be injected into the controller.

Specifically for your workflows:

Each workflow should probably have it's own controller. It may be useful to model them using some form of state pattern ( depending on the complexity of the workflow ), and providing a result from each state transition that your controller can translate into a redirect to the next step in the workflow.


When a form posts to a controller action it is the controller action to decide what to do with the posted results and which view to render next or redirect:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult HandleFormSubmission(FormCollection col)
{
    // do something with posted data
    // redirect to /someOtherController/someOtherAction
    // which could show some other form
    return RedirectToAction("someOtherAction", "someOtherController");
}


I was struggling with the same problem (using Windows Workflow Foundation with ASP.NET MVC) and I blogged about it here

Maybe you'll find it helpful, sorry for pushing my own link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜