Reputation: 67
I create three Project layer -Application layer, Business laer and DataAccess layer according to software architecture.Now I want to access Richtextbox from application layer to Business Logic layer.in business logic layer I implemented a WikipediaPerseCode to show the short text from Wikipedia page.I write the code. But I am not sure how to reference and show the text in application layer.I am trying, But as I am new in softawre architecture handling ,I do not know how to do it.
my Application layer is like-
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; }
}
}
}
My Business Logic layer for short text is- namespace WikiPerser { class 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";
}
}
}
}
}
Upvotes: 0
Views: 77
Reputation: 562
I think you looking for something like that :
Implementation of the Form
Form consume services of the ShortTextService
which is your WikiParser(as I understood so far)
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 is class which is responsible for request of the wiki data. This is what you mean with Business Logic, I guess.
ShortTextService implementation:
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;
}
}
I didn't have code for RequestObject so I left your code unchanged. Additionally I removed code for file handling. I didn't get why you first put the data to the file and than read it from the file into the response of the service. If it really needed you can add it again into your implementation.
Meaning of the Layered Architecture is to separate areas of the responsibilities. So you could reuse your existing implementation or replace some parts without impact on other parts of the application. Your application is quite simple to see the big benefit of this strategy.
Upvotes: 1