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