B: Build a Silverlight app with WCF RIA Service
Build a
Silverlight app with Entity Framework Database First approach, we follow the exact procedure from this article,
WCF - Ria Services (step 1~10), with the same project, service and data names. If any one is interested with the code, you can download it from another article,
Silverlight 5 WCF RIA Services.
Step 1 - Create an Silverlight app
We use the Visual Studio 2010 and .NET Framework 4 to build the app::
- Start Visual Studio and select Create a new project.
- In the Create a new project dialog,
- on the left side panel select Silverlight,
- on the right Select Silverlight Application (.NET Framework)
- Named as SLWCFRiaServices > OK.
- Choose Silverlight 5 and Check Enable WCF RIA Services => OK
We got two projects: One is Silverlight front end, named SLWCFRiaServices, another is the server (middle tier) named SLWCFRiaServices.Web, Note: a group of System.ServiceModel DLLs are added when we choose to Enable WCF RIA Service support.
Step 2, Reverse Engineer Model
We’re going to make use of Entity Framework Designer on the server project SLWCFRiaServices.Web:
We do the same as what we did in A: Step 2 above for WPF, and finally, we got:
While different from WPF, after adding the entity into the web app, there is no DataSource object created. Now, it is where the WCF RIA Service jump into.
Step 3, Add Domain Service Class
We’re going to add a Domain Service Class on the server project SLWCFRiaServices.Web:
- Choose the data, enable editing => OK
There are two files created: DataDomainService.cs and DataDomainService.metadata.cs, actually, they are the DataContext class and Data Entity Class:
DataDomainService.cs --- DataContext class:
- namespace SLWCFRiaServices.Web
- {
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Data;
- using System.Linq;
- using System.ServiceModel.DomainServices.EntityFramework;
- using System.ServiceModel.DomainServices.Hosting;
- using System.ServiceModel.DomainServices.Server;
-
-
-
-
-
-
- [EnableClientAccess()]
- public class DataDomainService : LinqToEntitiesDomainService<PublishingCompanyEntities>
- {
-
-
-
-
- public IQueryable<Article> GetArticles()
- {
- return this.ObjectContext.Articles;
- }
- }
- }
DataDomainService.metadata.cs --- Data Entity Class
- namespace SLWCFRiaServices.Web
- {
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.ServiceModel.DomainServices.Hosting;
- using System.ServiceModel.DomainServices.Server;
-
-
-
- [MetadataTypeAttribute(typeof(Article.ArticleMetadata))]
- public partial class Article
- {
-
-
-
-
-
-
-
-
-
- internal sealed class ArticleMetadata
- {
-
- private ArticleMetadata()
- {
- }
-
- public int ArticleID { get; set; }
- public Nullable<int> AuthorID { get; set; }
- public string Body { get; set; }
- public string Title { get; set; }
- }
- }
- }
But we still have not seen the DataSource object. However, if we swich to the Silverlight Project, the Data source object is showing up:
Actually, this is what we choose to Enable WCF RIA Service support to do, otherwise, it won't happen.
Step 4, Build the Grid, and Run the app
Exactly the same as WPF project, we add a grid, and run the app:
C: Understanding of WCF RIA Service
Now, we may have a better understanding of WCF RIA Service:
- It works on Silverlight
- It brings middle tier entity classes to client side without duplicating the classes.
- Middle tier Entity classes are built up by Entity Framework, or say, ADO.NET Entity Data Model.
- The Bridge to link middle tier and client side is the Domain Service Class that supplys a WCF services.
The following figure shows the architecture of a WCF RIA Domain Service −
from
WCF RIA Services – Querying Data
The following figure shows how a query is created on the client side
and executed on the server side to return queryable results. DAL stands
for Data Access Layer. from
Summary