commercetools-sunrise-java icon indicating copy to clipboard operation
commercetools-sunrise-java copied to clipboard

Use of ViewModels in session is not transparent

Open selo-ecube opened this issue 7 years ago • 3 comments

  • When ViewModels are serialized to the session and then deserialized, the data from ViewModel.extendedViewModel is lost.
  • Since ViewModel.extendedViewModel works fine in Handlebars, losing this information during session handling was unexpected to me.
  • We stumbled upon this problem when we wanted to add a boolean field to MiniCartBean. Not using the extended view model but introducing a subclass of MiniCartBean is undoable because
    • #681
    • CartInSession having all private fields so a lot of information needs to be duplicated in the subclass
    • MiniCartBeanFactory.create(Cart) not delegating to a separately overridable method that creates a Bean instance and at the same time being invoked via super.create(...) from TruncatedMiniCartBeanFactory so our subclass of TruncatedMiniCartBeanFactory cannot override MiniCartBeanFactory.create(Cart) in an effective way. Of course we could start copying more of TruncatedMiniCartBeanFactory but code copying has limits, right?
  • We would like to show a message in the minicart explicating that it has been truncated. We're not able to do that right now due to above limitations.

selo-ecube avatar Oct 19 '17 09:10 selo-ecube

@selo-ecube I just re-read the last line and realized that you may have needed some action to fix this for your project. Sorry! I understood the issues as just feedback to fix in next releases (which we are working on at the moment). Do you still need help with that? Regarding the topic itself, we are getting rid of ViewModels, specially in Session, so we are working in the right direction.

lauraluiz avatar Dec 28 '17 10:12 lauraluiz

Sorry for the late reply. Yes, we need help with that, but it's ok to wait until you fixed it. Thanks for getting back!

selo-ecube avatar Jan 17 '18 15:01 selo-ecube

Unfortunately we are working on that in the 1.0.0-RC1 version which is completely different from the v0.x.x, so I don't think we could painlessly provide a fix for the latter version. But these are the key changes involved:

  • We don't use ViewModels, we use the JVM SDK models directly in the templates (except for very few exceptions).
  • We only keep in cache JVM SDK models such as cart and customer. In Session we keep ID and version of those models, and any other quick and non-sensible data that we may often need.
  • Then we just request the cached cart and put it in the page data to the template. The template takes care of extracting all info for the mini cart.

In your case you told me in another issue that you didn't want to go the cache solution, but it's the only solution that will solve this I'm afraid. So for your version I can either point you to the v1.0.0-RC1 development branch files so that you can copy them to your project and drop the Session classes from v0.x.x, or we can discuss which would be the best way for me to integrate them in v0.x.x so that it can help your case.

lauraluiz avatar Jan 18 '18 08:01 lauraluiz