Sunday, September 15, 2019

MVVM and mobil app development.

A big part of the MVVM Pattern is separation and binding. I don't know how you are developing your mobile apps, but in my apps, many parts, I mean more than 80%, are created "on the fly" at runtime. Sometimes the elements would be created from the content of a database, sometimes from a REST(full) Webservice and sometimes from a config or user settings.

So here comes the funny part: Imagine controls have to be created from a database. From a ButtonClick on the View, the ViewModel calls the Model. The Model loads all the necessary information and hast to provide this information to the ViewModel. So far so good.

If you take a closer look, every component -  like a TEdit - is some kind of View. Views are created by the composition-root, perhaps from an event of the navigation-service, perhaps from a ViewLocator or in case of e.g. Messagedialogs, from a dialog-service. 

In this case, the ViewModel is created with a component-service to create a component from a predefined collection. Wouldn't it be great, If we already had a service like this? Yes we have. My FluentCreator could be called by this service and is able to provide an easy way to create predefined  - even complex - view-structures like I've demonstrated in my scale-able calendar component. 

But where is the target? The target is a container (TLayout/ListBox) on the View, but we don't have a View-reference in our ViewModel. Conclusion the View must call the component-creation-service. Not a big deal, we could create the View with this service. But how to pass all the information from the Model to the ViewModel to the View to the component-service and keep in mind, the Viewmodul has to be testable and if possible the service, too.

One problem, the possibilities of component creation are endless. We could fire a PropertyChange with some kind of string. E.g. "Create Edit_ID parent Listbox1" we could find the Listbox1 and we could create a TEdit_ID from our lookup list in our component-service. But our ViewModel should not know "Listbox1".

The next problem is the binding. How could we create the binding to our ViewModel? There aren't any properties to bind to. We also have to create a dynamic receiver for the created components and I'm sure we have to provide some kind of database connection to the receiver.

It looks like our component-service as three parts: One for the View, one for the ViewModel and one for the Model... 

To implement an easy to use MVVM framework is not trivial, but doing everything at runtime is a step forward. Of course, there will be a way, but finding the "best" way is the tricky part.

Think about it, I would love to read your ideas on this topic.

PS.: The dynamic creation stuff is probably not mobile related, but from my point of view Desktop apps are more static these days...


No comments:

Post a Comment