How should changes to MVC objects be propagated?

2019-08-02 02:21发布

问题:

This is a follow-up to a previous question: Should sub-objects be fetched in the Model or the Model Mapper?

Let's say a User can have one or more PhoneNumber objects. According to the answer in the above question, these sub-objects will be fetched upon instantiation of the User. If I were to delete a PhoneNumber from the User's phoneNumbers property (an array of PhoneNumbers), or modify one of the PhoneNumber objects, where should this change be propagated?

Should I manually delete/update the sub-objects in the database, or should the User do that automatically on save? Thank you,

回答1:

Let's see If I got it the right way.

The user is logged and it's on his profile page. On this page the user clicks a link "delete this phone number".

On the page where the action is performed it will executed DELETE FROM phones ETC.

Now after this is performed when you will load the User, the constructor will load the phonenumber etc and considering you performed the DELETE sql early the current user object will not have, of course, the just deleted phonenumber.

This is at least what happens in my MVC framework.