0

Lets assume I load some business collections eagerly with NHibernate, EF or any ORM tool you wish...

Now I have an IList where each Customer has many Orders and each Order has many Products.

You have a GetAllCustomer() method in your CustomerRepository.

WHERE do you and HOW do you aggregate all your data into THREE ObservableCollections of type

Customer, Order and Product because I need add/delete notification events !?

Do you really do something like that in the BillingViewModel where you execute the customerRepo.GetAllCustomer():

BillingViewModel.cs


private ObservableCollection<Customer> _customersOC = new ObservableCollection<Customer>();

public BillingViewModel()
{

var customers = customerRepo.GetAllCustomer();
ConvertDomainToUICollections(customers);

}


private ConvertDomainToUICollections(IList<Customer> customers)
{

    foreach(Customer c in customers)
    {
        _customersOC.Add(c);
        foreach(Order o in c.Orders)
        {

            // Here I do not know how to proceed and put each in another OC<Order> etc...
        }

    }
}
msfanboy
  • 5,273
  • 13
  • 69
  • 120

3 Answers3

0

Personally, I try to aggregate data like this only when required, and at the "lowest level" possible.

For example, in the BillingViewModel, I'd do an aggregation like this for Customers. However, I wouldn't do Orders here - but rather, handle this within the CustomerViewModel, since I'd try to set it up so that I edit Orders on a specific Customer.

If you need to do this at the very high level, you'll effectively be rewriting your entire Model. This can be done, but it really reduces the usefulness and maintainability of your data, since you're effectively maintaining two full versions of the Model at a very high level.

That being said, many ORMs handle this much more nicely for you. For example, Lightspeed (one of my favorites) just makes their collections automatically implement INotifyCollectionChanged straight from the ORM. This makes it unnecessary to convert into ObservableCollection<T> - you can just use the data as returned from the ORM.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Yes I know about that tool and its features. Seems be a great product what if offers. – msfanboy Jan 29 '11 at 21:49
  • and great that I checked now again their homepage and they made a change in the free edition. Former times it were restricted to 8 tables afair. Now its restricted to 8 models written in bold letters. The reason why I did not decide for LS formerly is that I have many N:M tables exceeding the former 8 table rule, but now everything fit perfectly with 8 models, thanks Reed for answering here indirectly you solved my problem :P – msfanboy Jan 29 '11 at 22:08
  • http://www.mindscapehq.com/forums/Thread.aspx?PostID=9405 hm... it seems a N:M sql relation (3 tables) are not mapped with 2 models with Lightspeed as it seems because it needs this through object which counts as another model... am I right? – msfanboy Jan 29 '11 at 22:49
  • yes I am => http://www.mindscapehq.com/blog/index.php/2010/06/17/many-to-many-associations-and-composite-keys-in-lightspeed/ then nothing has changed 1 Model = 1 table... – msfanboy Jan 29 '11 at 23:23
0

One option is to create classes (essentially viewmodels) that represent all three types in your domain model. So you would create a customerVM class, and it would have a member observable collection of orderVMs, and the orderVM would have a a member ovservable collection of productVMs. Then you map your domain model to your viewModels, which is the UI representation of the data.

Some people find it painful to have separate classes for the domain and the viewmodel and have to map back and forth between them. However I think it is just to reality of using databinding and MVVM. One option might be to generate proxies or wrappers for your domain objects, and another might be to have the Notifying interfaces on the domain objects themselves.

Link to a related question: WPF/MVVM: Delegating a domain Model collection to a ViewModel

Edit: I just realized the linked questions was your question as well. :)

Community
  • 1
  • 1
Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
  • Yeah actually I know how to do aggregating and where etc... with pure SQL... but with ORM tools I am forced to create a ViewModel for every domain model thats said... I just wanted to hear again some opinions from PRO guys ;-) but I see those guys just use PRO tools :P – msfanboy Jan 29 '11 at 21:50
0

You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). It's a WPF MVVM application which uses the Entity Framework. It solves exactly your issue by introducing a EntityObservableCollection.

jbe
  • 6,976
  • 1
  • 43
  • 34