HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Populate tree from 3 different data sources and show its content

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
showpopulatedifferentitscontentandfromdatasourcestree

Problem

The task is to build a WinForm app which populates a tree from 3 different data sources and shows its content.

  • 1 ComboBox at top - to choose datasource. (File System, XMLFile, SQLDB)



  • 1 Button next to ComboBox - to select datasource path. (Or Connection String)



  • 1 TreeView Control at left - to show tree.



  • 1 RichTextBox to show content. (in case of file system showing txt files content).



MainForm.cs:

```
public partial class MainForm : Form
{
private TreeProvider treeProvider;
private Action pathSelectFunction;

public MainForm()
{
InitializeComponent();
InitializeDataSourceComboBox();
}

private void InitializeDataSourceComboBox()
{
DataSourceComboBox.DataSource = ConfigurationManager.DataSourceTypes.dataSourceTypes;
DataSourceComboBox.ValueMember = "Type";
DataSourceComboBox.DisplayMember = "Name";
}

private void DataSourceComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
DataSourceType selItem = (DataSourceType)DataSourceComboBox.SelectedItem;
switch (selItem.Type)
{
case "XMLFile":
SelectDataSourcePathButton.Text = "Select XML File";
pathSelectFunction = SelectXMLFilePath;
break;
case "FileSystem":
SelectDataSourcePathButton.Text = "Select Root Folder";
pathSelectFunction = SelectRootFolderPath;
break;
case "SQLDB":
SelectDataSourcePathButton.Text = "Specify SQL connection string";
break;
default:
return;
}
}

private void SelectDataSourcePathButton_Click(object sender, EventArgs e)
{
pathSelectFunction();
}

private void SelectXMLFil

Solution


  • The usual whitespace/egyptian brackets mumbo-jumbo.



NodeBase nodeBase = new NodeBase() ;
    try{
        nodeBase.Identity = directory.Name;
        nodeBase.ParentIdentity = directory.Parent.FullName;


Should be:

NodeBase nodeBase = new NodeBase();

    try
    {
        nodeBase.Identity = directory.Name;
        nodeBase.ParentIdentity = directory.Parent.FullName;


Etc.

-
Did you mean for the internal classes?

class FileSystemTreeSource : ITreeSource
class SQLTreeSource : ITreeSource
public class XMLTreeSource : ITreeSource


Notice that XMLTreeSource is the only one that is public.

If you did want them to be internal, I suggest adding the internal keyword in front of them to be explicit about it.

  • Naming nitpicks:



  • XMLTreeSource should be XmlTreeSource;



  • GetXMLDOM should be GetXmlDom;



  • SQLTreeSource should be SqlTreeSource;



  • etc.



This is to fall in line with the best-practices of PascalCasing.

-
Your SQL should be parameterized queries instead. As it stands you are very open to SQL injection. (What would happen if I specified a tableName of Table; DROP TABLE Customers;? Not good things, I suggest.)

-
Usually I tend to favour immutability over mutability.

public string Identity { get; set; }
public string ParentIdentity { get; set; }
public string Content { get; set; }
public int ChildCount { get; set; }


Those set actions should be private.

Unless you absolutely have a need for mutability, you should try to favour immutability. It leaves you less vulnerable to problems with data validation, et al.

Code Snippets

NodeBase nodeBase = new NodeBase() ;
    try{
        nodeBase.Identity = directory.Name;
        nodeBase.ParentIdentity = directory.Parent.FullName;
NodeBase nodeBase = new NodeBase();

    try
    {
        nodeBase.Identity = directory.Name;
        nodeBase.ParentIdentity = directory.Parent.FullName;
class FileSystemTreeSource : ITreeSource
class SQLTreeSource : ITreeSource
public class XMLTreeSource : ITreeSource
public string Identity { get; set; }
public string ParentIdentity { get; set; }
public string Content { get; set; }
public int ChildCount { get; set; }

Context

StackExchange Code Review Q#98693, answer score: 4

Revisions (0)

No revisions yet.