Is MVVM pattern a symbiote of MVC + PAC patterns? - wpf

I've surfed Wikipedia and have found such an article:
http://en.wikipedia.org/wiki/MVC4WPF
A part from the link upper:
MVC4WPF is an open-source, extensible, automated code pattern developed at Information Control Corporation for Windows Presentation Foundation (WPF) development based on the Model-View-Controller (MVC) and Presentation-Abstraction-Control (PAC) patterns...
I know, that WPF/Silverlight do use MVVM pattern: Model-View-ViewModel.
So is MVC4WPF a first version of MVVM?
I don't know the history of WPF/Silverlight development well, but MVVM has always remind me some sort of MVC.
And if it's true, then MVVM = MVC + PAC?

Whenever I see questions like this, I always think about Dr. WPF's design pattern.
It really doesn't matter what you want to call your separate concerns. You will hear a lot of WPF people talk about MVVM, but all it really boils down to is trying to keep your code separate. In MVVM, you have:
Model All your data classes. Knows nothing about anything.
View You gotta show your data. It knows about the Models, but not how to get them. "Knows" about the ViewModel through Bindings. Depending on how you wire them together, it may even create the ViewModel.
ViewModel This is the glue between the View and the Model. It can know about the View, or you can do your best to hide the view from the ViewModel.
In MVC you have:
Model All your data classes. Knows nothing about anything.
View You gotta show your data. Knows about Models, but not how to get them.
Controller This is the glue between the view and the model. It knows about the View and the Model.
Really, the last bit is all that changes (hence Dr. WPF's MV-poo). And in WPF, Bindings in XAML are so nice why would you want to write a bunch of code? Call it whatever you want, it is easier in WPF if the View knows a bit about the poo.
Having never used the PAC pattern, I can't speak to its strengths, but from Wikipedia, it seems to be very similar to MVC. So, I would classify it as a bit of MV-poo.
Now, considering that MVC4WPF hasn't had a release since 2009 (and their website with documentation is down as of 7/6/13), I would suggest you steer clear of it. I can't speak to its strengths or weaknesses, but if you'd like a good MVVM framework, MVVM Light and Caliburn.Micro both have good support and great reviews.
I'd also point out that not all applications need the poo. Mike Hillberg put this nicely.

Related

How to design a data model to be usable across different application frameworks?

I have a WPF GUI application based on MVVM design and data binding. Now, I want to reuse the core code (i.e. the data model) in a Windows service, or a console UI app, or a WinForms app.
Is such a design reasonable? If yes, what are the pitfalls?
Or should I make a standalone data model instead, and interface WPF via wrappers?
UPDATE:
Sorry, I should have been more precise. Let me clarify: I don't doubt the very modularity thing =) My concern boils down to having my current DataModel implement INotifyPropertyChanged, use DispatcherTimers, etc. -- all that non-GUI but still WPF stuff. The model's business logic is based on it.
Is this (non-GUI WPF) design acceptable for reuse in the aforementioned cases, or should I abstract further, until no references to WPF would be required at all?
Yes, this is perfectly acceptable and most of the time it is desired!
When you build an MVVM app, it should be in at least 3 formal layers:
Presentation WPF, UI, xaml, behaviors. All that stuff. Not reusable
Application The view models and structure that supports your application rules. All that stuff. Not intended for reuse
Foundation Database access, business objects. Domain specific algorithms. Ideally this bit should be reusable anywhere
The foundation layer is the clever bit. This is where the meat in your application sandwich is. It makes perfect sense for this to be totally agnostic of UI technology. WPF, winforms, ASP. It shouldn't even need a UI.
Edit for question update:
Removing all references to WPF is hard because sometimes you need a CollectionViewSource on your view models for grouping/filtering of results. That is a WPF class.
It is very tempting to view your seperation-of-concerns as 'just dont reference wpf' and that helps but it can make life difficult. Instead, try to be disciplined with the type of behaviors you are putting in. If you find yourself writing 'clever' (domain) code on a view model, shift it to the foundation layer as a business object method or extension. Similarly, if you find yourself implementing IValueConverter often, perhaps you should make better use of view models.
One thing is for sure, your foundation layer should never, ever, ever reference WPF.
Such a design is very reasonable! You can create a portable C# library for all .NET technologies including WPF, WinRT, ASP MVC, etc which can contain your models. Obviously you'll need to wrap these portable models into a viewmodel anyway, but IPropertyChanged is implemented in all XAML flavors.

WPF/XAML vs MVC Paradigm: flawed in the very fundamental design

In MVC a model can have multiple views, in WPF a XAML code-behind model is tight to 1 single XAML (view) isn't WPF/XAML flawed from the start in respect to MVC ?
I recommend looking at MVVM for use with WPF.
You're comparing MVC with Windows Forms to WPF with code behind - which is comparing an architectural pattern to a technology (without using a pattern). This is an unfair comparison.
I wrote a series on MVVM starting from a Windows Forms perspective that may help with understanding this. You'll find that the separation of View and ViewModel possible with WPF is far cleaner and simpler to implement than the Controller of MVC. It actually allows even more decoupled architectures with less implementation work.
Good question!
In MVC you state that a model can have multiple views, which is true. However, each view would have its own controller.
In MVVM a model can have multiple views, and in this case each may have their own view model.
However, in practical terms, if a ViewModel does not have any concepts that are highly specific to a certain view, it is entirely possible to re-use view models. In fact, I wrote an article on cross-platform XAML applications where I re-used code between three apps, one on WP7, one in Silverlight, the other WPF:
http://blogs.msdn.com/b/mikeormond/archive/2010/12/09/writing-cross-platform-xaml-applications.aspx
The views for each were very different, however I was able to re-use both the models AND the view models for all three apps.
WPF/XAML is a technology, and MVC is design pattern, it's good when technology is not limiting you to a specific pattern, and allows developer to choose what pattern to use. Same as programing languages allow's you to use any design pattern.
You can use MVC, MVP, MVVM, or YourMegaPattern with XAML\WPF. Technology should be above patterns, it's frameworks that are usually bound to a specific pattern.
For example you can take ASP .NET techology, and MVC framework based on it.

Alternatives to Model View ViewModel for WPF

I've just been reading Josh Smith's MVVM article and am working on a WPF application at the moment. I'm umming and ahing about transfering my work so far to MVVM but find the idea of working purely through databinding and ICommands without any UI event handlers, a little daunting in the sense that it could take a while to convert what I've done up to now.
I'm a self taught C#-er and I've yet to apply patterns such as this to my projects which have up to now just been using the code-behind model and separating out business logic and data access into separate assemblies.
I know that my code can get a little confused sometimes with regard to what belongs where so I was looking into patterns as a way to help me get things straight.
What other patterns might I look at? Any links to articles appreciated.
Some other presentation patterns that you may want to take a look at are Model View Presenter and Model View Controller.
Martin Fowler has a number of good articles on presentation patterns as does Jeremy Miller. Jeremy is coming out with a book on presentation patterns in the (hopefully) near future. Take a look at his wiki and Fowler's site.
http://martinfowler.com/eaaCatalog/
http://www.jeremydmiller.com/ppatterns/Default.aspx?Page=MainPage&AspxAutoDetectCookieSupport=1
My advice when adopting the MVVM pattern for WPF applications is to leverage off one of the existing toolkits such as MVVMLight by Laurent Bugnion. Once you get your head around messaging the loss of event driven code behind based logic seems plausible. It's still a little tedious in terms of LOB development if you're coming from an old-school forms over data background but once you are into the swing of it you will see the benefits and UI power that can be derived from bindings and value converters.

Why MVVM and what are it's core benefits? [duplicate]

This question already has answers here:
Why use MVVM? [closed]
(13 answers)
Closed 2 years ago.
Why we go for MVVM over MVC or MVP while dealing with WPF?
What extra benefit we get by using this?
Edit:
To be honest , today I had an interview and I have been asked this question. I answered like INotifyPropertyChanged , ICommand,IValue Convertor.. but he was not satisfied. Henceforth I have put up this question
Thanks in advance
I'll point you to a particularly useful video by Jason Dolinger.
Coming from a WinForms world, implementing any MVX style pattern seemed like more hassle than it was worth but after working with WPF for a couple of years now, I can honestly say that I wouldn't consider anything less. The whole paradigm is supported out-of-the-box.
First off, the key benefit is enabling true separation between the view and model. What that means in real terms is that if/when your model needs to change, it can without the view needing to and vice-versa.
Secondly, while your model may contain all the data you might need in your view, you may want to abstract that data in such a way that your model doesn't support. For example, say your model contains a date property. In the model it can exist solely as a DateTime object but your view might want to present it in a completely different way. Without the viewmodel you'd either have to duplicate the property in the model to support the view or modify the property which could seriously obfuscate the 'model'.
You can also use a viewmodel to aggregate parts of your model that exist in separate classes/libraries to facilitate a more fluent interface for the view to deal with. It's very unlikely that you'll want to work with data in your code in the same way that a user will want to or will want that data presented to them.
On top of that, you get support for automatic two-way data binding between the view and viewmodel.
There really is a whole bunch of extra stuff that I could bang on about but Jason say's it far better that I could so my advice is watch the video. After a few days of working like this, you'll wonder how you ever got by without it.
Good luck.
These are mine specific to MVVM
Increases the "Blendability" of your views (ability to use Expression Blend to design views). This enables a separation of responsibilities on teams that are lucky enough to have a designer and a programmer... each can work independent of the other.
"Lookless" view logic. Views are agnostic from the code that runs behind them, enabling the same view logic to be reused across multiple views or have a view easily retooled or replaced. Seperates concerns between "behavior" and "style".
No duplicated code to update views. In code-behind you will see a lot of calls to "myLabel.Text = newValue" sprinkled everywhere. With MVVM you can be assured the view is updated appropriately just by setting the underlying property and all view side-effects thereof.
Testability. Since your logic is completely agnostic of your view (no "myLabel.Text" references), unit testing is made easy. You can test the behavior of a ViewModel without involving its view. This also enabled test-driven development of view behavior, which is almost impossible using code-behind.
The other two patterns are really sort of separate in terms of the concerns they address. You can use MVVM with MVP and MVC (most good samples out there do some form of this).
In fact, MVP (w/ a Passive View, rather than a Supervising Controller) is really just a variant of MVVM, in my opinion.
WPF has better databinding than any other UI framework, which MVVM would be unruly without
MVVM provides unit testability and excellent view-agnosticism, which make it a good thing to use
Baked in support for ICommand and INotifyPropertyChanged are the two biggest benefits. Using MVVM makes it really easy to wire up the commands and plug data into the WPF UI. Things just work.
I personnaly see MVVM not as a benefit, but as an obligation for those who want to use WPF cool features.
WPF is very very heavily built with data binding at the core, to enable separation of UI from Model. But the way data binding is technically done in WPF is somewhat special, as it's tied to classes like:
DependencyProperty
INotifyPropertyChanged
ObservableCollection
Because of this you just can't really write a model the way you want using standard .NET technology. For example, the WPF TreeView is almost impossible to use w/o using data binding and templates. You just can't populate it simply like you would from a generic model in Winforms for example. It must be bound to a hierarchical model using ObservableCollection to represent a node's children.
So let's say V represents the XAML code and it's code-behind counterpart (so it's tied to WPF as a technology), and let's say M represents your model (so it's not tied to WPF UI technology in anyway).
Well, you'll never have this working properly under WPF with only these V & M.
You must add something between the two. Something that's WPF-compatible and understands your model. Something that speaks DependencyProperty, ObservableCollection and INotifyPropertyChanged. That's what's called VM.
As a side note, an alternative to MVVM is to build a V & M (w/o VM plumbing) combination with M being WPF-compatible but still with a reasonable UI independency. Historically, ObservableCollection was in the WindowsBase.dll assembly (that was shipped with WPF), so it really looked weird to bind a generic model to something tied to a UI technology. It's been moved back to System.dll since. Even then, it's sometimes hard to keep a pure VM model w/o tweaking the M specifically for WPF...
The ability of XAML code to databind, as well as the existance of triggers will break the MVP and MVC Patterns.

Should I use the Model-View-ViewModel (MVVM) pattern in Silverlight projects?

One challenge with Silverlight controls is that when properties are bound to code, they're no longer really editable in Blend. For example, if you've got a ListView that's populated from a data feed, there are no elements visible when you edit the control in Blend.
I've heard that the MVVM pattern, originated by the WPF development community, can also help with keeping Silverlight controls "blendable". I'm still wrapping my head around it, but here are some explanations:
http://www.nikhilk.net/Silverlight-ViewModel-Pattern.aspx
http://mark-dot-net.blogspot.com/2008/11/model-view-view-model-mvvm-in.html
http://www.ryankeeter.com/silverlight/silverlight-mvvm-pt-1-hello-world-style/
http://jonas.follesoe.no/YouCardRevisitedImplementingTheViewModelPattern.aspx
One potential downside is that the pattern requires additional classes, although not necessarily more code (as shown by the second link above). Thoughts?
I definitely think you should use the MVVM pattern for Silverlight applications - and one of the benefits of the pattern is that you can actually make your application really blendable through some simple techniques. I often refer to "blendability" as "design for designability" - that you use certain techniques to make sure your application looks great in Blend.
One of the techniques - like Torbjørn points out - is to use a dependency injection framework and supply different implementations of your external services depending on wether the code is being executed in Blend or in the Browser. So I configure my container to use a dummy data provider when the code is executing in Blend, and that way you get design time support for your list boxes, data grids etc.
The challenge is often how to set the DataContext declaratively - so I often end up using a service locator class a a "front end" to the IoC container. That way I can bind the data context to a property on the service locator.
Another technique is create some kind of ObjectDataSource control (non visual) that has two properties: Design Time DataContext and RunTime Data Context. The control does the job of detecting where is being executing, and then setting the Parent DataContext to the right object.
I'm not sure if I can answer your question, but I have foudn the article below very valuable. Jonas Follesø is using ninject to switch out his services when in design/blend mode. Very nice!
http://jonas.follesoe.no/YouCardRevisitedImplementingDependencyInjectionInSilverlight.aspx
I also agree with Jonas regarding MVVM with Silverlight. I do believe that MVP is also a good choice, but recently I have had time to try both MVP and MVVM with Silverlight and I am much happier with the results from MVVM. (Yep, I changed my mind the more I used MVVM). The VM abstracts the binding of the Model from the View (obviously) in MVVM which allows for more binding scenarios (at least cleaner ways to do them) than with MVP. That's just one aspect, though.
I'll be posting some examples of both MVP and MVVM with Silverlight on my site.
I've tried a few options and I'm settling on MVVM as the best choice for me. Blendability is an important point, and I also find the VM aspect intuitive for rigging up dynamic behaviors and procedural effects and animations (like Nikhil's Silverlight.FX). At one point I tried to avoid Blend altogether through fluent interfaces but am finding the coupling between UI and behavior too painful in the long-run. I want to design my UI in Blend and then add effects and other behaviors in code, this is proving to be the best pattern for me to follow so far.
I think many of us are waiting for the trailblazers to go ahead and create really good sample apps using MVVM in Silverlight (and WPF for that matter). There are a number of tricky areas such as the lack of ICommand in Silverlight, or the difficulty of interacting with animations starting and stopping only using data binding.
Its definitely a pattern to watch for the future though, and is worth trying out if you don't mind 'cheating' occasionally in the places where you can't quite figure it out.
I love the ViewModel pattern and highly recommend it. I have a couple of "getting started with ViewModel" types of posts on my blog.
ViewModel Pattern
HelloWorld.ViewModel
Binding Converts - VisibilityConverter
Silverlight Airlines with a ViewModel
I agree with Jonas. MVVM seems to be the model that works best for me (though John Papa thinks MVP makes more sense). I have an MSDN Article on this coming out in March that hopefully will answer the call for a good example.
BTW, I would like to see some cohesion in the MVVM Framework department. There isn't a good solution for a framework to follow yet. I like Jonas' (I think Jonas' is the FX Framework) but since its not WPF compatible it might not be the right choice for some.
I've been using MVVM lately on a couple of different Silverlight projects and it's been working really well, I would definitely recommend it. Jonas's post is a great place to start, I've recently blogged on my MVVM experiences too and created a really simple solution to demo the main touch points.
There's a very good Techdays 2010 video introduction to the MVVM pattern, clearly explained:
TechDays 2010: Understanding the Model-View-ViewModel pattern
Hanselminutes podcast by Laurent
For more complicated applications that require a higher degree of automated testing it definitely makes sense, and the move away from DependencyProperties to DataContext binding is a lot neater than it's ASP.NET counterpart.
The biggest challenge I've found with Silverlight is testing the actual UI (there's one commercial framework so far I think), and the huge tangle of event calls you get into when using WCF services (or the WebClient for that matter) with Silverlight.
I've always thought MVVM and PresntationModel http://martinfowler.com/eaaDev/PresentationModel.html are essentially the same thing. PresentationModel is a lot easier to say.
I've used it succesfully in java swing, windows forms, WPF and silverlight. If you think in terms of separation of concerns a Presentation Model makes a lot of sense. You have one class whose only concern is providing a Presentation friendly Model.It really doesn't matter what technology is used to show it on the screen. It might change some implementation details but splitting the concerns apart is a good idea no matter how you show the information.
Because of that separation you can easily write tests against your presentation model regardless of the view technology. So that's a plus.
With the Feb 2009 release of Prism v2 by P&P, even better support for MVVM now available for Silverlight and WPF. See microsoft.com/compositewpf for more details.
Take a look at my article about MVVM and Silverlight in real life projects and decide for yourself.
http://alexburtsev.wordpress.com/2011/03/05/mvvm-pattern-in-silverlight-and-wpf/

Resources