martes, 1 de octubre de 2019

MVVM with SQLite: solution model

When implementing some design pattern or architecture that requires some concentration and learning from me, I like to document the process to have it as a reference in the future, and also to share it with others who want to travel the same path.

In this case, it's about how I applied the Model-View-ViewModel pattern along with SQLite persistence data, in my mobile application developed with Xamarin Forms.

I will just list the high-level steps of the process, which is illustrated in the class diagram below. It should be clarified that the diagram only reflects those attributes and behaviors of the class model that I consider relevant for the explanation, because, clearly, the complete model is much more extensive.

On the one hand I added an ExtendedBindableObject class, which derives from BindableObject, and adds the automatic notification of the PropertyChanged event, when a ViewModel undergoes some change in its exposed properties.

From the ExtendedBindableObject class, derive BaseViewModel, which will serve as the basis for all the ViewModels of each object to represent.

The ViewModels scheme in my case, speaking for now for simple objects, consists of a ViewModel with a collection of objects, another to add new objects, and another one to show the details of a given object, which also allows you to delete or modify its attributes.

For each of the ViewModels detailed above, a View corresponds, implemented within the Xamarin Forms model as a ContentPage.

When the View is created, the corresponding ViewModel constructor is called, and a new instance of the CrudService service (which implements the ICrudService interface) is passed, which the ViewModel contains. In this way, the dependency injection pattern is used (a particular case of the control inversion pattern) indicating to the ViewModel what service to use to access the data layer.

The CrudService service, in turn, is responsible for making calls to the data layer, represented by the AppDatabase class.

This is roughly the complete picture needed to implement MVVM together with SQLite. I had to spend several hours reading, and then testing, until it was working, but it leaves me happy to be aligned with the recommended practices.

If the promise of scalability and cleaner growth of the application, which this pattern proposes, is fulfilled, time will tell.

No hay comentarios:

Publicar un comentario