public WorkspaceInstance() { if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject())) { Workspace.LoadInstance(this); } else { System.Data.Common.DbConnection connection = null; try { connection = ((IObjectContextAdapter)Workspace.NorthwindModel).ObjectContext.Connection; connection.Open(); if (connection.State.HasFlag(System.Data.ConnectionState.Open)) { _Employees = Workspace.Employees; _Orders = Workspace.Orders; _CustomersTreeList = Workspace.CustomersTreeList; _Suppliers = Workspace.Suppliers; _Customers = Workspace.Customers; _Categories = Workspace.Categories; _Products = Workspace.Products; } } catch (Exception ex) { App.LogException(ex); Workspace.LoadInstance(this); } finally { if (connection != null && connection.State.HasFlag(System.Data.ConnectionState.Open)) { connection.Close(); } } } }
internal static WorkspaceInstance LoadInstance(WorkspaceInstance instance) { try { System.Uri resourceUri = new System.Uri("/Northwind.NET.Sample;component/SampleData/Workspace.xaml", System.UriKind.Relative); if (System.Windows.Application.GetResourceStream(resourceUri) != null) { System.Windows.Application.LoadComponent(instance, resourceUri); foreach (Product product in instance.Products) { Category category = instance.Categories.Where(cat => cat.ID == product.CategoryID).FirstOrDefault(); if (category != null) { product.Category = category; category.Products.Add(product); } Supplier supplier = instance.Suppliers.Where(suppl => suppl.ID == product.SupplierID).FirstOrDefault(); if (supplier != null) { supplier.Products.Add(product); product.Supplier = supplier; } } foreach (OrderDetail orderDetail in instance.OrderDetails) { Product product = instance.Products.Where(prod => prod.ID == orderDetail.ProductID).FirstOrDefault(); if (product != null) { product.OrderDetails.Add(orderDetail); orderDetail.Product = product; } Order order = instance.Orders.Where(ord => ord.ID == orderDetail.OrderID).FirstOrDefault(); if (order != null) { order.OrderDetails.Add(orderDetail); orderDetail.Order = order; } } foreach (Order order in instance.Orders) { Customer customer = instance.Customers.Where(cust => cust.ID == order.CustomerId).FirstOrDefault(); if (customer != null) { customer.Orders.Add(order); order.Customer = customer; } Employee employee = instance.Employees.Where(empl => empl.ID == order.EmployeeID).FirstOrDefault(); if (employee != null) { employee.Orders.Add(order); order.Employee = employee; } } CustomersTreeVm customersTreeList; var listCustomersQuery = from cust in instance.Customers orderby cust.Country group cust by cust.Country into countryGroup select new CountryCustomersTreeItem { Country = countryGroup.Key, Count = countryGroup.Count(), Cities = ( from country in countryGroup group country by country.City into sityGroup select new CityCustomersTreeItem { City = sityGroup.Key, Count = sityGroup.Count(), Customers = (from sity in sityGroup select sity).ToList() } ).ToList() }; customersTreeList = new CustomersTreeVm(listCustomersQuery.ToList()); instance.CustomersTreeList = customersTreeList; } } catch (System.Exception ex) { App.LogException(ex); } return(instance); }