MVVM: Best Practices and Design Patterns RRS feed

  • General discussion

  • While the title is somewhat generic I would love to disucss this as a beginner with experienced MVVM users and other beginners as well.

    I have created a WPF app mostly ignoring MVVM, at least to begin with. I have now decided to refactor it and use MVVM. I'm hoping to make it better and easier to manage. However there are several things I'm not 100% certain about how to implement some things in MVVM. I guess there isn't just one way but I would love to hear what you like to do and how. I'm adding numbered discussion points here to better catagorize. If others have questions or thoughts to add please be welcome.

    1) Most MVVM examples are quite simple. In reality it can be quite different. For example I often have objects that include a number of other objects which also include other objects. So there is a View where users could choose an object from a list, see detailed info about it and also a list of objects that are it's children. They could then choose a child object and get detailed info about it as well.

    How does this apply as ViewModels. Is it a single viewmodel that includes everything, or a viewmodel that opens another different viewmodel. I would like to have the child object as it's own ViewModel/UserControl - View and have the parent able to show this UserControl within it's own.

    Would you rather have the child inside the parents viewmodel or seperate but linked?

    Can you point out examples or books that investigates this?

    2) iEditableObject, I've used this interface on my objects and it works with CollectionViews. I haven't really seen it mention in MVVM. It means a object can basically be in a EditMode and you can rollback changes made and such. Now in my program when you look at the details of a object they are static, meaning you can't change text (it's textBlocks not TextBox), then you press a EDIT button and you get the edit look. I've done this by switching out the dataTemplate for the object. However that seems to mean logic inside Xaml. I guess I could do it in code, or just have seperate UserControls and swithc them out

    Also this handles well adding new items to collections but I haven't seen this used in any MVVM example I've seen. Could you point out any?

    3) If many apps are getting the data from the same place, and they can change that data. How do you keep the data current. Do you reload it regulary, use ActiveRecord pattern. I guess this has nothing to do with MVVM but I'm fishing out what people have used alongwith MVVM with good results.

    4) Can you use LINQ entity items with any good results, like perhaps populating the viewmodel with them? It's not proper to keep the context open but having the object itself might help with saving back to database. What happens to a LINQ object when the Context is closed. Do I still have access to it's relative tables an such.

    5) I'm very often showing objects as lists+details for the selected object (master detail view). However the details between objects can be somewhat different. I guess I could make a abstract ViewModel for their common functionality but what about the Views themselves. It will have the same buttons, the same format showing the list and then the details. Basicly the only difference will be the fields in the detail. I can't make a abstract UserControl (except without the XAML). How to solve this? Using styles, ControlTemplates? Again any examples out there I could check out?

    I'll probably have million other questions but this is enough for now. If anyone can answer or just point towards something with good answers/examples I would be very happy.

    Most of what I know about MVVM comes from http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

    Friday, February 18, 2011 2:56 PM

All replies