MVC tutorial/walkthrough that is meant for people familiar MVVM? - wpf

I'm used to working in WPF with the MVVM design pattern, but I've recently been asked to do something in ASP.Net. I'd like to try using MVC because I saw it referenced a lot when learning MVVM, however I don't know anything about it.
I can find plenty of sites that are meant to explain MVVM to someone who is familiar with MVC, however I cannot find a good one that explains MVC to someone who is used to MVVM. There are sites that explain MVC on it's own, but I'm having a hard time understanding them because my mind keeps trying to apply MVVM logic.
So, are there any good sites that can explain MVC in terms that someone used to MVVM can understand? Or can someone explain it to me here?

When you come from MVVM pattern and start with MVC pattern (especially ASP.NET MVC) I would suggest to think of the "MVC" pattern better as the "VMVC" because the "M" in MVC is not the Model meant by the "M" in MVVM. It actually corresponds to the ViewModel. I don't know if that represents the general definition of MVC but it is true and the most used and best practice when you work with ASP.NET MVC (although you see every now and then examples or questions here on SO where domain entities are used in a view (which is sometimes exactly the reason for the problem described in the question)).
The first thing I usually do when I create a ASP.NET MVC project from one of the Visual Studio templates is to rename the created folder "Model" into "ViewModel". If you take a look what the template code does with those "Models" you see that they are directly used for the views, that they have data annotations for input validation, for display formats and perhaps field naming on the view. These annotations are partially used directly by the HTML helpers to produce the HTML and don't represent domain or business logic. In other words: they are ViewModels for an Razor/HTML view in the same sense as you use ViewModels in MVVM for your XAML views in WPF/Silverlight/Phone7.
The domain "Model" is actually not part of the MVC pattern as it is a part in the MVVM pattern. So, the abbreviations are somewhat misleading when you compare MVVM with MVC. As a very simplified "translation table" one could say:
MVVM MVC
---- ---
M -> Domain Model not part of the pattern
V -> View (XAML) V -> View (HTML, Razor)
VM -> ViewModel M -> ViewModel
not part of the pattern C -> Controller
I'm not sure about the corresponding thing of a controller in MVVM. In MVC the controller is usually the module which translates domain objects into ViewModels and then into views (and vice versa) - schematically:
ControllerActionForGetRequest ( params )
{
objects = GetDomainObject(params) - entities, queryables or DTOs
viewModel = CreateViewModelFromDomainObjects(objects)
view = CreateViewFromViewModel(viewModel)
}
ControllerActionForPostRequest ( viewModel )
// ModelBinder makes "viewModel" from HTML input fields, etc
{
if (IsValid(viewModel))
{
data = CreateDomainObjectsOrDtosFromViewModel(viewModel)
WriteData(data) - back to data store
RedirectToActionForGetRequest
}
else
GoBackToView
}
Which part in MVVM has this responsibility? I am not sure. I have seen designs where the ViewModel holds some reference to a repository, pulls out the Model (Domain Model) to fill its own properties and writes back into the repository through ICommand handlers. That would mean that ViewModels in MVVM also have the responsibility to be a "controller" while ViewModels in MVC are much simpler: They are more or less only property bags with metadata to provide and format data for a view.
As a final note: Personally I found the MVVM pattern with WPF much more difficult to master than the MVC pattern. ASP.NET MVC is designed from the ground to support MVC pattern-friendly development and there is not need (or even not the possibilty) to leave this way. This is not the case for WPF. The original design was built with views and code-behind files in mind, not with the MVVM pattern. I found often situations where it was very difficult to bind view elements or attributes to a ViewModel and handling this in code-behing files was much easier, thereby violating MVVM principles a bit.
I would think that you won't have any problems to get into MVC when you have experience with the MVVM pattern.

Rachel, I have not run into any ASP.NET MVC sites that are geared towards the MVVM crowd, but from first hand experience I thought the ASP.NET MVC Music Store was absolutely fantastic.
I am originally a WebForms developer, and I can absolutely attest to having a certain technology in the back of your mind while learning another, forcing your logic to shift a certain way. That was especially difficult going from Webforms -> MVC. The best advice I have is to just analyze every aspect of that Music Store tutorial as a separate entity.
Good luck, and I hope that helps.

Related

can I use MVP with MVVM

I got a source-code and was trying to understand it using the documentation.
In the documentation, it says that the code has used both MVP and MVVM in combination to make the project.
can someone answer these following questions of mine:-
What is point of using two design pattern in a single project(both MVP and MVVM) ?
How can I simple know by looking at some source code if it is made using mvp,MVC or any other design pattern?
can you tell me about ....what are layers in any design pattern? how many minimum layers are we gonna use to make a project in any design pattern? and what is the benefit of having as many layers as possible?
what is the difference between java logic and android logic? coz we're simply separating apart java from android logic for the better testing environment by using any design pattern. Give example.
If the code is using MVC you will see the following:
Models: Models contain data information. Does not call or use Controller and View. Contains the business logic and ways to represent data. Some of this data, in some form, may be displayed in the view. It can also contain logic to retrieve the data from some source.
Controller: Acts as the connection between view and model. View calls Controller and Controller calls the model. It basically informs the model and/or the view to change as appropriate.
View: Deals with UI part. Interacts with the user.
For MVVM (Model View View Model):
ViewModel:
It is the representation of the state of the view.
It holds the data that’s displayed in the view.
Responds to view events, aka presentation logic.
Calls other functionalities for business logic processing.
Never directly asks the view to display anything.
Now let's see MVP (Model View Presenter):
Similar to traditional MVC but Controller is replaced by Presenter. But the Presenter, unlike Controller is responsible for changing the view as well. The view usually does not call the presenter.
Now your questions:
What is point of using two design pattern in a single project(both MVP and MVVM)?
Ans: It may be the need. Also they both are very closely related and as I said it might be useful to mix the two to solve a particular type of problem.
How can I simple know by looking at some source code if it is made using mvp,MVC or any other design pattern?
Ans: Read the explanation I provided. Try to see which pattern closely matches the code. There might be multiple design patterns used.
can you tell me about ....what are layers in any design pattern? how many minimum layers are we gonna use to make a project in any design pattern? and what is the benefit of having as many layers as possible?
Ans: There are no such hard and fast rule on number of layers. To make your code reusable, maintainable, open for extension and follow the best practices of software engineering it is important that you follow the design patterns.
what is the difference between java logic and android logic? coz we're simply separating apart java from android logic for the better testing environment by using any design pattern. Give example.
Ans: There is no such thing called Java logic and Android logic.

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.

Is MVVM pattern a symbiote of MVC + PAC patterns?

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.

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.

UI Design Pattern for Windows Forms (like MVVM for WPF)

MVVM is most commonly used with WPF because it is perfectly suited for it. But what about Windows Forms? Is there an established and commonly used approach / design pattern like this for Windows Forms too? One that works explicitly well with Windows Forms? Is there a book or an article that describes this well? Maybe MVP or MVC based?
I have tried MVP and it seems to work great with windows forms too.
This book has an example of windows forms with MVP pattern (sample payroll application). The application is not that complex but will give you an idea about how to go about creating it.
Agile Principles, Patterns, and Practices in C#...
You can get the source code at
Source Code
EDIT:
There are two variations of the MVP pattern
(a) Passive view and (b) supervising controller
For complex databinding scenarios I prefer to go with the Supervising controller pattern.
In supervising controller pattern the databinding responsibility rest with the view. So,for treeview/datagrid this should be in the respective views, only view agnostic logic should moved on to the presenter.
I'll recommend having a look at the following MVP framework
MVC# - An MVP framework
Don't go by the name (it's an MVP framework).
Simple winforms MVP video
Winforms - MVP
An example of dealing with dropdown list
MVP - DropDownList
Simple treeview binding example (poor man's binding). You can add any treeview specific logic in BindTree().
Below is the code snippet.... not tested, directly keyed in from thought....
public interface IYourView
{
void BindTree(Model model);
}
public class YourView : System.Windows.Forms, IYourView
{
private Presenter presenter;
public YourView()
{
presenter = new YourPresenter(this);
}
public override OnLoad()
{
presenter.OnLoad();
}
public void BindTree(Model model)
{
// Binding logic goes here....
}
}
public class YourPresenter
{
private IYourView view;
public YourPresenter(IYourView view)
{
this.view = view;
}
public void OnLoad()
{
// Get data from service.... or whatever soruce
Model model = service.GetData(...);
view.BindTree(model);
}
}
As it has already said, i always worked in a MVP pattern when using Winforms. But the design pattern you will use not mean you will use right. There is loads of anti-pattern attached to MVP.
If you want to starts everything in a good manner, you have to use the framework for building smart client. So i will recommend to use that design and practices: Smart Client Software Factory http://www.codeplex.com/smartclient
You have a discussion here about the current smart client frameworks : http://codebetter.com/blogs/glenn.block/archive/2008/05/10/prism-cab-and-winforms-futures.aspx
PS: I like this post on the MVP anti-patterns: http://blog.mattwynne.net/2007/06/13/mvp-smells/
Hope this helps
The Model-View-ViewModel (MVVM) Pattern is a design pattern. Per definition a design pattern shows a common solution in the object-oriented world and this solution can be applied in various platforms (WPF, WinForms, Java Swing, etc.). I agree that MVVM is best used with WPF because it leverages the strong binding capabilities. However, Windows Forms supports data binding as well.
The WAF Windows Forms Adapter shows how to apply the MVVM Pattern in a Windows Forms application.
I have written about a variation of MVP/MVVM design patterns called MVP-VM, which is a tailor made solution for winforms applications that require full testing coverage and use data binding as main mechanism for keeping the presentation updated with model data.
MVVM for .NET Winforms
MVVM (Model View View Model)
introduces similar approach for
separating the presentation from the
data in an environment that empowers
data binding (WPF). Since .NET
framework 2.0 already offers advanced
data binding infrastructure that also
allows design time binding of
application objects - the ‘View Model’
entity can fit quite well in MVP based
environment.
I asked this same question to two of my techies co-workers: is MVVM for WindowsForms possible? Both gave me the exact same answer: "No way! WindowsForms is missing the rich bindings of WPF and Silverlight (OneTime, OneWay, TwoWay, OnewayToSource) and it is also missing the TypeConverters."
Screen Activator Pattern for WindowsForms - you can find it here, ported from Caliburn.Micro by jagui
Rich Bindings and TypeConverters - Truss by Kent Boogaart, does it in an UI independent way
Commands - WPF Application Framework (WAF) has a WafWinFormsAdapter project that takes care of some MVVM stuff namely commands
Again, can we have MVVM for WinForms?
Yes we can. We have all the pieces. We just have to glue them together.
I believe that MVP is a pattern well-suited to WinForms development - as is partly evidenced by it's use in CAB - Microsoft's framework for WinForms.
I use MVP in WinForms to extract code out of the View - because I can't test the View code. And also to enable code that needs to be reused (or is duplicated) to stay out of the View where it can't be shared.
I can refer to my own project where I use the MVP pattern ExceptionReporter.NET. Though I'm sure I don't use it perfectly.
You mentioned MVVM working for WPF - I think the reason for that is because of strong data-binding support. If you were not using data-binding in WPF (and it's certainly not compulsory) then you could choose MVP. The point being that MVP is a strong choice for any client-side application. And possibly a 'better' choice, even in WPF, if you plan on sharing code between projects that aren't WPF.
For more evidence of the value of using MVP in WinForms see Boodhoo's video presentation on using MVP:
http://www.bestechvideos.com/2008/06/29/dnrtv-show-14-jean-paul-boodhoo-on-model-view-presenter
And an MSDN article by the same author at http://msdn.microsoft.com/en-us/magazine/cc188690.aspx
The BindTree method seems a little
flawed to me. Suddenly the the View
knows abou the Model. Is that a good
thing? There must be tons of poeple
being confronted with these kind of
problems. I am surprised that there
aren't any books about it. Since there
are books about everything in the .NET
world.
These Design not about hiding the model rather precisely defining the interactions between the different layers of the applications. You can change the backend completely and as long as you pass a Model through Bindtree your UI will continue to work.
Now class Model may be a poor choice of a name in the example that Rajesh gives. It can be TreeData, or RecordsData. However you define it, it has what you need to using the binding mechanism of Winforms to bind a specific control to the underlying data.
The best site to browse for this kind of material is here. Martin Fowler has collected a variety of useful UI design pattern and enterprise design patterns.
Again the key to this is the use of interfaces to precisely define how each layer interact with each other.
In my own application (a CAD/CAM applications used to run metal cutting machines) my structure looks like this.
Forms implementing form interfaces
UIDLL with views implementing view
interfaces that interact with forms
through the form interface. The
specific views register themselves
with UIViewDLL Views executes Command Objects found
in command libraries that interact
with the Model.
Command libraries; lists of
commands implementing ICommand.
The command that interact with
views do so through the interfaces
exposed in UIViewDLL.
UIViewDLL; exposes the View Interfaces
used by the commands.
Model; the classes and collection that
make up core data structures of my
application. For me these are things
like material, cuttingpaths, shape,
sheets, torches, etc.
Utility; a DLL that has commonly used
utility classes used by my company
that span different application. For
example complex math functions.
You can use Enterprise Architecture, Patterns and Practices as the starting point, although they are slightly dated.
Under General Guidance there is Application Architecture for .NET: Designing Applications and Services, which is a good introduction to .NET ways and layered N-tier application.
alt text http://i.msdn.microsoft.com/ms954595.f00aa01%28en-us%2CMSDN.10%29.gif
For more formal "patterns", there is Enterprise Solution Patterns Using Microsoft .NET.
(source: microsoft.com)
To name a few,
Model-View-Controller
Intercepting Filter
Three-Layered Services Application
The first good explanation of UI design patterns I read was in Jeremy Miller's blog - Building Your Own CAB. It describes the common patterns - Passive View, MVP, etc. and addresses some of the ways you might implement them in C#.
You can try MugenMvvmToolkit that allows to use a "pure MVVM" for WinForms.
Due to the fact that it supports bindings on all platforms, all of the native binding features available for WPF platform available on all platforms (include WinForms).

Resources