So, idea was to have to listbox where one list box I will fill with data from MySQL, and in another with data from XML.
This is example is maybe not good, since I don't have model, I just wanted to play with autofac on a simplest possible way.
So here are my interfaces:
public interface IMyTree
{
List<string> Read(List<string> listMyElements);
}
public interface IReadMyTree
{
List<string> ReadXml();
List<string> ReadMySql();
}
Here is the class which will actually return
public class DoMytree : IMyTree
{
public List<string> Read(List<string> listMyElements)
{
return listMyElements;
}
}
Note that this class basically does nothing, that is because I don't have model, so I couldn't pass list of elements, for example, because structure is not same as structure from mySql.
Now, here is example of ReadMySql method:
public class MyReader : IReadMyTree
{
private readonly IMyTree _output;
public MyReader(IMyTree output)
{
this._output = output;
}
public List<string> ReadMySql()
{
string MyConString =
"SERVER=server;" +
"DATABASE=db;" +
"UID=user;" +
"PASSWORD=pass;Convert Zero Datetime=True";
string sql = "select * from slam_categories";
try
{
var connection = new MySqlConnection(MyConString);
var cmdSel = new MySqlCommand(sql, connection);
connection.Open();
MySqlDataReader dataReader = cmdSel.ExecuteReader();
var pom = new List<string>();
while (dataReader.Read())
{
object bugId = dataReader["title"];
pom.Add(bugId.ToString());
}
return _output.Read(pom);
}
catch (Exception e)
{
MessageBox.Show("Error: " + e);
}
return null;
}
}
Note line: return _output.Read(pom); little bit of nonsense I agree :) I just wanted to follow dependency injection... This example is just for learning, remember ;)
Interesting thing to notice are lines:
var connection = new MySqlConnection(MyConString);
var cmdSel = new MySqlCommand(sql, connection);
connection.Open();
MySqlDataReader dataReader = cmdSel.ExecuteReader();
var pom = new List<string>();
while (dataReader.Read())
{
object bugId = dataReader["title"];
pom.Add(bugId.ToString());
}
Here you can see how to fill list of strings from mySql, example how to connect to mySql you find here.
public class ReadMySqlTreeViewAutofacViewModel
{
public static List<string> ReadMySql()
{
var scope = MainWindow.Container.BeginLifetimeScope();
var writer = scope.Resolve<IReadMyTree>();
return writer.ReadMySql();
}
public List<string> TreeMySqlViewModels { get; set; }
public ReadMySqlTreeViewAutofacViewModel()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyReader>().As<IReadMyTree>();
builder.RegisterType<DoMytree>().As<IMyTree>();
MainWindow.Container = builder.Build();
TreeMySqlViewModels = ReadMySql();
}
}
XAML:
<Window x:Class="ReadXmlTreeViewAutofac.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:viewModel="clr-namespace:ReadXmlTreeViewAutofac.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModel:TreeViewModel x:Key="TreeViewModel" />
<viewModel:ReadMySqlTreeViewAutofacViewModel x:Key="ReadMySqlTreeViewAutofacViewModel" />
</Window.Resources>
<TabControl>
<TabItem Header="Read from Xml">
<Grid DataContext="{StaticResource TreeViewModel}" OverridesDefaultStyle="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TreeView Grid.Row="0" Grid.Column="0" Name="TvXml" ItemsSource="{Binding Path=TreeXmlViewModels}"/>
</Grid>
</TabItem>
<TabItem Header="Read from MySql">
<Grid OverridesDefaultStyle="True" DataContext="{StaticResource ReadMySqlTreeViewAutofacViewModel}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TreeView Grid.Row="0" Grid.Column="0" Name="TvMySql" ItemsSource="{Binding Path=TreeMySqlViewModels}"/>
</Grid>
</TabItem>
</TabControl>
</Window>
Things to notice in XAML are lines:
xmlns:viewModel="clr-namespace:ReadXmlTreeViewAutofac.ViewModel"
This is namespace which we will use, and lines:
<viewModel:TreeViewModel x:Key="TreeViewModel" />
<viewModel:ReadMySqlTreeViewAutofacViewModel x:Key="ReadMySqlTreeViewAutofacViewModel" />
These are classes which we will use to bind them with our trees, and finally tree binding:
<TreeView Grid.Row="0" Grid.Column="0" Name="TvXml" ItemsSource="{Binding Path=TreeXmlViewModels}"/>
Note that TreeXmlViewModels is property which I am registering it in autofac registration part, also note that I am preparing that property in line TreeXmlViewModels = ReadXml();
Example application you can download from here.
Github is here.