/
MainWindow.xaml.cs
132 lines (125 loc) · 5.07 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Grid;
using System.Data.Entity.Core;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
namespace EntityInstantFeedbackCRUD {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
NorthwindEntities context;
Control control;
Customers newCustomer;
Customers customerToEdit;
public MainWindow() {
InitializeComponent();
context = (NorthwindEntities)FindResource("NorthwindEntities");
}
private void Add_Click(object sender, RoutedEventArgs e) {
newCustomer = CreateNewCustomer();
EditCustomer(newCustomer, "Add new customer", CloseAddNewCustomerHandler);
}
private void Remove_Click(object sender, RoutedEventArgs e) {
DeleteSelectedCustomer(view.FocusedRowHandle);
}
private void Edit_Click(object sender, RoutedEventArgs e) {
EditSelectedCustomer(view.FocusedRowHandle);
}
private void view_RowDoubleClick(object sender, RowDoubleClickEventArgs e) {
EditSelectedCustomer(e.HitInfo.RowHandle);
}
private void view_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
if(e.Key == System.Windows.Input.Key.Delete) {
DeleteSelectedCustomer(view.FocusedRowHandle);
}
if(e.Key == System.Windows.Input.Key.Enter) {
EditSelectedCustomer(view.FocusedRowHandle);
}
}
string GetCustomerIDByRowHandle(int rowHandle) {
return (string)grid.GetCellValue(rowHandle, colCustomerID);
}
void FindCustomerByIDAndProcess(string customerID, Action<Customers> action) {
try {
EntityKey entityKey = new EntityKey("NorthwindEntities.Customers", "CustomerID", customerID);
Customers customer = (Customers)context.GetObjectByKey(entityKey);
action(customer);
} catch(Exception ex) {
HandleException(ex);
}
}
Customers CreateNewCustomer() {
Customers newCustomer = new Customers();
newCustomer.CustomerID = GenerateCustomerID();
return newCustomer;
}
string GenerateCustomerID() {
const int IDLength = 5;
string result = String.Empty;
Random rnd = new Random();
for(int i = 0; i < IDLength; i++) {
result += Convert.ToChar(rnd.Next(65, 90));
}
return result;
}
void DeleteSelectedCustomer(int rowHandle) {
if(rowHandle < 0) return;
if(MessageBox.Show("Do you really want to delete the selected customer?", "Delete Customer", MessageBoxButton.OKCancel) != MessageBoxResult.OK) return;
FindCustomerByIDAndProcess(GetCustomerIDByRowHandle(rowHandle), customer => { context.Customers.DeleteObject(customer); SaveChandes(); });
}
void EditSelectedCustomer(int rowHandle) {
if(rowHandle < 0) return;
FindCustomerByIDAndProcess(GetCustomerIDByRowHandle(rowHandle), customer => {
customerToEdit = customer; EditCustomer(customerToEdit, "Edit customer", CloseEditCustomerHandler);
});
}
void EditCustomer(Customers customer, string windowTitle, DialogClosedDelegate closedDelegate) {
control = new ContentControl { Template = (ControlTemplate)Resources["EditRecordTemplate"] };
control.DataContext = customer;
FloatingContainer.ShowDialogContent(control, grid, Size.Empty, new FloatingContainerParameters()
{
Title = windowTitle,
AllowSizing = false,
ClosedDelegate = closedDelegate
});
}
void CloseAddNewCustomerHandler(bool? close) {
if(close ?? false) {
context.Customers.AddObject(newCustomer);
SaveChandes();
}
control = null;
newCustomer = null;
}
void CloseEditCustomerHandler(bool? close) {
if(close ?? false) {
SaveChandes();
}
control = null;
customerToEdit = null;
}
void SaveChandes() {
try {
context.SaveChanges();
} catch(Exception ex) {
HandleException(ex);
DetachFailedEntities();
}
entityInstantSource.Refresh();
}
void DetachFailedEntities() {
foreach(ObjectStateEntry stateEntry in context.ObjectStateManager.GetObjectStateEntries(
EntityState.Added | EntityState.Deleted | EntityState.Modified)) {
context.Detach(stateEntry.Entity);
}
}
void HandleException(Exception ex) {
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
}
}
}