Case: When your DB tanle name conventions and C# class name conventions are different,then to create map between these two, we can create conventions.
Step 1: Create a domain level attribute to define DB table for each C# class we need to map.
public class MyAttribute :Attribute
{
public MyAttribute(string tableName)
{
TableName=tableName;
}
public string TableName{get;set;}
}
step 2: Declare TableName attribute on domain object
[MyAttribute("Employee_Details")]
public class Employee
{
public virtual string Name{get;set;}
public virtual long SSN{get;set;}
}
step3: Create convention
public class TableNameConvention : IClassConvention
step 4: Hook convention to NH configuration
var configuration=AutoMap.AssemblyOf<Employee>()
.conventions.Add(typeof(TableNameConvention))
{
public void Apply(IClassInstance instance)
{
instance.table(SetTableName(instance));
}
private string SetTableName(IClassInstance instance)
{
var attributes = instance.EntityType.GetCustomAttributes(false);
foreach(var attribute in attributes)
{
if(attribute is MyAttribute)
{
return ((MyAttribute)attribute).TableName;
}
}
return instance.EntityType.Name;
}
}
}
Wednesday, December 15, 2010
Wednesday, March 31, 2010
WPF TreeView with Custom Object Collections
XAML:
<Window x:Class="MyTreeView.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:MyTreeView"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid Width="281">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.Resources>
<my:ListLeague x:Key="MyList"/>
<HierarchicalDataTemplate DataType = "{x:Type my:League}"
ItemsSource = "{Binding Path=Divisions}">
<TextBlock Text="{Binding Path=LeagueName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType= "{x:Type my:Division}"
ItemsSource = "{Binding Path=Teams}">
<TextBlock Text="{Binding Path=DivisionName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType= "{x:Type my:Team}"
ItemsSource = "{Binding Path=Players}">
<TextBlock Text="{Binding Path=TeamName}"/>
</HierarchicalDataTemplate>
<!--<DataTemplate DataType="{x:Type my:Player}">
<TextBlock Text="{Binding Path=FName}"/>
</DataTemplate>-->
</Grid.Resources>
<TreeView x:Name="tvLeague" Grid.Row="0" SelectedItemChanged="tvLeague_SelectedItemChanged">
<!-- <TreeViewItem ItemsSource="{Binding Source={StaticResource MyList}}" Header="My Soccer Leagues" />-->
</TreeView>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Button Width="80" Height="30" Click="Button_Click" Content="Add Zone" />
<Button Width="80" Height="30" Click="Button_Click_1" Content="Add Team"/>
</StackPanel>
</Grid>
</Window>
Code :
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace MyTreeView
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private ObservableCollection<League> lstLeague = new ObservableCollection<League>();
public League CurrentLeague { get; set; }
public Division CurrentDivision { get; set; }
public Team CurrentTeam { get; set; }
private int j = 0;
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
lstLeague.ToList().Find(i => i.LeagueName == CurrentLeague.LeagueName).
Divisions.Add(new Division { DivisionName = string.Format("New Zone {0}", j++), Teams = new ObservableCollection<Team>() });
}
private void tvLeague_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (((TreeView)sender).SelectedItem.GetType().Equals(typeof(League)))
{
League league = ((TreeView)sender).SelectedItem as League;
CurrentLeague = league;
}
else if (((TreeView)sender).SelectedItem.GetType().Equals(typeof(Division)))
{
Division division = ((TreeView)sender).SelectedItem as Division;
CurrentDivision = division;
}
else if (((TreeView)sender).SelectedItem.GetType().Equals(typeof(Team)))
{
Team team = ((TreeView)sender).SelectedItem as Team;
CurrentTeam = team;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
League l;
Division d;
Division d1; Division d2; Division d3; Team t1; Team t2; Team t3; Team t4;
Player p1; Player p2; Player p3; Player p4;
lstLeague.Add(l = new League { LeagueName = "IPL", Divisions = new ObservableCollection<Division>() });
d = new Division { DivisionName = "EAST", Teams = new ObservableCollection<Team>() };
d1 = new Division { DivisionName = "WEST", Teams = new ObservableCollection<Team>() };
d2 = new Division { DivisionName = "SOUTH", Teams = new ObservableCollection<Team>() };
d3 = new Division { DivisionName = "NORTH", Teams = new ObservableCollection<Team>() };
l.Divisions.Add(d1);
l.Divisions.Add(d2);
l.Divisions.Add(d3);
l.Divisions.Add(d);
t1 = new Team { TeamName = "MI", Players = new ObservableCollection<Player>() };
t2 = new Team { TeamName = "KKR", Players = new ObservableCollection<Player>() };
t3 = new Team { TeamName = "RR", Players = new ObservableCollection<Player>() };
t4 = new Team { TeamName = "CS", Players = new ObservableCollection<Player>() };
d.Teams.Add(t1);
d1.Teams.Add(t2);
d2.Teams.Add(t3);
d3.Teams.Add(t4);
p1 = new Player { FName = "P1", LName = "p1" };
p2 = new Player { FName = "P1", LName = "p1" };
p3 = new Player { FName = "P1", LName = "p1" };
p4 = new Player { FName = "P1", LName = "p1" };
t1.Players.Add(p1);
t2.Players.Add(p2);
t3.Players.Add(p3);
t4.Players.Add(p4);
lstLeague.Add(l = new League { LeagueName = "ICL", Divisions = new ObservableCollection<Division>() });
d = new Division { DivisionName = "EAST", Teams = new ObservableCollection<Team>() };
d1 = new Division { DivisionName = "WEST", Teams = new ObservableCollection<Team>() };
d2 = new Division { DivisionName = "SOUTH", Teams = new ObservableCollection<Team>() };
d3 = new Division { DivisionName = "NORTH", Teams = new ObservableCollection<Team>() };
l.Divisions.Add(d1);
l.Divisions.Add(d2);
l.Divisions.Add(d3);
l.Divisions.Add(d);
t1 = new Team { TeamName = "MI", Players = new ObservableCollection<Player>() };
t2 = new Team { TeamName = "KKR", Players = new ObservableCollection<Player>() };
t3 = new Team { TeamName = "RR", Players = new ObservableCollection<Player>() };
t4 = new Team { TeamName = "CS", Players = new ObservableCollection<Player>() };
d.Teams.Add(t1);
d1.Teams.Add(t2);
d2.Teams.Add(t3);
d3.Teams.Add(t4);
p1 = new Player { FName = "P1", LName = "p1" };
p2 = new Player { FName = "P1", LName = "p1" };
p3 = new Player { FName = "P1", LName = "p1" };
p4 = new Player { FName = "P1", LName = "p1" };
t1.Players.Add(p1);
t2.Players.Add(p2);
t3.Players.Add(p3);
t4.Players.Add(p4);
tvLeague.ItemsSource = lstLeague;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
lstLeague.ToList().Find(i=>i.LeagueName==CurrentLeague.LeagueName).Divisions.ToList().Find(i=>i.DivisionName==CurrentDivision.DivisionName).
Teams.Add(new Team{TeamName=string.Format("New Team {0}",j++),Players=new ObservableCollection<Player>()});
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
}
}
public class League
{
public League()
{
}
public string LeagueName { get; set; }
public ObservableCollection<Division> Divisions { get; set; }
}
public class Division
{
public Division()
{
}
public string DivisionName { get; set; }
public ObservableCollection<Team> Teams { get; set; }
}
public class Team
{
public Team() { }
public string TeamName { get; set; }
public ObservableCollection<Player> Players { get; set; }
}
public class Player
{
public Player() { }
public string FName { get; set; }
public string LName { get; set; }
}
public class ListLeague : ObservableCollection<League>
{
public ListLeague()
{
League l;
Division d;
Division d1; Division d2; Division d3; Team t1; Team t2; Team t3; Team t4;
Player p1; Player p2; Player p3; Player p4;
Add(l = new League { LeagueName = "IPL" ,Divisions=new ObservableCollection<Division>()});
d = new Division { DivisionName = "EAST" ,Teams=new ObservableCollection<Team>()};
d1 = new Division { DivisionName = "WEST", Teams = new ObservableCollection<Team>() };
d2 = new Division { DivisionName = "SOUTH", Teams = new ObservableCollection<Team>() };
d3 = new Division { DivisionName = "NORTH", Teams = new ObservableCollection<Team>() };
l.Divisions.Add(d1);
l.Divisions.Add(d2);
l.Divisions.Add(d3);
l.Divisions.Add(d);
t1 = new Team { TeamName = "MI",Players=new ObservableCollection<Player>() };
t2 = new Team { TeamName = "KKR", Players = new ObservableCollection<Player>() };
t3 = new Team { TeamName = "RR", Players = new ObservableCollection<Player>() };
t4 = new Team { TeamName = "CS", Players = new ObservableCollection<Player>() };
d.Teams.Add(t1);
d1.Teams.Add(t2);
d2.Teams.Add(t3);
d3.Teams.Add(t4);
p1 = new Player { FName = "P1", LName = "p1" };
p2 = new Player { FName = "P1", LName = "p1" };
p3 = new Player { FName = "P1", LName = "p1" };
p4 = new Player { FName = "P1", LName = "p1" };
t1.Players.Add(p1);
t2.Players.Add(p2);
t3.Players.Add(p3);
t4.Players.Add(p4);
Add(l = new League { LeagueName = "ICL", Divisions = new ObservableCollection<Division>() });
d = new Division { DivisionName = "EAST", Teams = new ObservableCollection<Team>() };
d1 = new Division { DivisionName = "WEST", Teams = new ObservableCollection<Team>() };
d2 = new Division { DivisionName = "SOUTH", Teams = new ObservableCollection<Team>() };
d3 = new Division { DivisionName = "NORTH", Teams = new ObservableCollection<Team>() };
l.Divisions.Add(d1);
l.Divisions.Add(d2);
l.Divisions.Add(d3);
l.Divisions.Add(d);
t1 = new Team { TeamName = "MI", Players = new ObservableCollection<Player>() };
t2 = new Team { TeamName = "KKR", Players = new ObservableCollection<Player>() };
t3 = new Team { TeamName = "RR", Players = new ObservableCollection<Player>() };
t4 = new Team { TeamName = "CS", Players = new ObservableCollection<Player>() };
d.Teams.Add(t1);
d1.Teams.Add(t2);
d2.Teams.Add(t3);
d3.Teams.Add(t4);
p1 = new Player { FName = "P1", LName = "p1" };
p2 = new Player { FName = "P1", LName = "p1" };
p3 = new Player { FName = "P1", LName = "p1" };
p4 = new Player { FName = "P1", LName = "p1" };
t1.Players.Add(p1);
t2.Players.Add(p2);
t3.Players.Add(p3);
t4.Players.Add(p4);
}
public League this[string name]
{
get
{
foreach (League l in this)
if (l.LeagueName == name)
return l;
return null;
}
}
}
}
Thursday, March 25, 2010
WPF TreeView
XAML
<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:WpfApplication3"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<HierarchicalDataTemplate DataType="{x:Type data:Root}" ItemsSource="{Binding Path=WebPages}">
<StackPanel>
<TextBlock Text="{Binding Path=Title}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type data:WebPage}" ItemsSource="{Binding Path=LinksInPage}" >
<TextBlock Text="{Binding Path=PageTitle}"></TextBlock>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Margin="0,0,15,0" Name="treeView1" Background="LightSkyBlue">
<TreeViewItem ItemsSource="{Binding Path=WebPages}"
Header="{Binding Title}"></TreeViewItem>
</TreeView>
</Grid>
</Window>
<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:WpfApplication3"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<HierarchicalDataTemplate DataType="{x:Type data:Root}" ItemsSource="{Binding Path=WebPages}">
<StackPanel>
<TextBlock Text="{Binding Path=Title}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type data:WebPage}" ItemsSource="{Binding Path=LinksInPage}" >
<TextBlock Text="{Binding Path=PageTitle}"></TextBlock>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Margin="0,0,15,0" Name="treeView1" Background="LightSkyBlue">
<TreeViewItem ItemsSource="{Binding Path=WebPages}"
Header="{Binding Title}"></TreeViewItem>
</TreeView>
</Grid>
</Window>
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Root r = new Root();
r.WebPages = new List<WebPage>();
r.Title = "HomePage";
r.Url = @"http://www.HomePage.com";
WebPage link1 = new WebPage();
link1.Href = @"http://www.HomePage.com/link1";
link1.PageTitle = "link1";
r.WebPages.Add(link1);
WebPage Link2 = new WebPage();
Link2.Href = @"http://www.HomePage.com/Link2";
Link2.PageTitle = "Link2";
r.WebPages.Add(Link2);
WebPage Link3 = new WebPage();
Link3.Href = @"http://www.HomePage.com/Link2/Link3";
Link3.PageTitle = "Link3";
Link2.LinksInPage = new List<WebPage>();
Link2.LinksInPage.Add(Link3);
WebPage Link4 = new WebPage();
Link4.Href = @"http://www.HomePage.com/Link2/Link4";
Link4.PageTitle = "Link4";
Link2.LinksInPage.Add(Link4);
WebPage Link5 = new WebPage();
Link5.Href = @"http://www.HomePage.com/link1/Link5";
Link5.PageTitle = "Link5";
link1.LinksInPage = new List<WebPage>();
link1.LinksInPage.Add(Link5);
WebPage Link6 = new WebPage();
Link6.Href = @"http://www.HomePage.com/link2/Link3/Link6";
Link6.PageTitle = "Link6";
Link3.LinksInPage = new List<WebPage>();
Link3.LinksInPage.Add(Link6);
treeView1.DataContext = r;
}
}
public class WebPage
{
public string Href { get; set; }
public string PageTitle { get; set; }
public List<WebPage> LinksInPage { get; set; }
}
public class Root
{
public string Title { get; set; }
public string Url { get; set; }
public List<WebPage> WebPages { get; set; }
}
}
Wednesday, March 24, 2010
WPF DataGrid Binding With Enum and Child Collection
<Window x:Class="SOTC_DataGridExample.Window1" Name="This" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:SOTC_DataGridExample" Title="DataGrid Example" Height="300" Width="500"> <Window.Resources> <ObjectDataProvider x:Key="myEnum" ObjectType="{x:Type sys:Enum}" MethodName="GetValues"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="local:GenderType"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <ObjectDataProvider x:Key="myContinentEnum" ObjectType="{x:Type sys:Enum}" MethodName="GetValues"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="local:Continent"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <Grid x:Name="dgRoot" DataContext="{Binding}"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <dg:DataGrid x:Name="myGrid" ItemsSource="{Binding}" Grid.Row="0" Grid.Column="0" IsSynchronizedWithCurrentItem="True" Margin="5" AutoGenerateColumns="False" Width="365"> <dg:DataGrid.Columns> <dg:DataGridTextColumn Binding="{Binding GameName}" Header="Game Name" /> <dg:DataGridTextColumn Binding="{Binding Creator}" Header="Creator" /> <dg:DataGridComboBoxColumn Header="Gender" ItemsSource="{Binding Source={StaticResource myEnum}}" SelectedItemBinding="{Binding Gender}" /> <dg:DataGridComboBoxColumn Header="Publisher" x:Name="_PublisherCombo" SelectedItemBinding="{Binding Publisher}" /> <dg:DataGridCheckBoxColumn Binding="{Binding OnXbox}" Header="On Xbox 360" /> </dg:DataGrid.Columns> </dg:DataGrid> <dg:DataGrid x:Name="CountryGrid" Grid.Row="1" Grid.Column="0" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=CurrentItem.CityInfo}" AutoGenerateColumns="False" Width="195"> <dg:DataGrid.Columns> <dg:DataGridTextColumn Binding="{Binding City}" Header="City" /> <dg:DataGridTextColumn Binding="{Binding Country}" Header="Country" /> <dg:DataGridComboBoxColumn Header="Continent" ItemsSource="{Binding Source={StaticResource myContinentEnum}}" SelectedItemBinding="{Binding CountryIn}" /> </dg:DataGrid.Columns> </dg:DataGrid> </Grid> </Window>
Code Behind:
using System; using System.Collections.Generic; using System.Data; using System.Windows; using System.Collections.ObjectModel; namespace SOTC_DataGridExample { public enum GenderType { Male, Female }; public enum Continent{NorthAmerica,Asia,Europe,Australia}; public partial class Window1 : Window { private DataTable _GameData; public Window1() { List<GameData> _GameData = new List<GameData>(); List<CityInfo> _cityInfo = new List<CityInfo>(); _cityInfo.Add(new CityInfo() { City = "Mumbai", Country = "India", CountryIn = Continent.Asia }); _cityInfo.Add(new CityInfo() { City = "London", Country = "UK", CountryIn = Continent.Europe }); _cityInfo.Add(new CityInfo() { City = "Atlanta", Country = "USA", CountryIn = Continent.NorthAmerica }); _cityInfo.Add(new CityInfo() { City = "Sydney", Country = "Australia", CountryIn = Continent.Australia }); List<CityInfo> _cityInfo1 = new List<CityInfo>(); _cityInfo1.Add(new CityInfo() { City = "Delhi", Country = "India", CountryIn = Continent.Asia }); _cityInfo1.Add(new CityInfo() { City = "Leeds", Country = "UK", CountryIn = Continent.Europe }); _cityInfo1.Add(new CityInfo() { City = "New York", Country = "USA", CountryIn = Continent.NorthAmerica }); _cityInfo1.Add(new CityInfo() { City = "Melbourne", Country = "Australia", CountryIn = Continent.Australia }); GameData gamedata; _GameData.Add(new GameData() {CityInfo=_cityInfo,GameName = "World Of Warcraft", Creator = "Blizzard", Publisher = "Blizzard", OnXbox = false, Gender = GenderType.Male }); _GameData.Add(new GameData() { CityInfo = _cityInfo1, GameName = "Halo 3", Creator = "Bungie", Publisher = "Microsoft", OnXbox = true, Gender = GenderType.Female }); _GameData.Add(new GameData() {CityInfo=_cityInfo, GameName = "Gears Of War", Creator = "Epic", Publisher = "Microsoft", OnXbox = true, Gender = GenderType.Male }); _GameData.Add(new GameData() {CityInfo=_cityInfo, GameName = "World Of Warcraft", Creator = "Blizzard", Publisher = "Blizzard", OnXbox = false, Gender = GenderType.Male }); /*_GameData = new DataTable(); _GameData.Columns.Add(new DataColumn("Game Name", typeof(string))); _GameData.Columns.Add(new DataColumn("Creator", typeof(string))); _GameData.Columns.Add(new DataColumn("Publisher", typeof(string))); _GameData.Columns.Add(new DataColumn("On Xbox", typeof(bool))); _GameData.Columns.Add(new DataColumn("Gender", typeof(GenderType))); var row = _GameData.NewRow(); _GameData.Rows.Add(row); row["Game Name"] = "World Of Warcraft"; row["Creator"] = "Blizzard"; row["Publisher"] = "Blizzard"; row["On Xbox"] = false; row["Gender"] = GenderType.Male; row = _GameData.NewRow(); _GameData.Rows.Add(row); row["Game Name"] = "Halo 3"; row["Creator"] = "Bungie"; row["Publisher"] = "Microsoft"; row["On Xbox"] = true; row["Gender"] = GenderType.Female; row = _GameData.NewRow(); _GameData.Rows.Add(row); row["Game Name"] = "Gears Of War"; row["Creator"] = "Epic"; row["Publisher"] = "Microsoft"; row["On Xbox"] = true; row["Gender"] = GenderType.Male; _GameData.RowChanged += GameDataRowChanged; _GameData.RowDeleted += GameDataRowChanged;*/ InitializeComponent(); _PublisherCombo.ItemsSource = new List<string>() { "Activision", "Ubisoft", "Microsoft", "Blizzard", "Nintendo", "Electronic Arts", "Take-Two Interactive" }; dgRoot.DataContext = _GameData; //myGrid.DataContext = _GameData; //ItemsSource="{Binding ElementName=This, Path=GameData}" } private void GameDataRowChanged(object sender, DataRowChangeEventArgs e) { Console.WriteLine("----Row Changed----"); Console.WriteLine("Action: " + e.Action); if (e.Action != DataRowAction.Delete) { Console.Write("Values: "); foreach (var val in e.Row.ItemArray) { Console.Write(val + ", "); } Console.WriteLine(); } Console.WriteLine("-------------------"); } public DataTable GameData { get { return _GameData; } } } public class GameData { public string GameName{get;set;} public string Creator{get;set;} public string Publisher{get;set;} public bool OnXbox{get;set;} public GenderType Gender{get;set;} public List<CityInfo> CityInfo { get; set; } } public class CityInfo { public string Country { get; set; } public string City { get; set; } public Continent CountryIn { get; set; } } }
Subscribe to:
Posts (Atom)