Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible wrong use of MVVM pattern #15

Closed
stefanomondino opened this issue Aug 7, 2017 · 4 comments
Closed

Possible wrong use of MVVM pattern #15

stefanomondino opened this issue Aug 7, 2017 · 4 comments

Comments

@stefanomondino
Copy link
Contributor

Hi guys,

first of all, let me thank you for your work, which is really great and inspiring!
I've been using MVVM in iOS since 4 years now and I've learned that everyone has its personal way to implement things with this pattern and, most of all, everyone intend a "View Model" in different way.
However, there seems to be 2 things that almost everyone agrees upon:

  1. Views should NEVER know the existence of the "model layer"
  2. In terms of separations between layers, UIViews and UIViewControllers falls inside the "View" layer

Taking a look at your example, in the PostsViewController, you're fetching the UITableView with the .rx extension: inside the closure, you're directly referencing the UITableViewCells IBOutlets and directly populating with model properties, which (in my opinion) totally breaks the MVVM pattern for different reasons :

  • The UITableViewCell (a view) is fully aware (and directly accessing) something from the Model layer.
  • The cell is configured by a single view controller, meaning that the same cell must be configured in every scenario it's going to be used (suppose you have another screen where you would like to present
  • There's no "cell view Model" concept, which could be useful to reuse the same cell (with same outlets) to display data from different models.

Does it make sense to you? Am I being too "strict to the pattern" for your scenario?
Thanks in advance :)

@sergdort
Copy link
Owner

sergdort commented Aug 8, 2017

Hi, @stefanomondino

Thanks for rising this issue :)

As a reference to MVVM I like to use msdn post.

I would agree here that we are doing data formatting on a binding layer is wrong, effectively leaking the Post entity to UI. The reason I did this is that there is no formatting involved in current example, e.g we set titleLabel.text = post.title.

But I think you are right and we should be precise! As some people out there could be confused (a specially people new to iOS development), thinking this the right way to do it.

I'll try to find some time and address it. Or if you want to do it yourself I would be more then happy to see your approach.

@stefanomondino
Copy link
Contributor Author

I've forked your repo, I'll try to work something out. I'll let you know!

@stefanomondino
Copy link
Contributor Author

@sergdort here it is. It's really a simple approach, in a real project i usually create a little bit more of structure inside the view model layer.
You can take a look (if interested) at Boomerang, it's something that solves this kind of problem by separating view model types ("List view models, Item view models, Selectable view models, etc.). We're writing docs in these days, then we'll release v1.

@sergdort
Copy link
Owner

sergdort commented Oct 8, 2017

I guess we can close this one :)

Feel free to reopen if you think there are thing that can be improved

@sergdort sergdort closed this as completed Oct 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants