MVVM Pattern?

May 2, 2008 at 2:43 PM
Is anyone using the Model-View-ViewModel pattern with Prism? I used the pattern to prototype an app and I found it to be really useful with WPF. Just curious if anyone was using it with Prism and what they discovered, if anything.

Is you are not familiar with the MVVM pattern check out this link: http://blogs.sqlxml.org/bryantlikes/archive/2006/09/27/WPF-Patterns.aspx. The pattern also goes by the name of DataModel-View-ViewModel.

May 2, 2008 at 5:31 PM
Edited May 2, 2008 at 7:35 PM
Actually, we have been looking at this pattern, and we were spiking a little with it, taking it a little further by composing at the ViewModel level with the help of templates.
Take a look at this post where a colleague talks about it: Using the Presentation Model in WPF.

Ezequiel Jadib
http://staff.southworks.net/blogs/ejadib/default.aspx

May 6, 2008 at 8:08 PM
The examples of using data templates instead of user controls is interesting and rather appealing. Although it took a few minutes to figure out what was going on. Looks promising!

How to bridge the ICommand with the PresentationModel will be interesting to see examples of with no code behind. Perhaps declaratively invoking a controller action?
May 7, 2008 at 1:17 AM
Bridging the gap wasn't so bad, I decided to give it a try and it also turned out nice. This approach really cleans and helps separate out responsibilities. I hope it sticks around, and I look forward to what the team comes up with.
May 8, 2008 at 8:19 AM
Check out the ViewModel UI Composition spike we uploaded in the last drop (in the PublishedSpikes folder). It illustrates using this technique to do all UI composition through models and templates. The shell is a model, regions are models, and views are models. DataTemplates act on each and provide the look and feel.
May 8, 2008 at 3:32 PM
I guess I wasn't clear enough, sometimes I get ahead of myself. The ViewModel UI Composition spike is what I was referring to when I mentioned using data templates instead of user controls.
May 9, 2008 at 10:11 AM
I have used this pattern before and it has significant advantages however is there some way we can overcome the limitations of no designer ? Most devs are so wed to a designer not having it is a big risk.

To me if we can get a designer we can forget about MVP/MVC ?

To me
No code behind ++ ( easier to test and maintain)
Reusability ++ ( standard templates - while you can do this other wasy with Templates its much cleaner)
Simplicity ++

However
No Designer ---


Am i wrong ? What are you views ?

Regards,

Ben
May 9, 2008 at 2:21 PM
I'm still digesting the ViewModel UI Composition spike in the latest release. But my initial thought was that ui/ux people will not like working with DataTemplates.

My initial idea when attemping to utilize the MVVM pattern was to create the View as a UserControl, create the ViewModel as a class bound to UserControl via DataContext property. And plain old classes for the Models as my business entities. I don't know if I'm applying the pattern properly, but I had used it this way in previous non-Prism WPF apps successfully (at least I think it was successful).

Still need some more time to fully grasp what's happening in the ViewModel UI Composition spike...
May 11, 2008 at 4:19 AM
@jmodzel

For the team, DataTemplates composition feel like a more "natural" extension of WPF. Although DataTemplate support is very limited in Cider, there's very rich support in Blend. Using models and templates, actually gives MORE opportunity for a designer to be able to visualize what is going on.

Regards
Glenn
May 12, 2008 at 5:19 AM
Glenn,

It seems a big improvement but surely there must be some way to force Cider to render it ? I thought Blend would do it but most devs dont have blend and you normally dont get the designer guys back for minor cosmetic changes..

Is there like conditional compilation some sort of #if extention..? Is it possible we will see a patch for VS to handle this ?

Regards,

Ben
Jun 6, 2008 at 5:12 AM
Anyone know if SP1 of VS2008 has fixed the non rendering of resources bug ?

BTW I think most UI people will like using Data Templates if they rendered and even better they will be nudged into putting more logic in the viewmodel and model as oposed to the usercontrol/view.

Regards,

Ben
Jun 6, 2008 at 8:16 AM
jmodzel, I hear you and I fully support your idea, personally I've not quite grasped data templates, I'm still grasping WPF as it's such a steep learning curve coming from a component model like Windows Forms.

I Really like the DataContext idea as it allows me to create simple data structures in my view-model and have a 1 liner in my presenter to bind the data object (model) to the UI.

Okay, that probably sounded stupid, and please take me out the back and beat me up, but I just wish all this technology would get into my head.

-Brett

P.S. Is WPF natural only to web developers? Am I the only one that's struggling with WPF?
Jun 9, 2008 at 3:27 PM
Oh, you're not the only one, Brett! One thing that makes it really tough for me is that there are so many ways to do the same thing. Even the "seasoned" WPF guys offer up different solutions to the same problem. It's cool technology, it's just not yet matured to the point where there is a lot of authoritative guidance on it. That's my 2 cents, anyway (I've been playing with it for a few weeks.)

brettryan wrote:
jmodzel, I hear you and I fully support your idea, personally I've not quite grasped data templates, I'm still grasping WPF as it's such a steep learning curve coming from a component model like Windows Forms.

I Really like the DataContext idea as it allows me to create simple data structures in my view-model and have a 1 liner in my presenter to bind the data object (model) to the UI.

Okay, that probably sounded stupid, and please take me out the back and beat me up, but I just wish all this technology would get into my head.

-Brett

P.S. Is WPF natural only to web developers? Am I the only one that's struggling with WPF?



Jun 10, 2008 at 6:25 AM
Yep, I know what you mean. I've been working really hard, and slowly moving along to work out how to make the WPF and Prism framework work for me. I'm trying to keep things as simple as I can while still gaining flexibility and extensibility.

I actually like the fact there's a lot of variety, it means that we can pick and choose the way to do things, eventually great patterns will come out of all the different ways of doing things. I'm starting to learn that WPF is actually a good tool for developers since it doesn't restrict us as much as it used to. I remember .NET 1.1 when I was first learning everything forced ADO.NET datasets down my throught and I felt it extremely difficult to break away from that model, now the industry seems to be wanting to do the same, of which WPF is helping a great deal.

Over the past week I've wiped away all my previous WPF solutions and started from scratch to try and come up with a simple Prism application, it currently represents a couple of use-cases for an upcoming project in my company that I'm experimenting with, but I think the ideas could help others. I'd love to polish it up and publish it for all to see. There's hardly any unit tests as I'm constantly changing the way the thing works. My goal is to work out how to make an extensible application though simple service contracts. The application is an MDI style app, the classic two pane application with a tabbed use-case browser extension on the left and a tabbed document window to the right. There are extension sites being exposed just for the menu bar at the moment but I do wish to create a general menu service, but for the most part the app design is trying to avoid extension sites as I'm a huge fan of compile time safety.

I'm thinking of putting this up as a "Issue Tracker" post so I can attach a file, is there any other way to post example solutions? I've also asked this question on this post. Mabe I should create a blog? I want to write a script that directs the readon on how to create the solution and why I'm doing things the way I am.

I hope I didn't ramble too much once again,
-Brett


ID10T wrote:
Oh, you're not the only one, Brett! One thing that makes it really tough for me is that there are so many ways to do the same thing. Even the "seasoned" WPF guys offer up different solutions to the same problem. It's cool technology, it's just not yet matured to the point where there is a lot of authoritative guidance on it. That's my 2 cents, anyway (I've been playing with it for a few weeks.)


Jun 11, 2008 at 12:41 PM
Edited Jun 11, 2008 at 4:55 PM

Just my two cents on learning WPF...

It seems that with the introduction of any new technology, your first attempts often based on old technology patterns.  For instance, I can (painfully) remember my first .Net 1.0 web application.  I was trying to use ASP.Net like it was classic ASP.  Similarly, with WPF, if you are coming from a WinForms background, you may find yourself using techniques that worked well in WinForms, but require an atomic shoehorn to work in WPF.

There does seem to be a "WPF mindset", which sets in after a few sleepless months.  The more you work with it, the more you start thinking WPF.  Best practices seem elusive, partly because, as mentioned, there are so many different ways to do things.  Choice is great, but it helps to know what it is you are choosing from, and more importantly - why?

A good approach is to think of Xaml similar to XHtml.  For me, once I become fluent with a markup language I don't bother with designers.  Just like working with Html in web apps, you just known what the markup renders like.  It took a while, but I'm pretty much at that stage with Xaml.

So, you are not alone, everyone goes through the WPF Wall of Fire.

On a separate thread...any prediction on the first official release of Prism?

Cheers,

Ron

Jun 11, 2008 at 1:22 PM
Thanks Ron, I think you've expressed how the majority of us are feeling, if not you certainly gathered how I feel.

I know what you mean with your learning path, I came from a java background, then went to Windows Forms, I haven't had a great deal of work with ASP.NET though have been working in JSP and JSF. XML markup isn't the problem I'm facing, it's more the patterns involved to put it all together. There are hundreds of things I'm still having trouble figuring out, like how to get a command to fire when someone double clicks a ListViewItem within a DataTemplate... I didn't get to bed till about 5:30am thismorning trying to figure that one out, and still haven't got it.

Slowly every day I unserstand just one little thing a little better, but it's adding up. I'm getting a little more fluent in XAML, but it's the patterns that I'm clawing at to try and get a hold of.

Maybe I'm just a slow learner? I feel it some days with WPF ;)
-Brett
Jun 11, 2008 at 3:14 PM
Edited Jun 11, 2008 at 3:16 PM
My experience is exactly the same.  I've been tasked with creating a WPF prototype for a new application and I've been working on it pretty consistently for the past 3 months or so.  Last week I had a major coding epiphany and now I "get it"....where "it" = WPF and Prism. 

One of my challenges is going to be getting my other devs up to speed and productive once my app is ready for development.  We're having our MS guys come in and do training, but you simply have to spend time doing this stuff before it sinks in. The wall of fire analogy is a good one. 

I thought gblock had said (in a video) that the end of the year would be a release 1.0....or at least implied it.  Not trying to start any rumors or anything.


Jun 12, 2008 at 12:00 AM
hehe, I think we've hijacked this thread and turned it into "So how are you finding WPF?", lol.

I wish I had the luxury of a team. I'm a 1 man "OO-guy" in our company in an apps team of 4. The other three are 4gl (Progress) people where I'm also writing progress code. Unfortunately our infrastructure guys have a mentality to "stay latest, we don't want to fall behind" yet we use legacy style systems from telnet sessions, with Windows satalite programs to handle more "graphical" applications (that's where I come in). Sometimes they don't understand how I have to learn things, there's a sort of feeling like "What? You already know C#, why do you need to learn more?", I just nod my head and sob a little inside, lol.

Maybe that's it, I just need the time. Can anyone tell me of any courses in Victoria, Australia that I can take?

Ah well, at least I'm still smiling and giving it a world of a go :)
-Brett
Sep 9, 2008 at 4:39 AM
Going back to the Presentation Model topic, I posted an example on how to use this pattern with DataTemplates.
Prism shipped using the presentation model pattern in the Commanding QuickStart, but without using DataTemplates. I took that code as a starting point to show how you could implement it without using UserControls as views, and composing the "UI" at the ViewModel level.

Link: Presentation Model with DataTemplates in CompositeWPF (Prism) Sample

Thoughts?
Sep 9, 2008 at 8:44 PM
I read your blog post and checked out the code.  This looks good to me.  My org is working on an app using prism and this is the basic pattern we are using.  We ended up simplifying our terminology a bit and we have models (aka ViewModels or PresentationModels) and views (DataTemplates).
Sep 12, 2008 at 2:02 PM
<<My goal is to work out how to make an extensible application though simple service contracts. >>

Brett - have you seen the Managed Extensibility Framework?

http://www.codeplex.com/Wiki/View.aspx?ProjectName=MEF


Andrew