Add New Skills!

Udemy
Showing posts with label RIA Services. Show all posts
Showing posts with label RIA Services. Show all posts

Sunday, October 9, 2011

RIA Services Basics

This post describes the basic elements of RIA services and is summary of the presentation from Tech Ed’11 by Brian Noyes.

What is RIA Services?

RIA services is all about making it easy to write client applications. It is built on WCF (Windows Communication Foundation) and it simplifies writing line of business applications. It is highly dependent on push-pull of data and can be used for non-CRUD operations. RIA services acts as a pipeline/infrastructure/communication layer between the presentation layer and the backend without having you to get into the complexities of WCF. RIA services lets you focus on the functionality and not worry about the plumbing that goes behind the scenes as part of WCF (Windows Communication Foundation).

image

Current clients supported are Silverlight and ASP.NET and future releases may include JavaScript (WCF JQuery).

Benefits:

  1. Built on WCF – just define what you want to expose from the server and use it on the client side – WCF pipeline is auto generated
  2. Client Query/Updates – ability to query data is easy using LINQ. Updates are also possible.
  3. Change Tracking – Change tracking of old values and new values is possible, you can decide when you want to submit the changes from the client side
  4. Shared Validation – just define the validation rules once and it automatically get applied on both client and server side
  5. Security – you can authenticate and authorize from the client side
  6. Flexible Data Source – you can pretty much use any data source. It best works with LINQ supported data sources because it gives you deferred execution across tiers.

Tools:

  1. Silverlight for Visual Studio 2010
  2. RIA Services toolkit
  3. Visual Studio SP1

Architecture:

image

Web Project

  • Silverlight host
  • Domain Services

Client Project

  • Generated Code (Compile)
  • Domain Context Classes (one/domain service)

With RIA Services, you generally first focus on Data Access and Entities that need to be exposed to the client. Define the back-end functionality of your application using a Domain Service (which is the base class you’ll derive from) which essentially exposes data to your client. When you compile it will generate new Domain Context and Entities on the Client which will mirror Entities on the server. Entities generated on the client will contain some base class functionality such as property change notifications for validation support. Finally you build your view to display your data.

Lets get started:

Start by creating a Silverlight Application in VS 2010:

image

image

When you enable WCF RIA Services, the basic plumbing between client-server side projects are setup.

Start by adding your database:

image

image

You’ll most likely generate from a database:

image

image

This will create the edmx file:

image

Next we will add the Web>Domain Service Class to the Server side project:

image

What this does is, it looks at the entity framework we have in the project and bring it up for selection and generates the code for us – query and update patterns. This is a onetime code generation when you add the domain service class.

image

When you check “Enable Editing”, it will generate the insert/update/delete code for the related entity. “Expose OData endpoint” will generate the code to expose the data in OData format.

This will add Domain Services References to the project:

image

This also adds <>DomainService.cs file, <>DomainService.metadata.cs file and updates the web.config on the server.

On the client side, domain contexts are setup that act as proxy to the domain service.

image

You can drag and drop the code generated entity types also known as Domain Data Source and drag and drop them on the MainPage.xaml.

image

This will create a data grid:

image

Run the project and you’ll see that the data grid is populated:

image

Note there was no code written which is perfect for Rapid Application Development (RAD) scenarios.

Now, if you just add a button to the xaml and write the following code, it will submit any changes to the database:

image

Edit a record and submit changes:

image

You can refresh the app and you’ll note that the screen reflects the updates.

RIA Services Update Operations:

Entity changes are cached in the client DomainContext which contains a Entity Container that tracks changes. Changes are sent as a Batch to the RIA Service which includes the original record for Optimistic Concurrency. The RIA service executes batch calling insert, update and delete with one entity per method call. RIA Service also returns the server updated entities and refreshes the client cache.

RIA Services Validation:

RIA services provides seamless client and server validation.

Data Annotation Attributes – these are standard in .NET4 and used by Silverlight RIA Services, ASP.NET dynamic data, ASP.NET MVC. This is simply done by tacking on attributes to properties of your entities types. The built in ones are: required, string length, regular expressions, ranges. You can also have your own custom logic.

Server-side update method logic – this lets you create additional logic on the server-side and can override validation done at the client side. This can simply be done by defining validation exceptions.

Aync [Invoke] method execution – you can expose arbitrary methods and invoke them asynchronously from the client.

Examples of Required:

image

image

Custom Validator:

image

To implement validation we need to use the Metadata code that was generated with the Entity Framework.

Setup a Partial Type and associate it with a buddy/metadata type.

image

The buddy type declares all the properties the real type has. Note below TimeEntries and Customer are properties of the real type: Task. We don’t need to redeclare ALL the properties, but only the ones where we will add some validation.

image

Required Field Validation:

image

Phone Number Validation:

image

Exposing data using OData, SOAP, JSON

You can expose your Domain Service via OData, SOAP and JSON.

OData – uses REST – ATOM standard and is readonly

SOAP – using BasicHttpBinding

JSON – using REST-JSON serialization

Define the following in Web.config

image

Resources:

http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/MID311