Should a model or ViewModel handle lazy loading of model properties?

Let's say I have a ConsumerModel . ConsumerModel has many model properties, and one has an AddressModel list. I want this Address property to be loaded lazy, because it is not used everywhere, which is in the ConsumerModel. It is used only in the AddressViewModel , which displays and allows you to change user addresses.

Who takes care of loading AddressModels, ConsumerModel or AddressViewModel ?

Or is there an alternative design that is proposed for this type of thing, for example, disconnecting the Addreses list from the Consumer and treating them as a separate property from the AddressViewModel ?

 public class ConsumerModel : IModel { private List<AddressModel> _addresses; public List<AddressModel> Addresses { get; set; } } 
+4
source share
2 answers

It belongs to the model. The point of using the ViewModel template is to remove these kinds of solutions from the view in the first place. The model is responsible for collecting, organizing and managing data. ViewModel is solely responsible for ensuring that what the model contains gets access to the user interface. Although it seems like a good idea to put this in the ViewModel if the specification changes next week, and suddenly you need this address in another ViewModel (for some reason), you will break the template and you will be scrambled to do it up.

+3
source

If the Addresses property belongs to the ConsumerModel, then the consumer model should be responsible for loading it. (Something else destroys encapsulation / some design principle - I have little regard for my terminology ... it's just wrong, damn it!)

However, if the Address list is used only in the AddressViewModel, there may be a case for transferring a property to this ViewModel and processing it with loading.

+1
source

Source: https://habr.com/ru/post/1339379/


All Articles