Data Aggregation Using Presentation Model in RIA and Silverlight 4

No.of Views3354
Bookmarked3 times
Downloads 
Votes0
By  Manas Patnaik   On  10 Mar 2011 11:03:08
Tag : Silver Light and XAML , Applications
My earlier post was about the basic CRUD operations on entities with relation .We had discussed about creating simple association and querying as well as Invoking CRUD operation onto them.As i have mentioned there ,here in this article we will have an in-depth look into Presentation Model and its mode of handling related multiple entities.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

 My earlier post was about the basic CRUD operations on entities with relation .We had discussed about creating simple association and querying as well as Invoking CRUD operation onto them.As i have mentioned there ,here in this article we will have an 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 linksAdventureWorksLT 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 { getset; }
  5. public string FirstName { getset; }
  6. public string LastName { getset; }
  7. public string EmailAddress { getset; }
  8. public string Phone { getset; }
  9. public string AddressLine1 { getset; }
  10. public string AddressLine2 { getset; }
  11. public string City { getset; }
  12. public string StateProvince { getset; }
  13. public string PostalCode { getset; }
  14. public int AddressID { getset; }
  15. public DateTime AddressModifiedDate { getset; }
  16. public DateTime CustomerModifiedDate { getset; }
  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 ADVWORKDomainServicewith 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 asCustomerPresentationModel.

  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

Download source files -5165 kb

 
Sign Up to vote for this article
 
About Author
 
Manas Patnaik
Occupation-Software Engineer
Company-Infosys
Member Type-Fresh
Location-India
Joined date-10 Mar 2011
Home Page-www.manaspatnaik.com
Blog Page-www.manaspatnaik.com/blog
 
 
Other popularSectionarticles
    Performance is a vital part for any application possibly more important for web based app.With wizard based approach of RIA service generally we tends to comprise all entities, exposing to the client and also allowing the DomainService to go for everything from the database. Not only this approach takes a toll on security, but also it loads the middle tier unreasonably. So this post is about few tips using which we can improve the performance. Well mostly we will cover the Pagination, Limiting
    Published Date : 10/Apr/2011
    Silvelight is latest buzz in Microsoft.NET Technologies. Silverlight enable us to create rich user interface for the web based application
    Published Date : 16/May/2010
    This article will demonstrate how to build a traditional master page style application in Silverlight.
    Published Date : 16/Feb/2010
    In the previous lesson we talked about COM automation support introduced in Silverlight 4 and we said that COM automation is available only for Silverlight OOB (Out-of-Browser) applications that have Elevated Trust, and that’s one of the security restrictions imposed by Silverlight. Today, we’re going to talk about COM automation in more details and give few Silverlight examples that make use of this great feature.
    Published Date : 31/Mar/2011
    In April 2010 Silverlight 4 was released to the world with one of its great features ever, COM-automation support, that allows you to create cutting-edge applications that do everything you can imagine.
    Published Date : 27/Mar/2011
Comments
By:PeterDate Of Posted:3/10/2011 7:59:58 PM
The useful
Hi, Its nice article and good work.i have search in google for RIA and got this great article. keep it up.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</