D
D
ddd3292017-09-23 06:37:11
C++ / C#
ddd329, 2017-09-23 06:37:11

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

4 answer(s)
M
MrDywar Pichugin, 2017-09-23
@Dywar

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           -                      +                    +

Everything is available in the first table.
In the second already depending on the selected element from the first.
In the third table, multiplying the first table by the second gives all possible combinations against which access to elements is indicated.
In other words, work with sets.
1st available in its entirety.
The 2nd is multiplied by the first, and the solution is given at the intersection.
The 3rd is multiplied by the first and second sets, the solution is indicated at the intersection.

P
Ptolemy_master, 2017-09-23
@Ptolemy_master

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.

S
Stanislav Makarov, 2017-10-14
@Nipheris

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).

A
Alexander, 2017-12-12
@alexr64

There are a lot of if's in the method, which check whether the values ​​are selected, if so, which ones.

switch(combobox1)
{
case "1":
enable_someOne_in_combobox2();
break;
case "2":
enable_someTwo_in_combobox2();
break;
}
switch(combobox2)
{
case "1":
enable_someOne_in_combobox3();
break;
case "2":
enable_someTwo_in_combobox3();
break;
}
switch(combobox3)
{
case "1":
doAction1();
break;
case "2":
doAction2();
break;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question