Businesses always dream of data centric applications which are easier to maintain, robust and flexible enough to accept the future changes. Silverlight being a part of .Net framework has all of these ingredients well thought of. Apart from such basic features, it provides rich user interface with minimum efforts. Though Silverlight is relatively new technology, it has a little learning curve for seasoned Microsoft developers. Silverlight also provides cross-browser and cross-platform support which is very important for targeting larger client base with diverse needs.
Three Important Benefits of MVVM
The goal of any system/application is to automate the business process and help people achieve utmost efficiency. Following are some key points which may compel us to use MVVM (Model-View-ViewModel) pattern:
- Eradicate tight coupling: Every system has N layer defined or undefined. When these layers are tightly coupled with one another i.e. every layer knows how the other layer is accomplishing its job, the system becomes unmanageable and least flexible. Change in any one layer requires changes in all the layers and thus creating chaos. MVVM decouples all the layers and helps us achieve robustness and flexibility which in turn makes the system absolutely manageable.
- Separation of responsibility: MVVM helps in a great way to separate the responsibilities in the development process. Multiple Software Engineers work on a system/application at a time during its development process. To make this process easier, separation of responsibility is very important. This can be very well achieved using MVVM for example, a designer and developer can work simultaneously and with least dependency on Silverlight application if we implement MVVM design pattern. As View on which designer has to work and ViewModel and Model are totally separate contexts on which Engineers work.
- Testability: Unit testing is very important part of development process especially during initial development and maintenance. MVVM creates loosely coupled system components which can be unit tested separately without any hassle.
How Silverlight Married with MVVM can Produce Great Results?
To get the best result out of a technology and a design pattern, it is very important that both of them are compatible with each other. If not then it would be more of an overhead on the system. Following of Silverlight features make it a best mach for MVVM design pattern:
- Databinding: Silverlight provides robust infrastructure for binding the data which helps in binding View with ViewModel in such a way that both of them are unaware of each other’s functioning.
- Datatemplates: Datatemplate is the actual template where ViewModel gets bound and this data template also defines how data will be shown to user. This helps in separating responsibilities of designer and developer.
- Commands: Commands are the actions that need to be performed. It is set of activity that gets performed. As we know ViewModel also has the state and behavior of the Model. When Command is bound to a ViewModel, it performs certain actions on user interaction. For example in a customer registration “Add customer” is an action for which we can create a command which executes on button click or context menu click.
Thus, Silverlight used with MVVM gives the business an edge over other technologies. Though Silverlight is still a young technology, Microsoft has been giving great support and has a very promising plan for its future versions. Considering these factors, it would be in the interest of any software solution provider who would like to cater its client base with Silverlight married with MVVM and we are not an exception
Tags: MVVM, Silverlight
Nice one Nishith!!
Very encouraging information is all that.but what exactly i need to know that how we provide data from database to that viewmodel.
Is there only option is WebService or WCF Service for connection to database. or directly we can connect to the database ?
Hi Nishith,
Wonderful article… Thanks for sharing your knowlege!!
@Brijesh,
Silverlight can access data using Web Services or WCF Services. One other interesting approach is to use RIA enabled WCF services to cater data needs of silverlight applications.
Thanks Sudev and Brijesh!!!
@Brijesh: You cannot connect to database directly as ADO.Net is not supported in Silverlight right now. So you have to use web service as a layer as mentioned by Sudev. So your options are Web service/WCF service/RIA services/Ado.net Services.
Hi Nishith,
Its been a long time for Silverlight in market and during that time a lot of templates are available to develop application in Silverlight
Like MVM, MVVM, PRISM, MTVT (recently read article on code project) etc.
How to define that which one is better for Application ?
Good question Alap!!
Framework is a tool to implement MVVM. Every framework is designed with a vision so it is very important to understand that and match that with our requirement before zeroing in to any one of the available frameworks. For example, MVVM light toolkit is very light framework and it is primarily built to create MVVM based application but Prism is very exhaustive framework which apart from MVVM support it gives lot of other features. So if you are not going to use those features or most of them then it is overhead for your system.So you need to be very clear about what you want from a framework and should choose the right framework otheriwse the framework may deteriorate the system performance.
I hope I have answered your question.
Hi Nishith,
Can you explain with any examples?
One more question is in current market lots of erp and bigger systems are in process to convert to silverlight/Wpf.
For bigger applications, to dynamically load modules MEF will be preferred or Prism ?
Hi Alap,
I am not sure what kind of example are you looking for but if we relate it to our real life projects then following example may help you.
Example:
Analyst x came across the requirement for an application which should work with WPF and silverlight. And also should be flexible enough for future changes and should also be easily manageable.
The analyst after studying the features of all the available frameworks zeroed in to MVVM light toolkit and Prism. Now one of the key requirement was of multi-targeting(should work with both WPF and silverlight) which made the choice pretty easy as only Prism out of the two selected frameworks provides facilities to create applications which work with both WPF and silverlight.
So it really depends on your project requirement and framework features.
Coming to your second question, if the purpose is of only dynamically loading modules then you should go with MEF as MEF (Managed Extensibility Framework) is primarily created for view management and dynamically loading modules. Prism has many more features like event aggregators for intra-module communication.
I hope I have answered your question satisfactorily.
Thank you Nishith.
It really helped me.
[...] get your app developed in the latest technology with UI as Silverlight and all Domain Operations based on the WCF Ria Services. Asp.net Web Forms are now a decade old [...]