SIGN UP MEMBER LOGIN:    
ARTICLE

MVVM Pattern Implementation in Silverlight - part 1

Posted by Mahadesh Mahalingappa Articles | Silverlight with C# August 13, 2011
In this article we would see a implementation of MVVM Pattern using Silverlight.
Reader Level:
Download Files:
 

This article is all about the implementation of MVVM Pattern using Silverlight.

Checked out the following link : http://weblogs.asp.net/dwahlin/archive/2009/12/08/getting-started-with-the-mvvm-pattern-in-silverlight-applications.aspx

I just loved the Implementation by dwahlin. The implementation is very simple and easy to code. I have modified the implementation to talk to database.

Created a new Silverlight Project named as MVVMBasic.

Created a new DataModel as shown below:

datamodel in silverlight

Created a new Silverlight Enabled WCF Service as shown below:

public class DataService
    {
        [OperationContract]
        public List<Person> GetPeople()
        {
            ProjectsDataEntities context = new ProjectsDataEntities(); 
            var persons = from person in context.People
                          select person;
 
            return persons.ToList();
        } 
        [OperationContract]
        public OperationStatus UpdatePerson(Person p)
        {
           
try
            {
                ProjectsDataEntities context = new ProjectsDataEntities();
 
                Person currperson = (from person in context.People
                                     where person.id == p.id
                                     select person).First();  
                currperson.LastName = p.LastName;
                currperson.FirstName = p.FirstName; 
                context.SaveChanges();
                return new OperationStatus { Status = true };
            }
            catch(Exception ex)
            {
                return new OperationStatus { Status = false };
            } 
        }       
    }
 

All the remainging things I have retained from the article by dwahlin.

Dwahlin returns the update status from the database in a nice way. 

He uses a class PeopleEventBus as shown below: Basically this is a event handler to track the Completed Database Update Operation.
 
  public static class PeopleEventBus
    {
        public static event EventHandler<OperationCompletedEventArgs> OperationCompleted;
        public static void OnOperationCompleted(object sender, OperationCompletedEventArgs e)
        {
            if (OperationCompleted != null)
            {
                OperationCompleted(sender, e);
            }
        }
    }
 

The Custom Event Args class which has the OperationStatus as a property. 

    public class OperationCompletedEventArgs : EventArgs
    {
        public OperationStatus OperationStatus { get; set; }
    }

 
Finally the Operation status class :
 
    public class OperationStatus
    {
        public bool Status { get; set; }
        public string Message { get; set; }
    }  

Tne below lines of code take care of catch the event and displaying the status:
 
  void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.ViewModel = this.Resources["ViewModel"] as PeopleViewModel;
            PeopleEventBus.OperationCompleted += new EventHandler<OperationCompletedEventArgs>(PeopleEventBus_OperationCompleted);
        } 
        void PeopleEventBus_OperationCompleted(object sender, OperationCompletedEventArgs e)
        {
            MessageBox.Show("Operation Status: " + e.OperationStatus.Status.ToString());
        } 

In the View Model we add the below line where WCF method returns the status :
 
void UpdatePerson_Completed(object sender, UpdatePersonCompletedEventArgs e)
        {
            PeopleEventBus.OnOperationCompleted(this, new OperationCompletedEventArgs { OperationStatus = e.Result });
        }

Lets give this a run.

wcf methode in silverlight

Let me modify the row and see if the update works:

Datamodel output in silverlight

Great it works.

In my next post I will add commanding to this example . Till then Happy Coding.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor