Answer the question
In order to leave comments, you need to log in
How to organize complex business logic?
Hi friends! I am writing a program and I seem to be stuck on simple things: there is a form where four controls are placed, the first three are a combobox, and the last is a text box (textbox). What you need: when you select a value in the first combobox, the second one displays a list associated with the first value, respectively, when the value changes in the second combobox, the list associated with the second value is displayed in the third combobox. Those. it turns out such a tree structure, but there are no problems in its display. The problem is that when choosing different values, different actions should occur.
For example:
If the first combobox has "Value_1" and the second combobox has "Value_5", then the third combobox must be disabled with the value "Value_7" selected and the textbox contains the value calculated by the "Algorithm_23" algorithm, which cannot be changed manually.
Those. it turns out that when choosing different values, not only the UI should change, but also the rules for calculating the algorithm. The algorithms here are not of the same type, which can be abstracted by the STRATEGY pattern, and each has its own dependencies (repositories, etc.).
The UI is implemented like this: View works with Presenter, and for example, when selecting a value from the list, it calls its method. There are a lot of if's in the method, which check whether the values are selected, if so, which ones. There are a lot of combinations of all values and actions connected with them. Actions concern not only changes in the displayed information in the UI, but also calculations in the business area. Since the work of the method depends on the state, I tried to apply the state pattern - somehow I didn’t really like it, a lot of actions are repeated and I have to come up with names for each state, for example, what name to give this state when the values are selected: "Value_1", "Value_5" and "Value_7"...
I've seen programs where there are about 20 controls on the form, and all of them are "intertwined"...
Answer the question
In order to leave comments, you need to log in
Table of possible values.
Таблица значений ComboBox_1. Всегда доступно все.
---------------------------------------------
ComboBox_1
CB1_Item_01
CB1_Item_02
CB1_Item_03
...
---------------------------------------------
Таблица значений ComboBox_2. Доступно то что со знаком "+", зависит от выбранного CB1_Item_хх.
---------------------------------------------
- CB2_Item_01 CB2_Item_02 CB2_Item_03
CB1_Item_01 + + +
CB1_Item_02 - + +
CB1_Item_03 - + +
...
Таблица значений ComboBox_3
---------------------------------------------
- - CB3_Item_01 CB3_Item_02 CB3_Item_03
CB1_Item_01 CB2_Item_01 + + +
CB1_Item_01 CB2_Item_02 - + +
CB1_Item_01 CB2_Item_03 - + +
...
CB1_Item_02 CB2_Item_02 - + +
CB1_Item_02 CB2_Item_03 - + +
Try to move away from imperative logic to declarative.
Let's say a set of rules is an array of some objects, in the fields of which you set some formulas and links to other objects whose data must be used. And separately there is some logic that reads these objects, reads interface elements and, in accordance with them, makes branches and calculations. It is enough to set the general rules for reading an object and generating actions once, and then you only have to set your own rules in an array.
This approach is called reactive programming . and implemented in Rx.NET and ReactiveUI libraries . It will not make business logic easier for you, but it will help to organize dependencies between which values are calculated and how (thanks to the declarative approach to describing dependencies).
There are a lot of if's in the method, which check whether the values are selected, if so, which ones.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question