I am creating my first real WPF application (i.e. the first one intended for use by someone other than me), and I'm still considering the best way to do something in WPF. This is a fairly simple data access application that uses the fairly new Entity Framework, but I have not been able to find many tutorials on the Internet to make the best use of these two technologies (WPF and EF) together. So I thought that I would throw it out as I approached him and see if anyone had any better suggestions.
I am using the Entity Framework with SQL Server 2008. EF hits me as much more complicated than necessary and is not yet ripe, but Linq-to-SQL seems to be dead, so I can also use the technology that seems to be focused on MS.
This is a simple application, so I (so far) did not consider it necessary to create a separate data layer around it. When I want to get data, I use fairly simple Linq-to-Entity queries, usually directly from my code, for example:
var families = from family in entities.Family.Include("Person") orderby family.PrimaryLastName, family.Tag select family;
Linq-to-Entity queries return an IOrderedQueryable result that does not automatically reflect changes in the underlying data, for example, if I add a new record through the code to the entity data model, the presence of this new record will not be automatically reflected in various controls that reference the Linq query . Therefore, I throw the results of these queries into the ObservableCollection to capture the main data changes:
familyOC = new ObservableCollection<Family>(families.ToList());
Then I map the ObservableCollection to the CollectionViewSource so that I can get filtering, sorting, etc., without returning to the database.
familyCVS.Source = familyOC; familyCVS.View.Filter = new Predicate<object>(ApplyFamilyFilter); familyCVS.View.SortDescriptions.Add(new System.ComponentModel.SortDescription("PrimaryLastName", System.ComponentModel.ListSortDirection.Ascending)); familyCVS.View.SortDescriptions.Add(new System.ComponentModel.SortDescription("Tag", System.ComponentModel.ListSortDirection.Ascending));
Then I bind the various controls and something wrong with this CollectionViewSource:
<ListBox DockPanel.Dock="Bottom" Margin="5,5,5,5" Name="familyList" ItemsSource="{Binding Source={StaticResource familyCVS}, Path=., Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource familyTemplate}" SelectionChanged="familyList_SelectionChanged" />
When I need to add or remove records / objects, I manually do this both from the entity data model and from the ObservableCollection:
private void DeletePerson(Person person) { entities.DeleteObject(person); entities.SaveChanges(); personOC.Remove(person); }
I usually use StackPanel and DockPanel elements to position elements. Sometimes I use Grid, but it's hard for me to support: if you want to add a new row to the top of the grid, you need to touch each control directly placed in the grid to tell it to use the new row. Uggh. (Microsoft never seemed to get the DRY concept.)
I almost never use the VS WPF constructor to add, modify, or control positions. The WPF designer that ships with VS is somewhat vaguely useful to see how your form will look, but even then, well, actually, especially if you use data templates that are not tied to data available at design time. If I need to edit my XAML, I take it as a person and do it manually.
Most of my real code is in C #, not in XAML. As I mentioned elsewhere , completely eliminating the fact that I'm not used to “thinking” in it yet, XAML amazes me like a clumsy, ugly language that also happens with poor designer and intellisense support, and this can't be debugged. Uggh. Therefore, whenever I can clearly see how to do something in C # code, I cannot easily see how to do it in XAML, I do it in C # without apologizing. Much has been written about how it is good practice to almost never use code on a WPF page (say, for event processing), but at least at least it makes no sense to me. Why do I have to do something with an ugly, awkward language with awful syntax, a terribly bad editor and little security like when I can use a good, clean language like C # that has a world-class editor, almost perfectly intellisense and security an unprecedented type?
So where am I. Any suggestions? Did I miss any big parts of this? Anything I really need to think differently about?