patterncsharpMinor
Best practices for decouple classes in C#
Viewed 0 times
practicesforclassesdecouplebest
Problem
So this is a lot more confusing than it has to be (I could just stick all of this in the main class with the ui event handlers) but I wanted to decouple this class for learning purposes.
Basic information:
I pulled out a bunch of code and put it in a separate class. This separate class opens files so I used exception handling. When an exception is thrown it should update the UI with an error message. To decouple this class I created an event handler and event listeners.
Questions:
-
Is this a common way to decouple classes?
-
Is this too loosely coupled where it creates too much overhead
-
Is this so decoupled that it makes it completely too complicated?
-
My friend suggested passing Form1 to the function, but I would still need to use the name of the label. So it would be less coupled, but not completely decoupled. Is this an acceptable approach?
-
Are there some other approaches that would work better?
Original class with UI event handlers:
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace compiler
{
public partial class Form1 : Form
{
CompilerControls controls = new CompilerControls();
bool ErrorFlag = false;
public Form1()
{
InitializeComponent();
//add an event listener to handle exceptions
controls.HandleException += new ExceptionCaught(CatchException);
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void CatchException(CustomEventArgs e)
{
UpdateStatus(e.Message, Color.Red);
ErrorFlag = true;
}
private void ctrlOpenFile_Click(object sender, EventArgs e)
{
DialogResult sourceFile = openFileDialog1.ShowDialog();
if (sourceFile == DialogResult.OK)
{
// Read the lines into a list from the file
controls.ReadFile(openFileDialog
Basic information:
I pulled out a bunch of code and put it in a separate class. This separate class opens files so I used exception handling. When an exception is thrown it should update the UI with an error message. To decouple this class I created an event handler and event listeners.
Questions:
-
Is this a common way to decouple classes?
-
Is this too loosely coupled where it creates too much overhead
-
Is this so decoupled that it makes it completely too complicated?
-
My friend suggested passing Form1 to the function, but I would still need to use the name of the label. So it would be less coupled, but not completely decoupled. Is this an acceptable approach?
-
Are there some other approaches that would work better?
Original class with UI event handlers:
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace compiler
{
public partial class Form1 : Form
{
CompilerControls controls = new CompilerControls();
bool ErrorFlag = false;
public Form1()
{
InitializeComponent();
//add an event listener to handle exceptions
controls.HandleException += new ExceptionCaught(CatchException);
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void CatchException(CustomEventArgs e)
{
UpdateStatus(e.Message, Color.Red);
ErrorFlag = true;
}
private void ctrlOpenFile_Click(object sender, EventArgs e)
{
DialogResult sourceFile = openFileDialog1.ShowDialog();
if (sourceFile == DialogResult.OK)
{
// Read the lines into a list from the file
controls.ReadFile(openFileDialog
Solution
Answer to question 5: you should use
Because strings are immutable, whenever you concatenate two strings you create a third. This will quickly ramp up the resources used, as you duplicate longer and longer strings.
So, for more than a handful of concatenations, you want to use StringBuilder. (For a few, it does not matter.)
Additionally, StringBuilder has methods to concatenate line-ends.
I find it deliciously ironic that lazyness can be an incentive for excellence. :)
Stringbuilder to create your strings.Because strings are immutable, whenever you concatenate two strings you create a third. This will quickly ramp up the resources used, as you duplicate longer and longer strings.
So, for more than a handful of concatenations, you want to use StringBuilder. (For a few, it does not matter.)
Additionally, StringBuilder has methods to concatenate line-ends.
I find it deliciously ironic that lazyness can be an incentive for excellence. :)
Context
StackExchange Code Review Q#11880, answer score: 5
Revisions (0)
No revisions yet.