32
votes
Accepted
Clean Architecture: What is the View Model?
Is this the same thing as the 'ViewModel' from the Model-View-ViewModel (MVVM) design pattern
Nope.
That would be this:
That has cycles. Uncle Bob has been carefully avoiding cycles.
Instead you ...
12
votes
Accepted
The right place for "app logic" in MVVM context
In MVVM, the business logic is built into the Model. The ViewModel is there to bridge between the View and the Model, so it's logic only pertains to driving the display and updating the model from ...
12
votes
Refactoring a legacy codebase with a god Repository and incomplete Clean Architecture
I see you've identified the following problems:
Repository layer sends raw data from the network api to the ViewModel forcing VM to do transformation and formatting making VM hard to test
A "...
11
votes
Accepted
When to use The Messenger (Mediator) Pattern in MVVM design
Those explanations are a bit confusing yes.
Technically the Mediator Pattern doesn't really change anything fundamentally about the relationships between your objects. What it does do is it makes the ...
10
votes
Accepted
WPF UserControl Reuse With MVVM
To begin with, this answer seriously lacks theoretical support (i.e. explaining why). I would very much second this answer instead. It is much under-voted because it was posted more than a bit later.
...
9
votes
In MVVM, how much of the business logic should reside in the View Model, and how much should reside in the Model?
Over ten years I have developed using MVVM and WPF I agree with Robert Harvey.
In the beginning, and I think most developers fall into this pattern, I started with very large view models. In time I ...
8
votes
How to choose NOT to use a framework (Caliburn.Micro, etc.) in a given MVVM application?
My first experience with WPF has been using Caliburn.Micro so this is probably quite different from most developers. I have found both WPF and Caliburn.Micro to be quite a steep learning curve, coming ...
8
votes
Clean Architecture: What is the View Model?
I find this problem too confusing and it would take lots of text and time to properly explain the problem as I believe you misunderstand both Martin's Clean Architecture and MVVM.
First thing to note,...
8
votes
Accepted
In MVVM, how much of the business logic should reside in the View Model, and how much should reside in the Model?
To determine the best course of action, one only needs to examine basic principles of OOP, SOLID and Separation of Concerns. Once that is done, the natural order of MVVM becomes obvious.
Applying ...
7
votes
Accepted
What's the M in MVVM?
In MVVM the Model is supposed to hold data and business/validation logic.
Taken from the Microsoft documentation on the pattern:
The model in MVVM is an implementation of the application's domain ...
7
votes
Accepted
How to avoid duplication of types in MVVM
CRUD operations in MVVM do seem unnecessarily redundant, don't they? But the situation changes when actual business operations are involved.
Let's have a look at a different problem domain.
public ...
6
votes
Accepted
MVVM. Is it a code smell when view model has properties with names show/hide/display that semantically belong to view?
Sometimes this can just be a naming issue.
For example, calling something ShowDialog in the ViewModel when what you mean is AskTheUserForAFilePath, which is independent of how the View handles that ...
5
votes
Duplicating Domain Model in View Model or not
MVVM tends to become a bit clunky when the application is straight forward (like the 1-1 mapping of fields you mention). The benefits come with apps of relative complexity and medium and big size.
...
5
votes
Accepted
Where is it better to implement Copy/Cut/Paste in MVVM?
I would add a clipboard service
IClipboard
{
void AddProgram(Program p);
Program GetProgram(string id);
..... etc
}
and inject this into the ViewModel, which would have the copy paste ...
4
votes
State of selected items in view or viewmodel?
Typically, a SelectedItem target binding belongs on the ViewModel.
This is because you usually want to do something with the SelectedItem (edit, delete, etc).
Having the SelectedItem on the VM also ...
4
votes
Accepted
State of selected items in view or viewmodel?
Its generally better to bind controls in the View to Properties of the ViewModel rather than other aspects of the View.
There is one clear benefit to the ViewModel having SelectedItem in this case, ...
4
votes
Accepted
Do shared ViewModels contradict MVVM?
It's really hard to say where do you draw the line between single and shared ViewModel usages.
As a rule of thumb, I would use shared ViewModel in the case and only in the case when two or more ...
4
votes
WPF UserControl Reuse With MVVM
The third solution you are looking for might be DependencyProperty
https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-properties-overview
You can use these to do all sorts of ...
4
votes
What's the better way to do MVVM with a number of very similar view models?
First of all, the purpose of Model View in the MVVM pattern is to hold and maintain the state of the view. It shouldn't know about the view, and it shouldn't have a view code.
Secondly, regardless of ...
4
votes
Accepted
UML Diagram double arrow
In UML associations A — B may be represented with an arrow A —> B to express navigability, i.e. the promise that there is an efficient way at runtime to get the related B’s for any A. But in UML, ...
4
votes
WPF MVVM - Pass data from child-view to parent
My prefered approach is to have a single top level view model which you bind to everything.
ie, and i'm just going to use pseudocode, sorry WPF is too long winded
<MainWindow>
<Title>@...
4
votes
Holding state in a service class in MVVM
A third option would be to stop needing state to be externalized. In the OOP paradigm you move behavior to where the data lives and encapsulate the data. Then rather then ask for state you tell ...
4
votes
Refactoring a legacy codebase with a god Repository and incomplete Clean Architecture
How would you approach refactoring a God Repository that’s tightly coupled across many parts of a legacy codebase?
One approach that allows you to transition away from this in incremental steps is to ...
4
votes
Approaches to Razor without combining C# in HTML
All of Web Forms, MVC, Razor Pages and Blazor allow you to define the code related to a view in a separate .cs code file.
Razor Pages in ASP.NET Core
Blazor
Be aware that in Blazor there are still ...
3
votes
MVVM. Is it a code smell when view model has properties with names show/hide/display that semantically belong to view?
Maybe.
If you were doing MVC I would say no. the purpose of the view model is to provide a model of the view which you can manipulate, query and test in code. It doesn't contain any business logic. ...
3
votes
Accepted
Avoid type checking but preserve separation of concerns
What you have here between your models and viewmodels is a mapping.
No amount of code sorcery1 is going to change the fact that you're going to have to map pairs of (model+viewmodel) in order to ...
3
votes
State of selected items in view or viewmodel?
The viewmodel doesn't do anything with the selected item. It's only for determining which content is shown in the ContentControl.
Then you don't need a SelectedItem property in the ViewModel. Simple ...
3
votes
Accepted
MVVM/WPF/Appropriate place for code
Personally, I like to keep each ViewModel as empty as possible - only including methods / properties that are directly responsible for providing data to the View, as the shell of an ICommand triggered ...
3
votes
Why can’t ViewModels communicate with each other?
Views and ViewModels are meant to be reuseable components that you can combine together in different ways.
If you hardcode a command on VM1 to always update VM2 then you break that reusuablity.
But ...
3
votes
How to avoid duplication of types in MVVM
DRY principle prescribes to avoid duplication of different pieces of knowledge, not just repeatable blocks of code. Two identical blocks of code can correspond to very different rules and that's ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
mvvm × 282c# × 105
wpf × 103
design-patterns × 52
architecture × 41
mvc × 24
design × 21
.net × 16
javascript × 9
android × 9
entity-framework × 9
dependency-injection × 8
silverlight × 8
xaml × 8
model × 7
mvp × 7
swift-language × 7
architectural-patterns × 6
ios × 6
layers × 6
view × 6
object-oriented-design × 5
asp.net-mvc × 5
separation-of-concerns × 5
java × 4