Is using getters in XAML view-models a bad thing?

I’ve recently had an argument with a colleague about using getters (without setters) in a view-model classes used by XAML.

Example:

public string FullName {    get    {       return $  "{FirstName} {LastName}";    } } //call OnPropertyChanged("FullName") when setting FirstName or LastName 

His argument was that you should only ever create get/set pairs which use a private field (with the setter raising the PropertyChanged event if the value has indeed been changed).

Example:

private string _fullName; public string FullName {    get    {       return _fullName;    }    set    {       if (value != _fullName)       {          _fullName = value;          OnPropertyChanged("FullName");       }    } } //Set FullName when setting FirstName or LastName 

I, on the other hand, think it’s fine to use getters (which calculate the output on the fly), provided they aren’t updated all the time via too many PropertyChanged calls.

I don’t think my colleagues approach is bad – I just think it’s way more busy work which might simply be not needed. However he was sure my approach is bad and should be avoided at all cost.

As a counter example I pointed out that the MVVM base class from DevExpress has RaisePropertyChanged and RaisePropertiesChanged (for refreshing multiple properties) methods ready for use – both of which wouldn’t be needed if all the properties were get/set pairs (the DevExpress base class also exposes a SetProperty method for use in setters, which also includes a PropertyChanged call). His argument was that “well, people are stubborn, keep doing it wrong, so DevExpress simply added those methods to make sure people are happy” which I found… odd, to say the least.

So what’s the take here? Is it bad design to use getters in view-models designed to be used by XAML?

EDIT: I forgot to mention… my colleague also made the claim that using get/set pairs will always be faster. That does seem to be true as far as updating the UI is concerned, but he also used this as an argument that getters shouldn’t ever be used. The whole argument started when I had a bit of performance issues when populating a data grid with more (i.e. 12k) rows where my VM used quite a few getters…

Propagating Data to multiple ViewModels – C#

I’m a bit new to MVVM, but here’s my dilemma:

  1. I have a model (or models, but let’s keep it simple)
  2. I want to show that model data on multiple different views.
  3. Ok, so ViewModel for each view gets created
  4. UserControls get created (my views)
  5. each Usercontrol gets a ViewModel

Here’s my question: What/where/how do I handle propagating this model to each viewmodel?

This is desktop dev, not web dev, so I’m getting stuck on the idea of making sure that the model is in each viewmodel. Normally I handle this by having a controller/parent class propagate down the information to each ViewModel, but I’m starting to get into a more complex application where it’s not one model that I need to propagate, but multiple models and viewmodels that might need to call services that would need that info as well, and I’m getting a bit confused as to how exactly the best practice is to proceed.

Just to be thorough, here’s a quick example:

View1, View2, View3, and View4 all exist.

View1 and View2 need models M1, M2, and M3 View3 needs models M1, M3, and M4 View4 needs models M2, M3, M4, and M5

Each view now has it’s own corresponding ViewModel, VM1-4.

How do I create and propagate M1-5 to the corresponding VMs?

Kotlin: Lifecycle-aware recurrent periodic task execution with LiveData and ViewModels

My app already uses some recent Android patterns for network calls:

  • LiveData class
  • MVVM architecture with ViewModel class
  • Kotlin coroutines for Repository classes
  • Retrofit interface etc.

Now I want to implement common feature which is automatic fetching current data from API every few minutes.

I read about WorkManager and give it a shot – I implemented it but I then saw that WorkManager (JobScheduler) keeps running after closing app which is not what I want. I also felt like WorkManager API is too much overkill for that simple task.

Then I read a guide on Codepath that suggests Handler class for main thread repetitive jobs and ScheduledThreadPoolExecutor for background repetitive tasks. I know that they will probably work fine but I’m not sure if they are best solution.

My question is: what’s currently the best way for recurrent API calls that:

  • works with LiveData and ViewModel classes (observable result like normal API call)
  • is Kotlin-friendly (any way to make coroutine recurrent?)
  • is “lifecycle-aware”, meaning that it will stop when app goes to the background?

Setters in ViewModels

So I have started reading some books and articles on architecture. They all state (put a little simply) that you should never let your view access the model directly, when using a Model-View-ViewModel (MVVM) pattern. Instead there should be a view model for each model.

One of the ideas is, as I understand it, that formatting can be done in the view model, which will then mainly contain strings. An example could be a person model which looks like this:

class PersonModel {     public string FullName { get; set; }     public DateTime DateOfBirth { get; set; }     public decimal YearlyIncome { get; set; } } 

Now a read only view model, could then look something like this:

class PersonViewModel {     private PersonModel _person;      public PersonViewModel(PersonModel person)     {         _person = person;     }      public string FirstName      {         get { return _person.FirstName; }     }      public string DateOfBirth      {         get { return _person.DateOfBirth.ToString("dd.MM.yyyy"); }     }      public string YearlyIncome     {         get { return _person.YearlyIncome.ToString("N2"); }     } } 

Now this approach is quite nice, since you don’t need to consider string formats in something like XAML (if you are using WPF) or whatever GUI stuff you are working with.

But here is my question; what do I do with the setters using this approach? They obviously need the same input type (string) as the getter, at least when using C# with WPF. I could of course make a custom setter, like this:

public string DateOfBirth {     get { return _person.DateOfBirth.ToString("dd.MM.yyyy"); }     set { _person.DateOfBirth = DateTime.Parse(value); } } 

Is this a viable approach or am I way off, in my understanding of how to use view models?

MVVMC iOS App with multiple ViewModels & how they can talk each other

He i am developing an iOS App and its base architecture is MVVMC design pattern. Using coordinators for navigation logic.

I have situation, where I have a complex UI with different components. So obviously I have to modularise my design and create multiple ViewModels for different UI sub sections or Views. All these are attached a parent view.

My question is where should I keep those ViewModel references?

I have the following options. Please help me to choose the right approach among these.

1). Have a parent view lets this parent view hold this references to those child ViewModels and pass the updates among them.

2). Have a parent ViewModel. Lets have these child ViewModels attached to this parent ViewModel and Manage the child ViewModel updates with in the ParentViewModel and pass to the view.

3). Lets the coordinator have these references to the child ViewModels and manage the updates among them with in the coordinator and pass to the view.

Or is there any other standard practice other than this? Please help me to have a clear path here.

Any suggestions with some examples will be more helpful.

Why can’t ViewModels communicate with each other?

Can someone explain concretely why it is not desirable for ViewModels to communicate directly with other view models? It seems to me that for virtually any UX application there will be a “root” view (with its ViewModel) and any further views will be somehow initiated from from this view/ViewModel, or a sub view/viewModel.

The recommendation seems to be that any communication between ViewModels takes place via a message bus or event aggregator but I can’t help but think this is overkill. For example, say we have an application that lets users define different profiles and do other actions based on the current profile. In this example the main window is dependent on something handling the profile management. Obviously i don’t want to hard core the ProfileManagerViewModel as a dependent of the MainViewModel but it seems to me that simply creating an interface (e.g. IProfileManager) is isomorphic with sending a message over a bus or aggregator. And if these are isomorphic then the interface is much easier to implement and maintain.

Even if it’s not completely isomorphic, it strikes me as a YNGNI situation: in what scenarios will I gain something extra from using an event aggregator over an interface and how likely is my application to ever encounter these scenarios during its expected lifetime? I would expect the common case to be that if my interface must change somehow (whether my interface be a C# interface or an event that I use to signal) both ViewModels will require modification anyway.

So what am I missing here? The only case I can think of is if additional viewModels were interested in e.g. the ProfileManager from my example being started but I don’t expect this to be a common situation in most applications. Of course if one sees the use of event aggregators as not adding any complexity then this whole question probably doesn’t make much sense but I think most people would see a standard interface solution radically easier to follow and reason about.