I do not fully understand what should be in the Model , and what should be in the ViewModel .

For example, the task of parsing user messages. There is such a class:

 class Post { public string Author { get; set; } public string Text { get; set; } public int LikeCount { get; set; } } 

Do I need to put a Get-request that picks up a page with a message, and regular pages that will tear out the necessary information into the same class, or should all this be located in PostVM?

  • As I understand in Model what you have. In VM, getting a list of posts, parsing, etc. - MaximK

2 answers 2

See it.

User messages are what your program works with, a typical model. Analysis of them should be in the model. How exactly do you get this very message and how do you pull information from it is not interesting for VM.

VM should receive only ready-to-use information from the model, convert it in a convenient way for itself, and put it into the UI.


Your Post class looks like a DTO object. For VM, I would remove setters from accessibility: it’s not a matter of VM to change messages, it’s a matter of model.

  • Understood thanks! Another point is interesting: initially I get a list of links to posts (detailed information is not available if you do not open the post separately), and then for each of the links you get a post and the necessary information is parsed from it. I understand correctly that with this approach the logic of getting a list of links to posts should be implemented in a separate model? - trydex
  • one
    @maxwell: Yes, this is another model class. Exactly. - VladD
  • It looks like an anmic model, which is not the same as DTO - vitidev
  • @vitidev: I would not offer DTO either, the parser is a rather thick model. - VladD

Model:

 public class PostModel { public string Author {get; set;} .... } 

ViewModel:

 public class PostViewModel { // Коллекция постов public ICollection<Post> Posts {get;} public void GetPosts() { Posts = .... ... } public void CreatePost(Post post) { .... } public void UpdatePost(Post post) { .... } public void DeletePost(Post post) { .... } }