How Prism use in Xamarin

28 Dec

Introduction of Prism

Prism is a framework that facilitate the additional features for developers.It makes Xaml application loosely coupled,Reliable, optimized and maintainable for multiple platforms like Xamarin.Forms, WPF, Windows10 and UWP(Universal windows plate form).

Basically prism is used for implementing the design patterns when we develop any mobile application.Design principle makes our code proper and well formatted architecture and easy to understand and maintainable.Prism mechanism has its own libraries and templates.

Prism libraries Basically help us for implementing design pattern inside the projects like MVVM, dependency injection, commands, Event Aggregator etc.Prism used for code sharing by PCL (Portable class library) and shared across the all 3 platform (Android, IOs And UWP).
Prism designed according to core architectural design principle that is mainly focused on code separation and loose coupling.It implements SOLID Principle architecture during the development. Prism allow to developer can create and develop modules individually, that is easy to testable and maintainable for different target platform.

The whole process is done in the MVVM(Model View-View Model).Prism have is own template and structure and it integrate with different IDE like visual Studio and Xamarin Studio.that is used to create a highly effective and productive work-flow and architecture for various platform like WPF, UWP, and native iOS and Android applications using Xamarin.Forms.

There are multiple versions are released by Microsoft.Currently,The last updated version is 7.2.0.708 pre. All latest versions are fully open source.prism helps us to build a more flexible,testable and maintainable design pattern.

Design pattern (MVVM)

MVVM (Model View- View Model) is a design pattern architecture.which increase the performance of the application.It basically used for separation between the user interface(View) and business logic(Model). this pattern is basically used for two way binding between the Model View and View Model.It increase the performance of the application and reduce the time of data binding.It makes application faster.

MVVM has mainly the following layers :

  • Model : Model is mainly represents the business logic and hold the information as well.Model read and update the data through a view model and bind inside the user interface.
  • View : View represent the UI(User Interface),structure, layout that visualized the user on screen.observe and get the data from model through a View-model update the UI elements accordingly.
  • ViewModel : View Model interacts with model and also prepares observable(s) that can be observed by a View.View model work as a mediator between View and Model . A view directly binds to properties on the view model to send and receive updates using Property Changed Events.

The below diagram shows MVVM components and basic interactions 

Why Prism :

When we create a new Xamarin Forms project. As all app developer would like to follow best architecture and design pattern to improve the performance and code quality of application , these are some of the reasons :

  • Prism helps us to make application more maintainable and loosely coupled that makes applications easier to test.It works on single responsibility principle.that is a part of SOLID principle.in this principle any high level module not directly depends on the low level module.
  • Prism makes code reusable and flexible for new changes.any new developer can easily understand the existing functionality and update.that feature enhance the performance and quality of code.
  • Prism have its own templates and library so we can say it is very complete them self and simple. It has its own structure that contains all navigation's, events, modules.
  • Prism followed MVVM architecture that makes application faster.
  • Prism support different platform like WPF,Windows,UWP, Xamarin.Forms etc.

Benefits of Prism :

  1. Code Re-Usability : Prism allowing to reuse the common services and components it decrease the line of code,clean and improve the quality of code.It’s make application well structured and reduce the size of application.Application built more lighter and flexible.
  2. Suppleness & Maintainability : Prism helps to create applications that are easy to extend by managing component dependencies,Prism allow to component easy enhance functionality integrate without effected other module.All module work independently not dependent on each other.It is easy to maintain.
  3. Flexibility & Reliability : Prism helps to create flexible applications by allowing them to be more easily updated as new capabilities are developed and integrated.Prism help to building a loosely coupled application.high level module not dependent to the low level module.User can easily update the logic.
  4. Modularity & Team Development : Prism promotes Modularity that allow developer and team can developed and deploy its own module individually.its concern of separation and discrete functionality implementation.All several multiple small modules are written Separately. that makes project more flexible when we are working with team.
  5. Effective Performance : Prism design pattern is used to increase the quality of applications because it allowing the user can reuse the common services and components with fully tested Modules.this design pattern improve the performance of application and increase the quality of code.

Implementation with Xamarin Form:

Step 1 :
For using Prism mechanism we have to install Prism Template Pack. We can install it from Visual Studio Gallery. Below are the steps that we have to follow
Visual Studio -> Tools -> Extensions and Updates

After successfully installation of Prism Template Pack, create a new solution.follow below steps.
File > New > Project... select Installed > Visual C# > Prism.
Here you can see all the available templates for a new Prism project...

Step 2 :
Select Prism Blank App (Xamarin.Forms) and fill in the name of your project / solution and click OK

Step 3: In Below step you can see that we have a different target platform like android,iOS,UWP with dependency injection.Select according to your requirement with container which you like to use and we are using the DryIoc dependency injection container. 

Step 4: Register type method is used to register each service that you will used for navigation in future.It is work like a container and this method is used for the register the all navigation service inside the container.it register using IContainerRegistry.RegisterForNavigation method in the RegisterTypes method.This method is defined inside the App.cs page.Because App.cs is a first executable page of the application 

Conclusion :

Prism architecture provide the facility to developer for create application with some additional features like increase Code Re-usability, Maintainability, all functionality loosely coupled and code much readable and flexible. It makes application faster.

Share this post with your friends!