Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:10:45+00:00 2026-05-23T20:10:45+00:00

This is based on the example from the book: Pro WPF and Silverlight MVVM

  • 0

This is based on the example from the book:
Pro WPF and Silverlight MVVM by Gary McLean Hall

where the author only insists on how to create the Model for a DB structure (and how to implement the DAL for it).

I am looking for the correct way to create the ViewModel(s).

Here is the database model and the MVVM Model– I suspect it is not quite complete, but the Product is missing the ProductCode:

My Views will be: Pages displaying / editing views for Products, Customers and Orders
I am familiar with the Models / ViewModels implementing / using the INotifyPropertyChange and ObservableCollection, no need to insist on that.

My questions:

  1. How to create the ViewModels in such a way that they would all share the same model
  2. How do I manage the ViewModels? Do I have one Main ViewModel which aggregates all the specific ones? This relates to ViewModel state saving and restoring.

I am particularly interested in how to deal with this: the Model for Order has a List of Products. I will also have to maintain a list of Products for my ProductsViewModel which supports the displaying / editing Views for the Products. How can all be synchronized? Should the OrderModel only have a List of ProductCodes instead? What are the implications in that case?

In general, the thing I am after here is: how to create and manage the ViewModels for Models which implement DB tables with many to many relationships (like Product-Orders). Do we use only the foreign keys as part of the Model objects or do we use a reference to a whole other Model object represented by that foreign key?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:10:45+00:00Added an answer on May 23, 2026 at 8:10 pm

    To me it sounds like you are thinking about it the wrong way round. When you ask “How to create and manage the ViewModels for the Models which implement DB tables with many to many relationships” it sounds like you are thinking about ViewModels in terms of Models. Which isn’t right. A ViewModel is a model of a View – not the Model. You shouldn’t be thinking about creating ViewModels for your models, you should be thinking about creating VewModels of your Views.

    In fact the model doesn’t even come into it until the end. Start with your UI – your View. You then create a logical representation of that View in code so that you can bind to it, your ViewModel. Then finally you implement your ViewModel by accessing your Model to do what ever needs to be done. When you come to design the next View, even though it might contain some of the same data as the first, you would still create a new model for it – another ViewModel. Now the new ViewModel could include some of the same properties at the first ViewModel, which is fine. Remember is is a model of the View not the Model.

    Consider an email app, with 2 views, summary and detail. Because there are 2 different Views you have 2 different ViewModels even though the are both pulling data from the same underlying model.

    Model                SummaryViewModel       DetailsViewModel
    -----                ---------------        ----------------
    RecipientAddress                            RecipientAddress
    SenderAddress        SenderAddress          SenderAddress
    Subject              Subject                Subject
    Content                                     Content
    

    Now the Summary View is only a summary and doesn’t display the RecipientAddress or the Content so consequently those properties don’t exist on the SummaryViewModel. The Details View displays more information, so has more properties. This same idea answers your question about foreign keys. Foreign keys are never visible on your View therefore have no business being members of your ViewModel. Your ViewModel cares only about what is required for the View.

    In answer to your question “how do you manage the view models”: you don’t. You don’t have to. ViewModel instances usually (not not always) have a one to one relationship with the Views, therefore you don’t need to manage them. They only live as long as the view lives. You just create a new instance of the ViewModel when the View is loaded (typically in OnNavigatedTo event handler, see below) and store it in the View’s DataContext. If two instances of the View are loaded, then there are two ViewModels. When the View is GC’ed, the ViewModel is too (if it isn’t you have a memory leak).

    Finally, as for how you should synchronize changes, that can be tricky in a desktop Silverlight app where many views might display concurrently. Fortunately on the Windows Phone we usually only have one view open at a time (although not always). So we can simply tell our ViewModel to refresh each time the page is navigated to:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
    
        if (DataContext == null)
             DataContent = new MyViewModel();  //Create new instance of the ViewModel
        else
             (MyViewModel)DataContext.Refresh();  //Refresh the existing ViewModel
    }
    

    This works well in most cases for more advanced scenarios you could look at the messaging infrasructure provided by a toolkit like Prism or MvvmLight.

    Hope this has helped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Pylons verson 1.0: Working on the FormDemo example from the Pylons book: http://pylonsbook.com/en/1.1/working-with-forms-and-validators.html
I am playing with the euclidian distance example from programming collective intelligence book, #
I want to try a lighting example from the book OpenGL ES 2.0 Programming
I just got a book for iPhone programming from the library which is based
I bought a book about MVC 3. In this book, there is an example
I 'm trying to retrieve data from a datastore by using this (in example)
Using this question as the base is there an alogrithm or coding example to
This is based on a similar question How to Replace Multiple Characters in Access
This is based on my full question . I decided to take it in
Some time ago I created this LAMP based web. At the time I wrote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.