.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Data Aggregation Using Presentation Model in RIA and Silverlight 4

Posted By:Manas Patnaik       Posted Date: March 07, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

In-depth look into Presentation Model and its mode of handling related multiple entities.
 

Introduction , What is Presentation Model ??

Presentation Model deals with data from more than one entities which are related to each other and projecting it as one entity.

We usually come across  situations where there is not need to expose all the entities to the client side either for security reason or else to avoid unnecessary code at client side.So instead of exposing everything as separate entities we can aggregate the data as a separate dummy to client or for presentation layer  and can invoke operations onto it.

Presentation Model in RIA and Silverlight 4

Case Study

Lets check a Data Model from AdventureWorksLT Database for online transactions .AdventureWorksLT is a sample sql server database from Microsoft .You can find database schema with following image.

AdventureWorksLTDetail

More details regarding the database can be found from here.Also you can download database samples from codeplex  with following links AdventureWorksLT 2005 Version,AdventureWorksLT 2008 Version.For Presentation model scenario i am going to use a part of the Data Model which involves Customer and Address .

Presentation Model in RIA and Silverlight 4

Till now normally we used expose Customer ,CustomerAddress and Address to client side as 3 separate entities and applying association between them and showing the results by joining them over by LINQ at client side .Instead of above procedure we will have a look into how to create a dummy entity and allowing only required combined property to the client side.

Setting Up the Project

The first steps involves here to create a class for entity at server side project which will hold the custom properties we want to propagate .Lets name it CustomerPresentationModel.cs.
  1. public class CustomerPresentationModel
  2. {
  3. [Key]
  4. public int CustomerID { get; set; }
  5. public string FirstName { get; set; }
  6. public string LastName { get; set; }
  7. public string EmailAddress { get; set; }
  8. public string Phone { get; set; }
  9. public string AddressLine1 { get; set; }
  10. public string AddressLine2 { get; set; }
  11. public string City { get; set; }
  12. public string StateProvince { get; set; }
  13. public string PostalCode { get; set; }
  14. public int AddressID { get; set; }
  15. public DateTime AddressModifiedDate { get; set; }
  16. public DateTime CustomerModifiedDate { get; set; }
  17. }



Well the basic step needs no elaboration , you can refer to my previous post for setting up a project using Entity Framework and RIA .Download the AdventureLT database and create a project as mentioned over my earlier articles.Add a domain service named ADVWORKDomainService with out selecting entity from the wizard.
image

Querying Data from Data Model

The first step involves in adding a method that will return all the Customer with properties we want to expose.So create a method with in the domainservice at server side project that will fetch the data from the Customer ,CustomerAddress and Address entities and project it as CustomerPresentationModel.
  1. public IQueryable<CustomerPresentationModel> GetCustomersWithAddress()
  2. {
  3. return from c in this.ObjectContext.Customers
  4. join ca in this.ObjectContext.CustomerAddresses on c.CustomerID equals ca.CustomerID
  5. join a in this.ObjectContext.Addresses on ca.AddressID equals a.AddressID
  6. select new CustomerPresentationModel()
  7. {
  8. CustomerID = c.CustomerID,
  9. FirstName = c.FirstName,
  10. LastName = c.LastName,
  11. EmailAddress = c.EmailAddress,
  12. Phone = c.Phone,
  13. AddressLine1 = a.AddressLine1,
  14. AddressLine2 = a.AddressLine2,
  15. City = a.City,
  16. StateProvince = a.StateProvince,
  17. PostalCode = a.PostalCode,
  18. AddressID = a.AddressID,
  19. AddressModifiedDate = a.ModifiedDate,
  20. CustomerModifiedDate = c.ModifiedDate
  21. };
  22. }



Build the project and we can find the CustomerPresentationModel only is propagated to client side instead of all 3 separate entities.
image

Presentation Model in RIA and Silverlight 4

Executing this method asynchronously from Silverlight project and bind the result to data grid,
  1. private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
  2. {
  3. biLoading.IsBusy = true;
  4. //Module lvel DomainContext
  5. cont=new ADVWORKDomainContext();
  6. //Call the Domain Service Method to fill the CustomerPresentationModel from 3 entities
  7. System.ServiceModel.DomainServices.Client.LoadOperation<CustomerPresentationModel> lp=
  8. cont.Load(cont.GetCustomersWithAddressQuery());
  9. //Attach OnCompleted Event handaler
  10. lp.Completed+=new EventHandler(lp_Completed);
  11. //Create Pagination for Grid
  12. System.Windows.Data.PagedCollectionView pagedcollview = new System.Windows.Data.PagedCollectionView(lp.Entities);
  13. dpCustomers.Source = pagedcollview;
  14. //Assign Paged Collection to the Grid
  15. dgCustomers.ItemsSource = pagedcollview;
  16. }



On executing the project we can find the result as follows
Presentation Model in RIA and Silverlight 4

Updating the Presentation Model

Until now we were able to fetch the data from the Data layer using a presentation model.Obiviously the next step involves updating the entities from the presented model from Silverlight page.So we need to add another method to domainservice class on server side to save the changes back to the model and database.Lets add a method with Update Attribute wrapper
  1. [Update]
  2. public void UpdateCustomer(CustomerPresentationModel customerPM)
  3. {
  4. Customer customerEntity = this.ObjectContext.Customers.Where(c => c.CustomerID == customerPM.CustomerID).FirstOrDefault();
  5. CustomerAddress customerAddressEntity = this.ObjectContext.CustomerAddresses.Where(ca => ca.CustomerID == customerPM.CustomerID && ca.AddressID == customerPM.AddressID).FirstOrDefault();
  6. Address addressEntity = this.ObjectContext.Addresses.Where(a => a.AddressID == customerPM.AddressID).FirstOrDefault();
  7. customerEntity.FirstName = customerPM.FirstName;
  8. customerEntity.LastName = customerPM.LastName;
  9. customerEntity.EmailAddress = customerPM.EmailAddress;
  10. customerEntity.Phone = customerPM.Phone;
  11. addressEntity.AddressLine1 = customerPM.AddressLine1;
  12. addressEntity.AddressLine2 = customerPM.AddressLine2;
  13. addressEntity.City = customerPM.City;
  14. addressEntity.StateProvince = customerPM.StateProvince;
  15. addressEntity.PostalCode = customerPM.PostalCode;
  16. CustomerPresentationModel originalValues = this.ChangeSet.GetOriginal(customerPM);
  17. if (originalValues.FirstName != customerPM.FirstName ||
  18. originalValues.LastName != customerPM.LastName ||
  19. originalValues.EmailAddress != customerPM.EmailAddress ||
  20. originalValues.Phone != customerPM.Phone)
  21. {
  22. customerEntity.ModifiedDate = DateTime.Now;
  23. }
  24. if (originalValues.AddressLine1 != customerPM.AddressLine1 ||
  25. originalValues.AddressLine2 != customerPM.AddressLine2 ||
  26. originalValues.City != customerPM.City ||
  27. originalValues.StateProvince != customerPM.StateProvince ||
  28. originalValues.PostalCode != customerPM.PostalCode)
  29. {
  30. addressEntity.ModifiedDate = DateTime.Now;
  31. }
  32. this.ObjectContext.SaveChanges();
  33. }



The following piece of code of above method above creates 3 entities based on their ID
  1. Customer customerEntity = this.ObjectContext.Customers.Where(c => c.CustomerID == customerPM.CustomerID).FirstOrDefault();
  2. CustomerAddress customerAddressEntity = this.ObjectContext.CustomerAddresses.Where(ca => ca.CustomerID == customerPM.CustomerID && ca.AddressID == customerPM.AddressID).FirstOrDefault();
  3. Address addressEntity = this.ObjectContext.Addresses.Where(a => a.AddressID == customerPM.AddressID).FirstOrDefault();




and then the next steps follows with assigning the changed values , Followed by saving the changes.

Conclusion

Hence the Presentation Model are the Custom Entities built for the presentation layer instead directly the direct data model structure.As i used to say earlier the implementation is completely depended on the scenario and security.Hope this article is detailed enough to explain how to start with presentation model.Keep suggesting Thumbs up .


Source Code

|
Download Source Code -: PresntationModel.zip

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend