1

Relations is probably the wrong word to use, but Data tables are what I have used before. I have EF4 pulling from a SQL database. Tables like Customer, Company, Department; things that usually go into Comboboxes for selection. I am holding them in static lists (only needs to populate on app startup), and I have a few comboboxes itemssource bound to them, which makes setting "relating" a specific selection easy by binding the selected item from the ViewModel.

My problem is, in a few places I just need a the name associated with an ID in a datagrid for display only. A couple hundred rows with a CompanyId that needs to be a CompanyName. I am concerned about performance here. I could use the DB FK's to get the names during the lookup, but thats seems like a waste since I have them all in static lists. I also don't know if Lazy loading would mean they get looked up during data binding, or during the initial query.

What is the best solution here? Can you make a wpf value converter using static lists? Should I perform a foreach on the data after getting it, and looking the values in the static list, storing the Name in the object?

Kyeotic
  • 19,697
  • 10
  • 71
  • 128

2 Answers2

1

You could build this logic into your query:

var viewModels = (from c in objectContext.Customers
                 select new MyGridViewModel {
                       CustomerId = c.CustomerId,
                       CompanyId = c.CompanyId,
                       CompanyName = c.Company.Name
                 }).ToList();

This will eliminate the need to fetch all of your columns (only CustomerId, CompanyId, and Company Name will be returned. You also won't be lazy loading the Customer's Company.

Then you can simple bind your View Models to the grid:

myGrid.ItemsSource = viewModels;
Jeff
  • 35,755
  • 15
  • 108
  • 220
  • My only concern with this solution is making SQL lookup `c.Company.Name` a hundred times might be a performance issue. If the application could do this using the lists its already pulled, I would expect that to work better. Of course, that is my question, so if you don't think its an issue I will go with that. – Kyeotic Aug 12 '11 at 22:04
  • It will not. The above example will perform a single query to return all results. It uses joins and nested queries to fetch CompanyName in the same query as CustomerId. – Jeff Aug 13 '11 at 03:09
0

Couldn't you just inject the static data required for ComboBoxes into the constructor of each of your ViewModels?

For example, in your constructor, you might pass in a List<Customer> customers or if you want to avoid using entity objects, you could pass in a List<CustomerViewModel> customerViewModels, which has only the columns you need for populating combo boxes.

If you are using a dependency injection framework, this would be particularly easy, but even if you're not, it shouldn't be too bad, and it's good practice.

devuxer
  • 41,681
  • 47
  • 180
  • 292
  • I don't actually see how this relates to my question. Yes, I could do that, but how will that accomplish my task? Also, the question isn't about the comboboxes, please read it again. – Kyeotic Aug 12 '11 at 22:24
  • I was focusing on your statement about making a ValueConverter. I don't think a ValueConverter should be necessary--you could inject the data you need into your ViewModels. That said, JeffN825's answer actually seems pretty solid to me. I wouldn't worry about a few extra database queries for a WPF app. Databases like SQL Server are designed to handle huge loads. It would definitely be an example of premature optimization to try to save various query results to avoid some foreign key joins. If performance becomes an issue, then you can try to cache some of the data in static lists. – devuxer Aug 13 '11 at 01:14