patterncsharpMinor
Populate tree from 3 different data sources and show its content
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.
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
- 1
ComboBoxat top - to choose datasource. (File System,XMLFile,SQLDB)
- 1
Buttonnext toComboBox- to select datasource path. (Or Connection String)
- 1
TreeViewControl at left - to show tree.
- 1
RichTextBoxto 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 : ITreeSourceNotice 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:
XMLTreeSourceshould beXmlTreeSource;
GetXMLDOMshould beGetXmlDom;
SQLTreeSourceshould beSqlTreeSource;
- 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 : ITreeSourcepublic 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.