How to say ViewModels in MVVM? - c#

The MVVM and MVC are promation models similar.
If the logic in MMVM are located in the ViewModels like Controllers in MVC.
How would you call a ViewModel in the MVVM pattern?
I mean ViewModels, like specific created entities for show data.

It can be called the Model for the View.

Related

What should go in [Name]View.xaml.cs files?

I am creating a desktop application using WPF & Caliburn MVVM. In my "Views" directory, I have several files that follow this pattern:
ExampleView.xaml <= this is the actual UI design...
ExampleView.xaml.cs <= what goes here?
Then I also have a ViewModel for each view.
Can somebody explain what should go in the ExampleViewModel.cs and what should go in the ExampleView.xaml.cs? It looks like my ViewModels inherit from "Screen", where my ExampleView.xaml.cs inherits from "Window".
Thanks!
That is your "Codebehind" for the view. Many people use the codebehind to interact with their view. However, many people prefer a decoupled approach called MVVM that uses a viewmodel instead. The MVVM architecture is more unit-testing friendly and easier for code reuse.
So in summary, the ExampleView.xaml.cs is used to interact with your controls on the view.
Well, your views can be inherited from Window or UserControl or other WPF classes. In MVVM the view is the user interface and the view model is an abstraction of the view exposing public properties and commands. Instead of the controller of the MVC pattern, or the presenter of the MVP pattern, MVVM has a binder. In the view model, the binder mediates communication between the view and the data binder.
But your view can contains UI-logic that is not depend on data from ViewModel. This logic can be added to [Name]View.xaml.cs files as View's (and inner control's) event handlers, for example OnLoad, OnResize. It calls Code behind.
I just wrote a couple WPF apps using MVVMLight Nuget and all of my [views].aspx.cs only have the auto-generated code. I didn't need to put anything in the views code behind because it goes in my view model. I also converted some web apps and a couple windows 8 store apps to MVVM and almost completely eliminated all code from the views code behind.
The only time I have put something in the views code behind is maybe to instantiate a view model to go along with that view in very isolated situations.
There is much debate out there whether to have zero code to follow MVVM best or having some code in the code behind is okay. Personal preference and based on work load, complexity, timelines, etc.

Is MVC specific to Web application?

I might sound very naive here.
Does MVC applies only for Web application? I have worked on MVVM for quite some days and I see people telling MVVM with WPF is a windows version of MVC.
What would be the alternate solution for MVC or MVVM as of now? Isn't any design patterns better than this discovered yet?
I am very specific to .NET here.
No. Mvc is not specific to web applications or .net.
All the mvx patterns have in common the view and the model. The view is concerned with the display of the data, the model delivers the data.
In mvc the data is pulled out of the model by the controller who puts it in the view. The user interactions where delegated from the view to the controller who acts on the model. View and Model does not now of each other.
With new technologies like data binding mvc becomes bulky. Mvp was introduced. It allows for the view to use data binding directly to the model, bypassing the controller who is now called presenter. The presenter orchestrates view and model but has fewer responsibilities.
Later mvp was replaced with mvvm. Here model and view are again completely separated and do not know each other. The Viewmodel is a model that is designed specific to the views needs. It also handles the responsibilities of the controller.
In my opinion the asp.mvc is not truly mvc. You hand out either objects of the model, or special desinged viewmodels to the razor views, so its either more mvp or mvvm with adding a controller to manage the other parts.

MVVM in ASP.NET MVC

I am confused about using MVVM in ASP.NET MVC.
Views in ASP.NET MVC can only take in one Model class. I have a view and controller that needs to operate on data from more than one model class, so I created a ViewModel class that contains instances of these model classes.
I could populate the ViewModel in the Controller, but I have heard people say that you should keep your controllers thin. So I have created a service class to populate the viewmodel. This service class is then injected into the controller.
1) Is this the right way of going about things doing MVVM in MVC? Can people point me to some resources about this? Or have I got the wrong end of the stick about MVVM?
2) Does presentation logic go in the ViewModel? For instance, in the View I could use Razor to do something like #if (Model.Order.Count > 10) show an unordered list. Or do I create a boolean field in the ViewModel called something like ShowUnorderedList and populate this value in the service?
3) If I do that kind of presentation logic in the view, how do I unit test the logic in the view?
Have done more research, my confusion arose from the fact that I thought that the MVVM pattern was what you called it when you used ViewModels with MVC. However, this is not the case. I am not using MVVM - I am just using ViewModels with ASP.NET MVC.

What is difference between MVC, MVP & MVVM design pattern in terms of coding c#

If we search Google using the phrase "differences between MVC, MVP & MVVM design pattern" then we may get a few URL's which discuss the difference between MVC MVP & MVVM design pattern theoretically like:
MVP
Use in situations where binding via a "dataContext" is not possible.
Windows Forms is a perfect example of this. In order to separate the view from the model, a presenter is needed. Since the view cannot directly bind to the presenter, information must be passed to the view via an interface (IView).
MVVM
Use in situations where binding via a "dataContext" is possible. Why? The various IView interfaces for each view are removed which means less code to maintain.
Some examples where MVVM is possible to include WPF and javascript projects using Knockout.
MVC
Use in situations where the connection between the view and the rest of the program is not always available (and you can’t effectively employ MVVM or MVP).
This clearly describes the situation where a web API is separated from the data sent to the client browsers. Microsoft’s ASP.NET MVC is a great tool for managing such situations and provides a very clear MVC framework
But I have not found a single article which discusses the difference theoretically along with sample code.
It would be really nice if I get an article that discusses the difference between these 3 design patterns (MVC, MVP & MVVM) along with code.
I'd like to get my hands on the source code of 3 similar CRUD apps that have been implemented by these three design patterns (MVC, MVP & MVVM). So that I can go through the code and understand how one should write code for these three design pattern (MVC, MVP & MVVM).
So if any such article exists which discusses how code would look different for these 3 design patterns (MVC, MVP & MVVM) then please redirect me to that article.
Some basic differences can be written in short:
MVC:
Traditional MVC is where there is a
Model: Acts as the model for data
View : Deals with the view to the user which can be the UI
Controller: Controls the interaction between Model and View, where view calls the controller to update model. View can call multiple controllers if needed.
MVP:
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.
MVVM
The difference here is the presence of View Model. It is kind of an implementation of Observer Design Pattern, where changes in the model are represented in the view as well, by the VM.
Eg: If a slider is changed, not only the model is updated but the data which may be a text, that is displayed in the view is updated as well. So there is a two-way data binding.
MVC, MVP, MVVM
MVC (old one)
MVP (more modular because of its low-coupling. Presenter is a mediator between the View and Model)
MVVM (You already have two-way binding between VM and UI component, so it is more automated than MVP)
Great Explanation from the link : http://geekswithblogs.net/dlussier/archive/2009/11/21/136454.aspx
Let's First look at MVC
The input is directed at the Controller first, not the view. That input might be coming from a user interacting with a page, but it could also be from simply entering a specific url into a browser. In either case, its a Controller that is interfaced with to kick off some functionality.
There is a many-to-one relationship between the Controller and the View. That’s because a single controller may select different views to be rendered based on the operation being executed.
There is one way arrow from Controller to View. This is because the View doesn’t have any knowledge of or reference to the controller.
The Controller does pass back the Model, so there is knowledge between the View and the expected Model being passed into it, but not the Controller serving it up.
MVP – Model View Presenter
Now let’s look at the MVP pattern. It looks very similar to MVC, except for some key distinctions:
The input begins with the View, not the Presenter.
There is a one-to-one mapping between the View and the associated Presenter.
The View holds a reference to the Presenter. The Presenter is also reacting to events being triggered from the View, so its aware of the View its associated with.
The Presenter updates the View based on the requested actions it performs on the Model, but the View is not Model aware.
MVVM – Model View View Model
So with the MVC and MVP patterns in front of us, let’s look at the MVVM pattern and see what differences it holds:
The input begins with the View, not the View Model.
While the View holds a reference to the View Model, the View Model has no information about the View. This is why its possible to have a one-to-many mapping between various Views and one View Model…even across technologies. For example, a WPF View and a Silverlight View could share the same View Model.
MVP:
Advantages:
Presenter will be present in between Model and view.Presenter will fetch data from Model and will do manipulations for data as view wants and give it to view and view is responsible only for rendering.
Disadvantages:
1)We can't use presenter for multiple modules because data is being modified in presenter as desired by one view class.
3)Breaking Clean architecture because data flow should be only outwards but here data is coming back from presenter to View.
MVC:
Advanatages:
Here we have Controller in between view and model.Here data request will be done from controller to view but data will be sent back to view in form of interface but not with controller.So,here controller won't get bloated up because of many transactions.
Disadvantagaes:
Data Manipulation should be done by View as it wants and this will be extra work on UI thread which may effect UI rendering if data processing is more.
MVVM:
After announcing Architectural components,we got access to ViewModel which provided us biggest advantage i.e it's lifecycle aware.So,it won't notify data if view is not available.It is a clean architecture because flow is only in forward mode and data will be notified automatically by LiveData. So,it is Android's recommended architecture.
Even MVVM has a disadvantage. Since it is a lifecycle aware some concepts like alarm or reminder should come outside app.So,in this scenario we can't use MVVM.
The image below is from the article written by Erwin van der Valk:
The article explains the differences and gives some code examples in C#
MVC vs MVP vs MVVM
MVC - Model View Controller
View(.xml, .storyboard) - Controller(Activity, Controller) - Model(Others)
View - rendering UI
Controller is bound with View
MVP - Model View Presenter
View(.xml, .storyboard, Activity, Controller) - Presenter(plain Class) - Model(Others)
MVVM - View Model ModelView
View(.xml with data, view.swift) - ModelView(ViewModel, ObservableObject) - Model(Others)
Android DataBinding, SwiftUI
MVVM uses data binding or two way data binding to connect View and ModelView based on Observation
Sometimes in UI world(iOS, Android) you can find additional object(Router, Navigator, Coordinator) which is responsible for navigation and setting dependencies

MVVM ViewModel lots of properties

I am new in MVVM and I am developping an application. I have a form view with a lot of property. About 50. I can not seperate these into usercontrol because I would break the mvvm principles.
I can not seperate these into model, because these contains logic. PropertyChange, Error change and these would not be poco classes, and these are not the model.
Would it be nice If I kept 60 property in a same viewmodel?
Do I think it wrong?
How would you organize these?
I can not seperate these into usercontrol because I would break the mvvm principles.
I'm not sure what you mean by this. Essentially you'll want to use view composition and break down the view model and views into constiuent parts.
A view is a WPF UserControl (or Window), so if you're using MVVM then you're using UserControl's, it's just conceptually they are considered as views in the pattern.
I would also recommend that you use an MVVM framework if you're using the MVVM pattern, and something like Caliburn.Micro makes view composition incredibly easy.
I would also not recommend using dependency properties for view models, use INotifyPropertyChanged instead.
Most MVVM frameworks provide a base view model type which includes a lambda based method to invoke the PropertyChanged event, thus aiding refactoring.
Please don't use PropertyChanged for 60 Properties. Use DependencyProperty.
For terms of usabilty use the propdp Shortcut from Visual Studio and press Tab twice.
Please refer to this link:
http://www.codeproject.com/Articles/62158/DependencyProperties-or-INotifyPropertyChanged

Categories