ReactiveCocoa / ReactiveViewModel

Model-View-ViewModel, using ReactiveCocoa
MIT License
1.96k stars 259 forks source link

The relationship of the view-model and data-model in MVVM, especially with regard to collections #14

Closed bobspryn closed 10 years ago

bobspryn commented 10 years ago

I know this isn't necessarily a RAC question, so please close it if you don't like these types of questions here.

I was having a conversation with @AshFurrow exploring the relationship of the view model and the (data) model. Namely I was wondering if in a situation where you were on an 'edit' screen, whether the view model was always a proxy between the view and the model.

He does proxy/shadow his non-collection properties with RACChannel which makes sense for validation, etc. The more complicated situation in my mind is any collection properties on the model. Is it worth the headache of maintaining an entirely separate collection on the view model? If not, do you expose the model itself? Some of it's properties? Ash solves for this by using methods that internally access the set on the model. I'm thinking that exposing the set as an immutable read-only property on the view-model might make sense.

Trying to read about this from the .Net world. Some people seem to say you can expose the model or some of it's properties where doing otherwise would just be overhead and no transformation is necessary when accessing.

Would love to hear thoughts on this.

jspahrsummers commented 10 years ago

I generally like to keep model objects immutable, and put any mutable references—e.g., a changing collection—into the view model where possible. It simplifies this use case, and makes each model more like a dumb value object (a la NSString or NSDate).

When you do want/need a mutable collection in the model, ReactiveCocoa/ReactiveCocoa#1032 should make it easier to manage changes between it and the VM. Otherwise, there's no really good answer for that right now.

bobspryn commented 10 years ago

Thanks. Looking forward to 3.0!