How to structure the code for the next / previous step in a complex wizard?

I have a 45-step user interface (complex domain - insurance). It is implemented in ASP.NET MVC 3. The problem is that my action methods contain a lot of code to determine what the next step, depending on the user, already collected data, weather forecast ... And even worse, similar code repeated for back button functionality. Is there a pattern that I can use to abstract this logic? I hope for a way to identify steps forward and automatically jump back.

+4
source share
1 answer

My suggestion is to create an interface as a class:

public interface IStepDecisionMaker { StepInfo DefineNextPreviousStep(object data); } public class StepInfo { public int NextStepId {get;set;} public int PreviousStepId {get;set;} public object Values {get;set;} } 

Now you can add a table to the database or XML, where you install all the available steps and assign a specific class to each step that implements the IStepDecisionMaker interface

In your action, you can collect step data and using the IoC system or CreateInstance reflection, you can create an instance of a specific class, call DefineNextPreviousStep, passing in the data you need to check, and the method can return a class that tells you what the next or previous steps are

This way you can achieve several things.

  • your action will have only a few lines of code
  • logic is not a ton if then, but inside each class
  • You can change some conditions directly in a particular class of a particular step without touching another part of the code.

IMHO

0
source

Source: https://habr.com/ru/post/1447821/


All Articles