【问题标题】:Access RichtextBox from BusinessLayer and show text on it using c#从 BusinessLayer 访问 RichtextBox 并使用 c# 在其上显示文本
【发布时间】:2016-01-10 10:47:19
【问题描述】:

我根据软件架构创建了三个项目层-应用层,业务层和数据访问层。现在我想从应用层访问Richtextbox到业务逻辑层。在业务逻辑层中,我实现了一个WikipediaPerseCode来显示来自的短文本维基百科页面。我编写代码。但我不确定如何在应用层引用和显示文本。我正在尝试,但由于我是软件架构处理的新手,我不知道该怎么做。

我的应用层是这样的-

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 { 类短文本 {

    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";
            }
        }



      }


   }
 }

【问题讨论】:

  • 这就是所谓的“分层”:您将代码分层组织。层只允许调用底层。所以你会有 UI -> BL 调用,而你会反过来拒绝所有调用。这有助于保持复杂性并保持代码有点干净。另外,您可以在这里使用msdn.microsoft.com/en-us/library/ef2xyb33%28v=vs.110%29.aspx

标签: c# winforms architecture richtextbox layer


【解决方案1】:

我认为您正在寻找类似的东西:

表单的实现 表单使用 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 是负责请求 wiki 数据的类。我猜这就是您对业务逻辑的意思。

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 的代码,所以我没有更改您的代码。 此外,我删除了用于文件处理的代码。我不明白为什么您首先将数据放入文件中,然后将其从文件中读取到服务的响应中。如果确实需要,您可以将其再次添加到您的实现中。

分层架构的意义是将职责分开。因此,您可以重用现有实现或替换某些部分,而不会影响应用程序的其他部分。 您的应用程序很容易看出这种策略的巨大好处。

【讨论】:

  • 非常感谢您的回答。我会尝试并让你知道。
  • @MaximFleiting。非常感谢您的回答。它运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多