RIA services exposing & consuming queries

Preview:

DESCRIPTION

Slides from week 8 of the Inland Empire .NET User's Group Silverlight 4 class

Citation preview

Week 7 – WCF RIA Services

Overview

Jim LaVinejim.lavine @gmail.com

Remaining Schedule• 1-8 Exposing and consuming querying services• 1/15 Updating data• 1/22 Business Logic and validation

Agenda• Domain query rules• The DomainContext Class• Asynchronous loading• Client-side data caching• Shaping data in the client• Retrieving object hierarchies• A peak under the hood

Domain Service

Design-time workflow

Domain Service

Data model

MetadataEntityMetadataEntity

MetadataEntity

DAL

L2S EF REST/SOAP POCO

Server project

CRUD

Custom logic

Client project

Domain Context

MetadataEntity proxy typeMetadataEntity proxy type

MetadataEntity proxy type

UI Views

Entity Container

Load

Invoke

Build

1

2

4

3

Bindable

Domain query operation rules [EnableClientAccess()] public class ChinookDomainService : LinqToEntitiesDomainService<ChinookEntities> { public IEnumerable<Invoice> GetInvoiceByCustomer(int customerId) { return ObjectContext.Invoices.Where(c => c.CustomerId == customerId); }

public Customer GetCustomerById(int customerId) { return ObjectContext.Customers.Where(c => c.CustomerId == customerId); } }

public

IEnumerableIQueryable

<T>

Entity

Supportedtypes

public IEnumerable<Customers> GetCustomersByJoinData(DateTime mindate, DateTime maxdate)

{ return ObjectContext.Customers.Where(c => c.JoinDate >= mindate &&

c.JoinDate <= maxdate); }

NoOverloads

public class Customers { [Key] public int CustomerId { get; set; } public string FName { get; set; } . . . }

Domain query operation rules

Identity

Convention vs. configuration• Query operation explicit marking : QueryAttribute• Signature match : returns IQueryable<T>, IEnumerable<T>, T• QueryAttribute parameters

– IsComposable– ResultLimit– HasSideEffects

[Query] public Iqueryable<Customers> GetCustomersByState (BillingState state) { . . . }

public Iqueryable<Customers> GetCustomersByState (BillingState state) { . . . } [Query (IsComposable=false)] public SalesPerson GrabTopSalesperson (int year) { . . . }

Demo• Defining domain queries

Code generation

l

Server assemblies Silverlight Client

Entity type

Custom Attribute

Silverlight client

Entity type

Entity type

Metadata class

Entity proxy

Custom Attribute

DomainContext types

Query factory methods

Entity proxy

Entity proxy

Namespace

Class name

Public props

InotifyPropertyChangedValidationMetadata

NamespaceDomainService types[EnableClientAccess]

Public methods

The typed DomainContext class

• Constructors• Query proxy methods• Entity proxy properties• Entity container factory

Demo• Examining generated classes

Asynchronous loading

ChinookDomainContext ctx = new ChinookDomainContext();

var query = ctx.GetInvoicesByCustomer (1) ; LoadOperation<Invoice> op = ctx.Load(query);

CustomerGrid.ItemsSource = op.Entities

EntityQuery<Invoice>

LoadOperation<Invoice>

IEnumerable<Invoice>

The Load methodLoad method overloads• Load(EntityQuery<TEntity>)• Load(EntityQuery<TEntity>, LoadBehavior, Callback, UserState)• Load(EntityQuery<TEntity>, Callback, UserState)• Load(EntityQuery<TEntity>, ThrowOnError)• Load(EntityQuery<TEntity>, LoadBehavior, ThrowOnError)

LoadOperation<Invoice> op = ctx.Load<Invoice>(query,lo => { /* Check for errors, bind to UI, do work */},null);

ctx.Load<Invoice>(query).Completed += new EventHandler(LoadComplete);

void LoadComplete(object sender, EventArgs e){/* Check for errors, bind to UI, do work */ }

LoadOperation

Load Operation: AllEntities Entities EntityQuery TotalEntityCount

OperationBase: INotifyPropertyChanged IsComplete, Completed,

event Cancel(), CanCancel,

IsCanceled Error, HasError

Demo• Loading data

Caching query results

DomainContext

DomainService

EntityContainer

EntitySet Entity

Set EntitySet

EntityEntity

Entity

Change trackingValidationData bindingUpdating

Sendquery

QueryResults

QueryResults

INotifyPropertyChanged

EntitySetpropertyLoad()

Demo• Binding to cached data

Load options

Entity Container

EntitySet

Changes (local)Changes (remote)

?

LoadBehavior:

-KeepCurrent-MergeIntoCurrent-RefreshCurrent

Load()

Demo• Using LoadBehavior

Client side data shaping

EntityQuery<T>e.g. GetProductsQuery()

QueryNameParametersQueryIsComposable

LINQ operators(Where, Skip,Take, OrderBy)

EntityQueryable(extension)

DomainService

Client

Server

Server query +Client-side querycombined

LINQ operators(Where, Skip,Take, OrderBy)

Demo• Filtering data

Retrieving object hierarchies

Server

Server-sideEntity type

[IncludeAttribute]

MetadataClass

Domain ServiceIQuerable<foo>

GetFoos()

Include

foo1

Client

EntityContainer

foo1 [Association]new props

Demo• Returning related data

A look behind the scenes

DomainContext

DomainService

WebDomain Client

BeginQuery()

WCF Channel Factory

WCF Client Channel

WCF ServiceContract DomainServic

eHttpModule

Virtual.SVC file<%@ServiceHost

Service=“XYZService Factory=“System.Web.Ria.

DomainServiceHostFactory”%>

WCF ServiceContract

DomainService

HostFactory

DomainServiceHost

Operation Description

QueryResult<T>

REST/binary endpoint

Summary

• Integrated Infrastructure• Signature-based conventions• Metadata-specific object heirarchies• Simplified asynchronous calls• Client-side data caching• Integrateing data shaping• REST-based default configuration