Suppose I have a WPF/MVVM application for managing some hypothetical customers :).
Domain model contains an entity named Customer (represented as a POCO in code).
The main screen contains a grid, bound to a view model (CustomersViewModel) that loads its data from Repository< Customer>.
The main screen also allows to create new customers (and save it to the DB).
Suppose I need to implement ‘add customer’ use-case. The most obvious approach is as follows:
- Present the user with a dialog window to be filled out with new customer data.
- Handle ‘Save’ button click in the ViewModel.
- Create customer (var new_customer = new Customer(..)) domain object using the data from the dialog (step 1).
- Call Repository< Customer>.Save(new_customer) to save the new customer to the DB.
- Reload CustomersViewModel with fresh data from the DB so that newly added customer is visible in the grid.
Personally I don’t like this ‘quick-and-dirty’ way (because of need to reload the full list of customers from DB every time a new customer is added).
Can anyone suggest a better approach (that wouldn’t require refreshing the customer list from the DB)??? I feel there gotta be some best practice for handling scenarios like that:) ).
Thanks in advance!
If the saving of the
Customeris successful, why can’t you just add that singleCustomerinstance to your collection of customers? No need to re-load all customers unless the user explicitly refreshes the view (usually via a refresh button).