问题
I would like some advice on how to separate the UI and business logic in a simple C# Windows Forms Application.
Let's take this example:
The UI consists of a simple textbox and a button. The user enters a number between 0 and 9 and clicks the button. The program should add 10 to the number and update the text box with that value.
The business logic part should have no idea of the UI. How can this be accomplished?
Here's the empty Process class (Business Logic):
namespace addTen
{
class Process
{
public int AddTen(int num)
{
return num + 10;
}
}
}
The requirement is:
- When the user clicks the button, somehow, the Process::AddTen gets invoked.
- The Textbox must be updated with the return value of Process::AddTen.
I just don't know how to connect these two.
回答1:
First, you need to change your class name. "Process" is name of a class in the Class Library and will likely cause confusion for anyone reading your code.
Let's assume, for the rest of this answer that you changed the class name to MyProcessor (still a bad name, but not a well-known, often-used class.)
Also, you're missing the code to check to ensure that the user input is, indeed, a number between 0 and 9. That's appropriate in the Form's code rather than the class code.
- Assuming the TextBox is named textBox1 (The VS generated default for the first TextBox added to the form)
- Further assuming the button's name is button1
In Visual Studio, double-click on the button to create the button click event handler, which will look like this:
protected void button1_Click(object sender, EventArgs e)
{
}
Within the event handler, add code so it looks like this:
protected void button1_Click(object sender, EventArgs e)
{
int safelyConvertedValue = -1;
if(!System.Int32.TryParse(textBox1.Text, out safelyConvertedValue))
{
// The input is not a valid Integer value at all.
MessageBox.Show("You need to enter a number between 1 an 9");
// Abort processing.
return;
}
// If you made it this far, the TryParse function should have set the value of the
// the variable named safelyConvertedValue to the value entered in the TextBox.
// However, it may still be out of the allowable range of 0-9)
if(safelyConvertedValue < 0 || safelyConvertedValue > 9)
{
// The input is not within the specified range.
MessageBox.Show("You need to enter a number between 1 an 9");
// Abort processing.
return;
}
MyProcessor p = new MyProcessor();
textBox1.Text = p.AddTen(safelyConvertedValue).ToString();
}
The class, with the access modifier set properly, should look like this:
namespace addTen
{
public class MyProcessor
{
public int AddTen(int num)
{
return num + 10;
}
}
}
回答2:
Make your 'Process' class public (and as @DavidStratton says, change the name):
public class MyProcess
I would say you should parse your string
value from TextBox.Text
to an int
:
private void button1_Click(object sender, EventArgs e)
{
MyProcess myProcess = new MyProcess();
string result = textBox1.Text;
int number;
if(int.TryParse(textBox1.Text, out number))
{
result = myProcess.AddTen(number).ToString();
}
textBox1.Text = result;
}
回答3:
You can create another class called "Process.cs" for example. Methods that involve processing or data calculation you move there. In your case for example:
public class Process
{
public int AddTen(int num)
{
return num + 10;
}
}
Your UI click event will have a call to your "Process layer":
var myProcess = new Process();
//and then calculation
var initNumber = Convert.ToInt32(textBox.Text);
var calculatedValue = myProcess.AddTen(initNumber);
textBox.Text = calculatedValue.ToString();
This way your business logic, such as calculating is kept separately. If your UI changes you can still simply call myProcess.AddTen() method whether it's a web, Windows or a Mobile form.
回答4:
To separate the logic completely you can declare a base class that can contain the button and manage handlers. Your specific process can inherit the base class, and the logic can be set. Finally the form can declares an instance of the class and pass in the button.
It looks something like this:
class BaseProcessor
{
System.Windows.Forms.Button myButton;
public System.Windows.Forms.Button MyButton
{
get
{
return myButton;
}
set
{
myButton = value;
myButton.Click += new System.EventHandler(this.MyButton_Click);
}
}
public BaseProcessor()
{
}
public virtual void MyButton_Click(object sender, EventArgs e)
{
}
Then declare the process:
class MyProcess : BaseProcessor
{
public override void MyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("This is my process");
}
}
Then inside the form, declare an instance of the process and attach the button:
public partial class Form1 : Form
{
MyProcess myProcess = null;
public Form1()
{
InitializeComponent();
myProcess = new MyProcess
{
MyButton = button1
};
}
}
Using this method, there is no business logic code in the form. The parent class is useful because events like clicking buttons are pretty common so its easier to declare them centrally, in my opinion.
来源:https://stackoverflow.com/questions/11547438/c-sharp-windows-forms-app-separate-gui-from-business-logic