Is it bad design to have a business logic object prompt the user for input?
I have a flow chart that I'm implementing and it has 4 or 5 paths through it depending on user input and the results of some processing. Naturally, I don't want all t开发者_JAVA百科his logic this in my Windows form, I just want to call a method on the class in the form. Is it bad design to have my business logic class reference System.Windows.Forms and show dialogs and MessageBoxes to get the input it needs to process and return a result?
Yes, this is bad design. Your class should offer a mean to communicate with the form and get data back. Just create events and let the Form
subscribe to them, getting the information to create the dialogs from a custom EventArgs
class. After it gets the input, just push the same class back with the additional information via a second event.
This should resemble the MVP pattern.
Yes, this is a bad idea. You are effectively coupling your business logic very tightly with the presentation. You (probably won't) be able to re-use business logic easily under other circumstances, and you won't be able to replace the UI without touching the business logic.
You need to have the UI and business logic layers communicate, and let the UI layer handle, well, the UI.
I think it's bad design. When you separate components of your application, a rule of thumb is to keep them separate enough so that you can run them on different computers.
Yes. Because it means your busienss object is simply not a business obejct.
Use a MVVM pattern and put the logic into the viewmodel.
It's bad design, because you may need to run your business logic in a situation with a different UI or no UI at all (such as in a server, or a batch process). That's what separation of business logic and UI is all about. If possible, it's better to get all the necessary user input up-front in the UI class before handing things off to the business logic. However, if it's necessary to have the business logic prompt for more information, then it's better for the business logic API to take a callback method delegate, which it can call to request the further input. Then the UI layer can decide how best to request from the user.
精彩评论