I have a winforms application and I’m not following any kind of design pattern. My problem is, I have these base classes which contain all my business logic. When ever an exception occurs or I need to display a dialog box to the user, I have written the code directly into the base classes where I need it.
I know that I need to separate my business logic and display logic, so I wrote a static class that includes the methods that I need to display the messages with.
My question is, is there an easier way to separate business logic from the display?
my static methods look like this,
public static void DisplayMessage(string message)
{
MessageBox.Show(message);
}
public static bool DisplayDialogBox(string message,string caption )
{
DialogResult newresult = new DialogResult();
newresult = MessageBox.Show(message,caption,MessageBoxButtons.OKCancel);
if (newresult == DialogResult.OK)
{
return true;
}
else
{
return false;
}
So I’ll be calling these methods from the base classes, like
MsgDisplay.DisplayMessage(e.Message);
and is this method a good practice?
No, this method is not a good practice. There is no any difference between your class and MessageBox class.
See, with your class:
and just using MessageBox
This wrapper doesn’t provide any additional functionality.
If you want to separate business logic and ui, then you have to break down your methods and display message only in UI layer.
And, small point. Instead of:
type just:
UPDATE:
If all you want to display is exception message then you should catch exception and show message on UI layer. So in your business class instead of displaying message:
throw exception to the ui layer:
and then display the message outside of the business logic: