What is the difference between the controller in mvc and viewmodel in mvvm




















Just a learner TStamper TStamper The sentence "The controller is replaced with a View Model" is not correct. In MVVM what does the role of the controller is databinding or binding by convention if you use that.

DaniCE: Josh Smith: If you put ten software architects into a room and have them discuss what the Model-View-Controller pattern is, you will end up with twelve different opinions. OmShankar The 11th isn't from yourself. There are 10 total people, and 12 total opinions. The adage is meant to imply that the definitions of these patterns is so open to interpretation that at least two people will be confused enough to have more than one opinion.

DaniCE Well this is actually the point of WPF's data binding, and the Microsoft invented MVVM, in that one can bypass the controller completely, claiming the sentence "The controller is being replaced with a View Model" to be incorrect just because there is a controller behind the scenes, is basically like claiming a statement "Higher level language replace the use of cryptic machine code with more readable ones" to be incorrect because behind the scenes machine language is still being used Chris Ballance Chris Ballance I think the paragraph you quoted sums it up nicely IMHO.

I thought the view was always supposed to bind to the controller in MVC, no matter what. Think of it as of a robot - Model represents the position of robot's joints, View is a LCD monitor on which you see the robot, Controller is e. In such setup, View depends on Model, i. I would say it like this: The model is closet thing to DB schema.

When a query is run it can project the data into strong types at the model layer. The viewmodel is collections of things, including model objects, but can and does hold view state with respect to the data. The controller is simply a traffic cop between the viewmodel and the view and of course the view is only concerned with view states. Add a comment. Here's a crucial section: In the Model-View-ViewModel design pattern, an app is composed of three general components. Mat Mat 7, 4 4 gold badges 38 38 silver badges 56 56 bronze badges.

Rap 6, 2 2 gold badges 47 47 silver badges 81 81 bronze badges. George R George R 3, 3 3 gold badges 31 31 silver badges 37 37 bronze badges. The term is the correct one i think.

Therefore we need ModelView which acts as a local, unsaved working set of data extracted from model using e. The view does indeed "read" the model data because it's already been put there by the controller. I like to refer to it as a "data injection" by the controller as it's really the controller that is in charge. All the view does in render and fire events in my mind. I apologize but disagree with the MVVM interpretation. A ViewModel has no idea about a View or what a View will look like or how it will respond and a Model likewise has no idea of a ViewModel.

In fact, a View shouldn't even know of a Model either, just a ViewModel. Model should represent data and application state, ViewModel should translate the state to UI capable data I recommend all primitives at this point and a View should react to the ViewModels translation. The data will often be the same but it should still be wrapped and re-delivered via a ViewModel and no controllers exist.

Amazingly detailed and accurate answer! Made it crystal-clear for me. I recently 2 years ago was able to put my own experience into a summarized game plan and lead a team start to finish doing so and we were extremely successful. That said, I can't point you into any one spot and apologize. I can say that you are correct, because of the various opinions it is very confusing but, IMO, with MVVM it's to be as generic as possible. ViewModel, to me, are best used like an API, but with strict communication.

Follow the game plan for binding, editing, commanding, etc. If the View needs extra logic to function a specific way, that has nothing to do with the app or data such as an animation or a dropdown box..

Again, there's a plethora of opinions and this is just mine but I have a strong background here and a solid track record so far. I have example apps that I don't mind sharing and or wouldn't mind setting up a simple show and tell for you or anyone else if wanted or curious. Show 2 more comments. 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. View: Deals with UI part. Interacts with the user.

Responds to view events, aka presentation logic. Calls other functionalities for business logic processing. Never directly asks the view to display anything. Pritam Banerjee Pritam Banerjee Holmes Queen 2 2 gold badges 6 6 silver badges 17 17 bronze badges. Ali Nem Ali Nem 4, 1 1 gold badge 38 38 silver badges 37 37 bronze badges.

There's a great answer under all the flavor text here With some formatting and throwing out small talk between components this could be the best one on this page. Knockout is all about data-binding on the client side. I stated this, in , because far too many people in the community accepted this answer.

Cheng Cheng 8 8 silver badges 12 12 bronze badges. They were way ahead of their time apparently Actually, saying it originated from Martin Fowler's Presentation Model isn't accurate. It's very difficult to determine which came first, but both patterns allowing that they are really the same pattern were arrived at independently and at roughly the same time.

Select querystring, parms for direct ADO access to inject queries and get back strong types. This addresses the EF is slow argument. The ViewModel gets the data and does the business rules and validation The controller on post back will cal the ViewModel Post method and wait for results. The controller will inject the newly updated Viewmodel to the View. The View uses only strong type binding. The view merely renders the data, and posts events back to the controller. In clarification of item 6 above by request Community Bot 1 1 1 silver badge.

Can you clarify item 6? I realises you are covering ASP. Net only, but it appears to be adding an unwanted dependency to the ViewModel. A code pseudo-code? It contains the datacontext and tracks all selected item information as well as can contain all validation logic using the IValidatableObject interface.

The ViewModel interfaces with the DB at the model layer which can use strong typed models. But the controller of MVC is much cleaner, it is essential a routing handler. Net "controllers" i. It is the Views, Models, ViewModels and business logic that need to be reusable. I would have thought treating the business logic modules as service providers, not as controllers, would be a better option.

But you are talking about the "ViewModel" in Asp. Two different things. The ViewModel has a reference to the Model but not vice versa. The View has no reference to the Model and vice versa.

If you are using a controller, it can have a reference to Views and ViewModels , though a Controller is not always necessary as demonstrated in SwiftUI. Data Binding : we create listeners for ViewModel Properties so that data can flow from the view to the model through the view model. Its clear how the view gets data from the model, by reading its own properties. Data Binding is how to detect events within the view and feed them back to the model.

Both have models. Both have views. How can you trust that a unit test of a view controller is comprehensively safe without testing all the methods and listeners simultaneously? You can't wholly trust the unit test results. Advantages of MVC Providing business logic within the controller reduces the need for branching and therefore statements are more likely to run on the cache which is more performant over encapsulating business logic into ViewModels. Providing business logic in one place can accelerate the development process for simple applications, where tests are not required.

I don't know when tests are not required. Providing business logic in the ViewController is easier to think about for new developers. ScottyBlades ScottyBlades 8, 2 2 gold badges 53 53 silver badges 68 68 bronze badges. Best explanation — p Nishu Tayal Rinat Galyautdinov Rinat Galyautdinov 2 2 silver badges 2 2 bronze badges.

Why Kafka Is so Fast. Emil Koutanov in The Startup. Serhii Zahranychnyi in The Startup. Better Code Reviews. James Ford in The Startup. Priyanka Mane in Serious Scrum. Stein Janssen in Level Up Coding. Review of the Advanced Web Application Architecture. This idea is false, and is the result of unfortunate naming. In fact, every software project has a Controller piece, since every software project manipulates data. The core difference between the two architectures lies in the richness and complexity of the ViewModel.

If the client wants to see different data, or to temporarily manipulate the data it sees, the View must send a new request to the Controller. Create a free Team What is Teams? Learn more. Ask Question. Asked 10 years, 2 months ago. Active 8 years, 5 months ago. Viewed 17k times. Improve this question. Kazark 1, 1 1 gold badge 16 16 silver badges 35 35 bronze badges.

TomCaps TomCaps 2 2 gold badges 6 6 silver badges 13 13 bronze badges. Add a comment. Active Oldest Votes. The controller and the ViewModel differ in various ways.

Improve this answer.



0コメント

  • 1000 / 1000