Complete Generic MVVM in Silverlight Part 1

Vuyiswamb
Posted by in Silverlight category on for Beginner level | Points: 250 | Views : 7898 red flag
Rating: 5 out of 5  
 2 vote(s)

Almost everyone doing development in Silverlight is talking about MVVM. This is the part 1 on the subject. I decided to split the articles into different parts as this tends to be a confusing subject for the beginners in the subject. What is good about this article is that I will cover the basics of MVVM and give you a full explanation on how you can retrieve, Edit, add, Update and delete data in MVVM and this is the only article I have seen so far that can do that. Another great thing about this article is that we are not going to use any Toolkit to enforce the rules of MVVM, So this is a Generic MVVM Article, you don’t need to install anything or download anything to follow this article.

Introduction 

Almost everyone doing development in Silverlight is talking about MVVM. This is the part 1 on the subject. I decided to split the articles into different parts as this tends to be a confusing subject for the beginners in the subject. What is good about this article is that I will cover the basics of MVVM and give you a full explanation on how you can retrieve, Edit, add, Update and delete data in MVVM and this is the only article I have seen so far that can do that. Another great thing about this article is that we are not going to use any Toolkit to enforce the rules of MVVM, So this is a Generic MVVM Article, you don’t need to install anything or download anything to follow this article.

Objective


The objective of this article of this first part of the series is to give you a brief description of MVVM.

 

What is MVVM

 MVVM is nothing else but a pattern. A pattern is meant to solve a problem. One pattern cannot solve the entire problems that you have across different applications. Before you design an application you must decide on a pattern to use. There are other Patterns that work well for certain Technologies.

Every Pattern has its own advantages and disadvantages. MVVM is good for XAML based applications like Silverlight and WPF. One of the main advantages that I saw in MVVM is the code reusability and Data binding.

Why Choose MVVM

       Separation of concerns

       Developers and designers can coexist in harmony

       Makes unit testing easier

       Reduces repetitive/mundane code by using data binding

       Improves maintenance and scalability 

The history Before MVVM

Before MVVM there was N-Tier Architecture. This Pattern did well and there are still some other applications that are Build using that pattern or architecture. The N-Tier Pattern is still good till today, but it solve certain problems. The Following diagram shows you how N-Tier looks

As you can see from this diagram, the separation is done well and re-usability of code is encouraged and separation of concern is also promoted. If you are not familiar with the pattern let me explain how it works. The Presentation Layer is what the user interact with (ASP.NET Pages, Silverlight Page, WinForm). The idea here is that the Presentation Layer will access the data via the business Layer and the Business Layer will access the Data via the Data Layer. So the Presentation Layer cannot access the Data Layer directly, in fact the Presentation Layer does not know that the data Layer Exist or the data Layer does not know that the Presentation Layer exist. So Basically the Business Layer is the Middle man between the Presentation Layer and the data Layer. The purpose of the Business Layer is to make sure that the business rules are not broken. But due to types of validation we have in different technologies, most of the business rules are done on the Database or on the Presentation layer on the client side. So this leaves the business layer as a message transporter and the code that is in there is just redundant. So this becomes an extra work when working in Silverlight and WPF, which means you need to call the data Layer functions and nothing else, one might agree that it is now turning to be a redundant layer in the pattern.

MVVM solves this problem by making sure that the Business Layer comes into the party and become an active Layer in a pattern that plays an important role and in MVVM it was named “ViewModel”. The DataLayer in MVVM is called a “Model”.  The Presentation Layer is called a “View”. The following diagram demonstrates MVVM.

From the explanation I gave, you now have a clear understanding of the pattern so far. The Model will be the DataLayer, where we execute our StoredProcedure. The ViewModel will be our Business Layer that comes into the party in this pattern. This is where the code that we normally use to call the Model resides. This is where we make sure that our business rules are not broken. The View is our Silverlight Page or WPF Forms.

Conclusion

This was the first part of the series in MVVM. In the next part of this series I will start from the Model and explain what the Model should contain.

Thank you for Visiting Dotnetfunda

Page copy protected against web site content infringement by Copyscape

About the Author

Vuyiswamb
Full Name: Vuyiswa Maseko
Member Level: NotApplicable
Member Status: Member,MVP,Administrator
Member Since: 7/6/2008 11:50:44 PM
Country: South Africa
Thank you for posting at Dotnetfunda [Administrator]
http://www.Dotnetfunda.com
Vuyiswa Junius Maseko is a Founder of Vimalsoft (Pty) Ltd (http://www.vimalsoft.com/) and a forum moderator at www.DotnetFunda. Vuyiswa has been developing for 16 years now. his major strength are C# 1.1,2.0,3.0,3.5,4.0,4.5 and vb.net and sql and his interest were in asp.net, c#, Silverlight,wpf,wcf, wwf and now his interests are in Kinect for Windows,Unity 3D. He has been using .net since the beta version of it. Vuyiswa believes that Kinect and Hololen is the next generation of computing.Thanks to people like Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject), Sheo Narayan (.Netfunda),Rajesh Kumar(Microsoft) They have made vuyiswa what he is today.

Login to vote for this post.

Comments or Responses

Posted by: debal_saha-9451 on: 11/17/2011 | Points: 25
Nice Article . Good starting .
Posted by: Vuyiswamb on: 11/18/2011 | Points: 25
Thanks

Login to post response

Comment using Facebook(Author doesn't get notification)