2016-01-10 21 views
0

根據軟件體系結構創建三個Project層--Application層,Business laer和DataAccess層。現在我想從應用程序層訪問Richtextbox到業務邏輯層。層我實現了一個WikipediaPerseCode來顯示來自維基百科page的短文本。我編寫代碼。但我不確定如何引用和顯示應用程序層中的文本。我在嘗試,但由於我是softawre架構處理中的新手,我不知道該怎麼做。從BusinessLayer訪問RichtextBox並使用c顯示文本#

我的應用層是喜歡 -

namespace TouristPlace 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     ShortText.txt1 = richTextBox1; 
    } 

    public string SetText1 
    { 
     get { return richTextBox1.Text; } 
     set { richTextBox1.Text = value; } 
    } 


    } 
} 

我的短文本是 - 命名空間WikiPerser { 類ShortText {

public static RichTextBox txt1 = new RichTextBox(); 

    public static void shortText(string name) 
    { 
     using (WebClient wc = new WebClient()) 
     { 

      var startPath = Application.StartupPath; 
      //var spath = Path.Combine(startPath,@"\ShortText\"); 
      string folderName = Path.Combine(startPath, "Short Text"); 
      System.IO.Directory.CreateDirectory(folderName); 

      string fileName = name + ".txt"; 

      var path = Path.Combine(folderName, fileName); 



      var client = new WebClient(); 

      var response = client.DownloadString("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=" + name + "&redirects="); 

      var responseJson = JsonConvert.DeserializeObject<RootObject>(response); 
      var firstKey = responseJson.query.pages.First().Key; 
      var extract = responseJson.query.pages[firstKey].extract; 

      try 
      { 
       Regex regex = new Regex(@".(?<=\()[^()]*(?=\)).(.)"); 
       string.Format("Before:{0}", extract); 
       extract = regex.Replace(extract, string.Empty); 
       string result1 = String.Format(extract); 
       result1 = Regex.Replace(result1, @"\\n", " "); 

       //richTextBox1.Text = result; 
       txt1.Text = extract; 
       File.WriteAllText(path, txt1.Text); 
      } 


      catch (Exception) 
      { 
       txt1.Text = "Error"; 
      } 
     } 



     } 


    } 
} 
+0

這就是所謂的「分層」:你組織代碼層。層只允許調用底層。所以你會有UI - > BL的電話,你會反過來拒絕所有的電話。這有助於保持複雜性並保持代碼的清潔。另外,您可以在這裏使用https://msdn.microsoft.com/en-us/library/ef2xyb33%28v=vs.110%29.aspx。 – mikalai

回答

1

我想你尋找類似業務邏輯層即:

Implemen表單 形式的塔季翁消耗ShortTextService這是您的WikiParser的服務(我瞭解到目前爲止)

public partial class Form1 
     : Form 
    { 
     private readonly ShortTextService _shortTextService; 

     public Form1() 
     { 
      _shortTextService = new ShortTextService(); 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      richTextBox1.Text = _shortTextService.GetShortText(NameTextBox.Text);//here NameTextBox is input for the name 
     } 
    } 

ShortTextService是類,這是負責維基數據的請求。我猜這就是您用Business Logic的意思。

ShortTextService實現

public class ShortTextService 
{ 
    private string _baseUrl = 
     "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles={0}&redirects="; 

    public string GetShortText(string name) 
    { 
     string requestUrl = string.Format(_baseUrl, name); 
     string result; 

     using (WebClient client = new WebClient()) 
     { 
      try 
      { 
       string response = client.DownloadString(requestUrl); 

       RootObject responseJson = JsonConvert.DeserializeObject<RootObject>(response); 

       var firstKey = responseJson.query.pages.First().Key; 
       var extract = responseJson.query.pages[firstKey].extract; 

       Regex regex = new Regex(@".(?<=\()[^()]*(?=\)).(.)"); 
       extract = regex.Replace(extract, string.Empty); 
       result = Regex.Replace(extract, @"\\n", " "); 
      } 
      catch (Exception) 
      { 
       result = "Error"; 
       //handle exception here. E.g Logging 
      } 
     } 
     return result; 
    } 
} 

我沒有代碼RequestObject所以我離開你的代碼不變。 此外我刪除了文件處理的代碼。我沒有明白爲什麼你首先把數據放到文件中,而不是從文件中讀入服務的響應。如果確實需要,可以將其添加到實施中。

分層架構的意義在於區分責任區域。因此,您可以重新使用現有的實現或替換某些部分,而不會影響應用程序的其他部分。 您的應用程序很容易看到此策略的巨大優勢。

+0

非常感謝您的回答。我會嘗試並讓你知道。 – Nowshin

+0

@MaximFleiting。非常感謝您的回答。它工作完美。 – Nowshin

相關問題