2012-07-18 60 views
8

我想就如何在簡單的C#Windows窗體應用程序中分離UI和業務邏輯提供一些建議。C#Windows窗體應用程序:從業務邏輯中分離GUI

讓我們這個例子:

的UI由一個簡單的文本框和按鈕。用戶輸入介於0和9之間的數字並單擊該按鈕。該程序應該將數字加10,並用該值更新文本框。

enter image description here

業務邏輯部分應該沒有UI的想法。這如何實現?

下面是空的Process類(業務邏輯):

namespace addTen 
{ 
    class Process 
    { 
     public int AddTen(int num) 
     { 
      return num + 10; 
     } 
    } 
} 

的要求是:

  1. 當用戶點擊該按鈕時,不知何故,過程:: AddTen被調用。
  2. 必須使用Process :: AddTen的返回值更新文本框。

我只是不知道如何連接這兩個。

回答

6

首先,您需要更改您的班級名稱。 「過程」是類庫中類的名稱,可能會導致任何人閱讀代碼時出現混淆。

假設,對於這個答案,你改變了類名MyProcessor的其餘部分(仍然是一個不好的名字,但沒有一個知名的,經常使用的類)。

而且,你」重新丟失了要檢查的代碼,以確保用戶輸入實際上是介於0和9之間的數字。這適用於表單代碼而不是類代碼。

  • 假設文本框被命名爲textBox1的(該VS生成的默認用於第一文本框添加到表格)
  • 進一步假設該按鈕的名稱BUTTON1

在Visual Studio中,雙擊按鈕來創建按鈕單擊事件處理程序,這將是這樣的:

protected void button1_Click(object sender, EventArgs e) 
{ 

} 

在事件處理程序中添加代碼,以便它看起來像這樣:

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(); 
} 

類,設置適當的訪問修飾符,應該是這樣的:

namespace addTen  
{  
    public class MyProcessor 
    {  
     public int AddTen(int num)  
     {  
      return num + 10;  
     }  
    }  
}  
+2

不要過多地混淆水域,但你也可以改變AddTen函數來接受一個字符串輸入,並在該函數中進行驗證。這實際上是我的偏好,並會將更多業務邏輯移出表單,但無論哪種方式都可行。 – David 2012-07-18 18:18:01

+0

抱歉關於所有編輯。我應該在Visual Studio中完成它。我會馬上發現所有的編譯和邏輯錯誤。 – David 2012-07-18 18:19:11

2

讓你的 '過程' 級的公共(和@DavidStratton說,更改名稱) :

public class MyProcess 

我會說,你應該從TextBox.Text分析你string值至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; 
} 
+0

我忘了在回答中公開Process類。一個好的捕獲+1。 – David 2012-07-18 18:03:53

1

您可以創建一個名爲 「Process.cs」 例如另一個類。 涉及您在那裏移動的處理或數據計算的方法。你的情況,例如:

public class Process 
{ 
public int AddTen(int num) 
{ 
    return num + 10; 
} 
} 

你的UI單擊事件將調用你的「過程層」:

var myProcess = new Process(); 
    //and then calculation 
    var initNumber = Convert.ToInt32(textBox.Text); 
    var calculatedValue = myProcess.AddTen(initNumber); 

    textBox.Text = calculatedValue.ToString(); 

這樣你的業務邏輯,如計算是分開存放。如果您的用戶界面發生變化,您仍然可以簡單地調用myProcess.AddTen()方法,無論它是Web,Windows還是Mobile表單。

相關問題