What is PRISM?
PRISM is a framework for Xamarin.forms which helps developers write testable, maintainable and clean MVVM code.
It is easy to install and use, The best way is to install a PRISM template pack. Which has all the boilerplate code or the scaffolding any developer needs to get started.
What Does it Provide?
In a perfect world a good software application means, writing a code with decoupled modules that can be easily tested by writing unit tests, so that it is easily maintained.
Unfortunately, Xamarin.Forms in itself doesn’t does not encourage these behaviours there are some shortcomings here and there.
PRISM on the other hand helps us to write this kind of code, It has built in abstraction of Navigation services which is not possible in Generic Xamarin.Forms apps
How to Install ?
In an existing visual studio window click on extensions in the upper tool bar,
Browse and search for Prism template package ,
Download it and shut down visual studio a dialog window will appear prompting to modify visual studio, follow the instructions and Prism will be installed.
Whenever you need to create a new Xamarin.forms project u can search for Prism Xamarin forms blank app and that will create a new Prism project.
It will look exactly like a Xamarin forms app, with some additional Prism template code, which can be understood pretty easily once you take a look at the official documentation for Prism Library.
Select the Prism Blank App
Containers can be selected in the dropdown.
Create the Project.
How is it different from conventional MVVM?
I will use some code snippets as examples for this.
Firstly, the biggest problem that PRISM solves is the Navigation problem, in a conventional Xamarin.forms app it is really difficult to perform navigation via VIewModel.
What we did in that case was write something like this:
Now, it’s okay if we do this when we have to perform navigation once or twice, but this cannot be done in an application where we need to do a lot of navigation.
And, anyone who has worked with MVVM knows that we cannot write
It doesn’t work, to solve this we create a Interface which we will use as a service for navigation and call the navigation methods.
In PRISM we don’t need to do all that, there is some code present in our App.xaml.cs that allows us to perform navigation in viewmodel
Now, we can easily perform navigation
We can discuss a lot about navigation in detail, but that would make this document too long.
For more on navigation check out this link.
other than this Prism can auto-wire views to views to view models but, it uses reflection for that, which may cause slowdowns.
It appends viewmodel to the view name and tries to find the viewmodel.
Viewname + ViewModel = ViewNameViewModel
Containers in Prism.
Xamarin.forms uses containers for Dependency Injection.
Dependency Injection is kind of it’s own topic, so we are not focussing too much.
Just to be aware we basically have the option to use one of the two most popular containers
The above mentioned installation process uses DryIoc but it can be changed in the dropdown.