开发者

MVVM - creating new objects in the viewmodel

开发者 https://www.devze.com 2023-02-07 14:15 出处:网络
Pretend I have a viewmodel in my MVVM application. The ViewModel talks to a service layer, the service layer has a method say SavePerson(person p){..}. doing the Person p开发者_开发技巧 = new Person()

Pretend I have a viewmodel in my MVVM application. The ViewModel talks to a service layer, the service layer has a method say SavePerson(person p){..}. doing the Person p开发者_开发技巧 = new Person(); in the viewmodel doesn't seem right to me. any suggestion on how to do this in another way..a pattern maybe?


Im using relay commands and atm Im creating the new objects inside the command then passing it to PersonService.SavePerson(person) also inside the command. is that bad pratice?


You should indeed inject the service that is responsible for creating/saving the models (in your case Person) into the view model (interface based of course), then use that one.

If you use a service, you have a more loosely coupled design, but it's just a question on how far you want to go with creating a loosely coupled design.

0

精彩评论

暂无评论...
验证码 换一张
取 消