Message Boards

Using MVVM in liferay

Tiago Machado, modified 5 Years ago.

Using MVVM in liferay

Junior Member Posts: 59 Join Date: 2/21/19 Recent Posts
Hello again friends xD. I know I'm being boring but...I need some advice again xD

I searched about Liferay MVVM but I didnt find much info about it.
Well, I'm new to MVVM, I read about it and seems a pretty nice concept, I have made the full presentation layer in the backend course but Eddie didn't talk about MVVM, if I'm wrong please correct me, maybe i didn't catch all the details.

so, by what I read I will have a package called for example com.training.liferay.movies.view.model in my movies-WEB right? and there I will have the ModelViewClasses(view and author), and that classes will receive the data from for example AddMovieActionCommand, or EditMovieActionCommand classes(these one receive the data from the JSP), and after fetching the data from the ActionCommand classes they will comunicate to the Model to persist the data. 
Did I catch it right? is this the way to do it in liferay? Seems interesting but at the same time looks like its doing the same im doing at the CommandClasses, or did I miss something? or the idea is the include the serviceImp here and in the service Impl just persist the data fetched from the ViewModel?

Thanks a lot for your time!
thumbnail
Olaf Kock, modified 5 Years ago.

RE: Using MVVM in liferay

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
I'm not sure I fully understand. But let's start one-by-one:

You should never have a dependency of your -web module to your -service (implementation) module - only to its -api. As soon as you add a dependency to your implementation, you've made a mistake in terms of encapsulation. I've seen common additional modules, but not dependencies on other modules.

As of the user interface: The Backend Developer course teaches the way that Liferay portlets are built, and what happens when you keep close to the portlet standard. Other UI-environments and their relationship to the portlet specification are a different beast. You're free to use any of them, but Liferay won't quickly adopt them: It'd be a constant struggle to keep up with the development of those frameworks and the favorite UI layer of the week for every new version of Liferay.

On the view layer, we use the regular model classes, to display their values on the JSPs. While they're not the most modern way to build UIs, it's by far the most stable one. And the model layer is partially prepared to be used in a web environment (e.g. through "escaped models", that can be used in HTML without the fear of XSS). Thus, there are no separate ViewModels in Liferay's standard way of developing. Does that mean that you can't have them? No. But it means that they're not likely covered in the standard way that Liferay develops the UIs.

I hope that helps. If it doesn't, I'd need more context to understand your question
Tiago Machado, modified 5 Years ago.

RE: Using MVVM in liferay

Junior Member Posts: 59 Join Date: 2/21/19 Recent Posts
Thanks for the answer Olaf! I dont have any dependecy of my web module in my  service, i'm sorry i wasn't fully clarifying.
Okay I understand that Liferay dont use by standard the viewmodels.

What I need to do is to implement the MVVM pattern, I have the -API, -SERVICE and the -WEB modules.

The idea I got to implement the MVVM pattern was, in the -WEB module to create a package for the viewmodels.
I'm new into this, I knew about MVC and MVP, but I dont know the MVVM, so I'm reading info about it and well, asking here in the forum xD.

In Liferay may be a little diferent to implement thats why im asking and well since I really dont know MVVM I would apreciate some of your knowledge.
What steps should I make to implement it? 

I have a fully working app, I just want to implement the MVVM, should I create the package for that in the WEB module? and if yes, what should I do there? Im reading about it, but it seems a little complicated. maybe you can show me the light.

​​​​​​​Thank you very much and sorry for the confusion.
Tiago Machado, modified 5 Years ago.

RE: Using MVVM in liferay

Junior Member Posts: 59 Join Date: 2/21/19 Recent Posts
I'm getting the hand of it right now emoticon

just a little question please, now that I'm using MVVM pattern, the viewmodel can only contain view logic, and well, basically it's like a bridge to pass data from service and view. I'm thinking in implement the Mediator pattern too, since is not good practice the communication between the viewmodels, and it should contain only view logic, for what I have seen, I could implement the Mediator, and the mediator will be the bridge to the viewmodels am I right?
So my true question is, since I'm setting  my Author to a Movie (movie.setAuthor(author)) int he MovieLocalServiceImpl, and for that I had to go to the MovieImpl generated by the service builder to expose the author to the movie(i know its perfectly ok, and it was suggested by an amazing person from the community, but im trying to learn and understand more), now I could put that logic in the mediator, so I dont have to have code in the MovieImpl? or that code logic should be mantained in the MovieImpl/MovieLocalServiceImpl?
thumbnail
David H Nebinger, modified 5 Years ago.

RE: Using MVVM in liferay

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
Perfection in implementation of all appropriate design patterns leads to a perfect textbook application, but a lousy implementation.

Let's do a thought experiment to show how. You have a Movie entity, maintained by SB code, which (can) come with caching, indexing, permission controls, workflow, asset management, recycle bin support, ...

In order to implement MVVM, you now have a MovieView class w/ the simple getters/setters that comprise the view. Every retrieve gets marshaled (copied) into your view class, your view is populated by the incoming web form and you marshal (copy) it back to the entity for persistence.

Note here the two (unnecessary) copies going on. The copying of data is not free, it comes at a cost. It costs memory and cpu to use these things, plus you haven't eliminated costs of development because now you may have two classes to maintain, the view model and the SB entity.

When you add the Mediator pattern in to decouple obvious relationships between the view models, it is yet additional overhead introduced (unnecessarily) into a system.

The problem here is that you're adding overhead with only a percieved benefit because you're dutifully applying this new design pattern that you know or learned about. The extra memory and cpu required to maintain this drains away resources that could be used to service incoming requests, negatively impacting your overall capacity. The extra layers of marshaling also will increase your response times (because you're doing extra work in the service of each request) that cannot be tuned away by normal performance tuning activities.

The thing about the design patterns, they were meant to be blueprints on how to solve problems that every developer faced. But they were never meant to be followed blindly. There was always an understanding that the patterns have costs associated with them, some more than others, but at the end of the day you still need to consider the use of each pattern an weight the impact and make educated decisions on what to do when.

Introducing MVVM and Mediator into your basic portlet will make it seem like a nice implementation, but at the end of the day if your boss brings me in to fix performance issues my first recommendation will be to dump all of this rediculous overuse of unnecessary design patterns.  Why? Because the bosses care about performance, capacity and runtimes, they don't care so much about code elegance.

Note that this is not a rail against design patterns - I use them and live by them every day. But when I use a pattern, I'm always keeping in mind the potential runtime impacts. It's important to point out that each pattern includes the section, "what the pattern is designed to solve". The key here is that you have a problem that you need solving. Implementing MVVM out of the gate is just an attempt to solve a problem that you might not even have, same with Mediator. You need to know what your problems are before you can solve them.

There's an old saying, "Everything is good in moderation"; same is true for use of design patterns. 
Tiago Machado, modified 5 Years ago.

RE: Using MVVM in liferay

Junior Member Posts: 59 Join Date: 2/21/19 Recent Posts
Very helpful as always David!

Thanks to you I understand much better what to do and what not to do!

Thank you very much for your explanation! totally helped me a lot in the direction im going!