ViewModel musings
-
Ramble I've been looking at using the View-ViewModel-Model pattern in WPF & Siverlight for some small apps. Often I find the ViewModel is very similar to the Model in my simple apps, but maybe just a few more view specific properties. Wrapping my Model class in a ViewModel is sometimes problematic. Say we have an Employee Class and we want to add a IsSelected property so it can be used in the View. If we create a ViewModel Employee class and add the new IsSelected property all is okay, except.... If any other class, say Company.GetEmployees() returns a List, then I have to write another wrap to enumerate through the results and transform Employee to ViewModelEmployee. Possible solution? Use Extension Methods (don't have Extension Properties) Then I can just add GetIsSelected, SetIsSelected methods to the base Employee class. Using an ObjectDataProvider (don't think this is in Silverlight) we can access the GetIsSelected method, using Bindings similar to properties. At least that's what I'm thinking haven't tried this out yet! /Ramble
-
Ramble I've been looking at using the View-ViewModel-Model pattern in WPF & Siverlight for some small apps. Often I find the ViewModel is very similar to the Model in my simple apps, but maybe just a few more view specific properties. Wrapping my Model class in a ViewModel is sometimes problematic. Say we have an Employee Class and we want to add a IsSelected property so it can be used in the View. If we create a ViewModel Employee class and add the new IsSelected property all is okay, except.... If any other class, say Company.GetEmployees() returns a List, then I have to write another wrap to enumerate through the results and transform Employee to ViewModelEmployee. Possible solution? Use Extension Methods (don't have Extension Properties) Then I can just add GetIsSelected, SetIsSelected methods to the base Employee class. Using an ObjectDataProvider (don't think this is in Silverlight) we can access the GetIsSelected method, using Bindings similar to properties. At least that's what I'm thinking haven't tried this out yet! /Ramble
To solve your very specific problem, use the OptionViewModel class from this article: http://www.codeproject.com/KB/WPF/ExploringWPFMVVM.aspx[ href="http://www.codeproject.com/KB/WPF/ExploringWPFMVVM.aspx" target="_blank" title="New Window">^] Give yourself more time and understand "why" you need a ViewModel. Writing some small aps is a great idea. Don't forget to write Unit Tests for those apps. ViewModel's make writing unit tests easy.
Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
-
To solve your very specific problem, use the OptionViewModel class from this article: http://www.codeproject.com/KB/WPF/ExploringWPFMVVM.aspx[ href="http://www.codeproject.com/KB/WPF/ExploringWPFMVVM.aspx" target="_blank" title="New Window">^] Give yourself more time and understand "why" you need a ViewModel. Writing some small aps is a great idea. Don't forget to write Unit Tests for those apps. ViewModel's make writing unit tests easy.
Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
Great answer Karl. Like it.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
-
To solve your very specific problem, use the OptionViewModel class from this article: http://www.codeproject.com/KB/WPF/ExploringWPFMVVM.aspx[ href="http://www.codeproject.com/KB/WPF/ExploringWPFMVVM.aspx" target="_blank" title="New Window">^] Give yourself more time and understand "why" you need a ViewModel. Writing some small aps is a great idea. Don't forget to write Unit Tests for those apps. ViewModel's make writing unit tests easy.
Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
Thanks Karl, looking forward to your MVVM series! I just noticed this article in the updates which is pretty much what I was thinking about. http://www.codeproject.com/KB/WPF/WPF-Presentation-Models.aspx[^]