0

So, I really like the idea of separating business logic with UI code (MVC or MVVM). But let's say that in an application some event happens (in business logic) and it needs user input (dialog) to continue.

For example if you have a simulation about money or stocks of some sort, and in your model you do some calculations and suddenly the stock prices are quite high and you want to ask the user to sell them ("yes-no" dialog). What is a good way to go about this? (I'm not making such an application, it's just an example, so please answer abstractly).

pseudo code:

update() {
    ...
    if(stock->price > Stock::ADVICE_BUY_PRICE) {
        showDialog("..."); // !! AVOID UI LOGIC IN MODEL !!
    }
    ...
}

This question has probably been asked already, but I haven't found a good answer yet. Just mark answer as duplicate if there already is a viable answer.

melledijkstra
  • 361
  • 3
  • 17
  • 1
    Look at it from another perspective: The domain object (model) has no knowledge about the external world. It's agnostic to users/user interactions. If some action - like your dialog display - should take place, then another entity should run it. That other entity can read/change model state(s) and make, for example, display/request input decisions. –  Jun 28 '17 at 12:44
  • @aendeerei Thanks for your answer. I get your perspective, and it should be like that. I've done some more research and I think that some sort of event system is the best (Observer pattern). And just notify any listeners to when the event happens and let them choose how to respond. Maybe I'll post an answer when I got something concrete working. – melledijkstra Jun 28 '17 at 13:17
  • 1
    You are welcome. If observer fits to your goals, it's perfect. Good luck! –  Jun 28 '17 at 13:51
  • There are TONS of questions like this on here. There is no one solution. It usually involves IoC serving an implementation of an interface by which business logic can query for whatever from whoever. –  Jun 28 '17 at 16:46
  • 1
    @Will yeah, saw that post already, but that post is really targeted to WPF and didn't really help. For me it's just in a global sense. But I'll confirm it as duplicate. – melledijkstra Jun 28 '17 at 17:32

0 Answers0