Hello WCF RIA Services An introduction to RIA










































![Client Async Calls • Expose [Invoke] methods on domain service • Call method with Client Async Calls • Expose [Invoke] methods on domain service • Call method with](https://slidetodoc.com/presentation_image_h2/50f9fa87a6b7fd3f6b9e2475850cfe98/image-43.jpg)






- Slides: 49
Hello WCF RIA Services An introduction to RIA Services Gill Cleeren Microsoft Regional Director Silverlight MVP Ordina Belgium
Glad to meet you. . . Gill Cleeren Microsoft Regional Director Silverlight MVP Telerik Insider & MVP. NET Architect @Ordina (www. ordina. be) Speaker (Tech. Days BE-NL-UK-SE-CH. . . , Tech. Ed Berlin, Dev. Days, NDC Norway, Spring Conference UK, SQL Server Saturday Switzerland. . . ) • Visug user group lead (www. visug. be) • Author (Silverlight 4 Data and services cookbook) • www. snowball. be - gill@snowball. be - @gillcleeren • • •
Agenda • • • Part 1: Introduction to WCF RIA Services Part 2: The basics of RIA Services Part 3: Concepts explained Part 4: What about MVVM? Part 5: Let’s validate!
PART 1: INTRODUCTION TO WCF RIA SERVICES
The situation of the problem… Web service Database Trust Boundary Data access layer App logic
What is WCF RIA services exactly then? Framework, tools and services with prescriptive pattern for n-tier applications Framework: WCF RIA Services provide end-to-end use of data Retrieved by DAL of choice, shaped in BLL, annoted for validation Data and metadata can flow over tiers through controlled set of operations Tools: build multiple tiers together Code is generated on the client Services: common services such as authentication can be reused Based on ASP. NET foundation
Why WCF RIA Services? • Less plumbing code to write • Prescriptive approach for working with data on the client side • Makes working with WCF easier, hides details • Makes using LINQ possible in n-tier scenarios • Client side framework for service calls, validation and security
Important design principles • End-to-end view of an application: – Framework – Tooling – Services • Prescriptive approach to app logic • DAL-neutral and targets multiple presentation technologies • Make RIA development as productive as ASP. NET development • . NET on server and on client
Being neutral. NET Clients Silverlight, WPF (hopefully phone) Standards Clients Java. Script Server Rendering HTML, Sitemaps Services SOAP, XML, JSON, ODatabases ADO. NET, ORMs (LTS, EF, …) App Logic Unit Test Code Lists/Objects Repository (NHibernate, …) Services REST/SOAP (Azure, …)
How do I get WCF RIA Services? • If you have Silverlight tools installed, you have RIA Services already • Additional download: RIA Services Toolkit – Adds support for other endpoint types (SOAP, JSON) – ASP. NET support for consuming domain service in process • WCF RIA Services SP 1 – We’ll look at this later!
PART 2: THE BASICS OF WCF RIA SERVICES
The Dev Process: Server-Side 1 Create Business Application Create Entity Model 2 3 Create Domain Service 4 Add/generate queries Modify Metadata 5
Creating the server-side DEMO
The Dev Process: Client-Side Use Data Sources window 1 Drag on the interface 2 3 Note t one w hat this is ju ay of using st RIA Generate Domain. Data. Source 4 Success! servic es
Creating the client-side DEMO
PART 3: CONCEPTS EXPLAINED
Concepts of RIA Services Code generation Shared code Domain Data source WCF RIA SERVICES Validation Meta data Domain Context Querying Authentication
Server-side Domain Service Metadata Classes Validation Attributes Security Attributes Shared Code
Use query operations to read data • Inside the service, we can query data – Query operations return object, IEnumerable<T> or IQueryable<T> • IQueryable<T> and LINQ provider are required if we want to use deferred execution – Based on convention (Get. XXX, Retrieve. XXX…) • If convention isn’t possible, use Query attribute • Other attributes exist: – Is. Composable=false when returning single entity – Result. Limit to constrain maximum items returned to client
Let’s defer things • The use of IQueryable<T>: Deferred Execution – RIA Services serializes the expression tree from the client the server – LINQ operations specified by the expression tree are executed on the server – When the underlying provider supports it (i. e. Entity Framework and LINQ to SQL), deferred execution goes all the way to the DB
Querying Relations • What if we want to return related entities in one go? – Load all Hotels for a City – Step 1 (Entity Framework) • Object. Context. Cities. Include(“Hotels”) – Step 2: • Entity metadata type needs [Include] attribute on related property
Conventions can help us! • RIA Services uses naming conventions in domain service to identify methods – – Insert: Insert. XXX, Add. XXX, Create. XXX Update: Update. XXX, Modify. XXX, Change. XXX Delete: Delete. XXX, Remove. XXX Where XXX is an entity type name • If we want Dutch code (yes some people do that…), we can use Attributes – Insert, Update, Delete attributes
Metadata or buddy classes • • • Define a partial class for the entity type Define a nested metadata class inside the entity type – Not required, but convention – “Buddy class” Add a Metadata. Type attribute on the entity partial class to tie in the metadata type Add attributes to properties on the metadata type that match the entity properties Those attributes show up on the client entity type or influence entity transmission – Validation – Include – Exclude – Composition
Client-side Domain Context Entity Query Entity Load Operation Submit Operation
Client-side generated entities • On the client, entities get generated (code-generation) – Entity Framework or LINQ to SQL entities supported automatically – POCOs must follow certain conventions • Must have a [Key] property • Relation properties must have an [Association] attribute to indicate the related property type’s key fields • Need [Include] attribute on properties to child entity collections or single entities to have them serialized
Loading Data on the Client • Option 1: what you CAN do: the Domain. Data. Source – Allow you to execute a Domain. Context method to query data • Including updates • Happens async – Drag/drop data binding is VS 2010 generates XAML Domain. Data. Source and Grid • (we’ve seen this in the demo) – Supports parameterized queries – Supports sorting, grouping, filtering, paging Not a good fit with MVVM • Puts query “logic” into the view
Loading Data on Client • Option 2: What you SHOULD do: Domain. Context. Load() – Pass an Entity. Query<T> – Can modify the expression tree on client if target method in Domain. Service returns IQueryable<T> – Executes asynchronously – Bind to entity collection for automatic update via INotify. Collection. Changed – Pass async callback to be notified and work with results or handle errors yourself – Can work with returned Load. Operation<T> for async notification as well
Update Operations • Implicit unit of work: – Entity changes are cached in the client Domain. Context until Submit. Changes called Contains an Entity. Container for each entity type that tracks changes • Sent as a batch to the service – Including original version for optimistic concurrency • Service executes batch, calling Insert, Update, and Delete methods with one entity per method call
Security • • • Authentication Roles User profile Declarative: class / method-level Integrated into Query / Submit processing All based on ASP. NET infrastructure
What is added with SP 1? Cloud Ready Domain Collection View T 4 Templates Complex Type Support Shared Entities
DEEP-DIVE DEMO
PART 4: WHAT ABOUT MVVM?
RIA Services and MVVM == BFF
MVVM DEMO
PART 5: VALIDATION WITH WCF RIA SERVICES
Validation in Silverlight • Validation in Silverlight is based on the concept of data binding • Several options exist: – IData. Error. Info – INotify. Data. Error. Info • INotify. Data. Error. Info supports async validation – When we set a value, it may be OK client-side but not server-side – An event is raised if server-side validation fails – Silverlight watches for this event being raised • RIA Services Entity base class provides the implementation
INotify. Data. Error. Info interface public interface INotify. Data. Error. Info { bool Has. Errors { get; } event Event. Handler<Data. Errors. Changed. Event. Args> Errors. Changed; IEnumerable Get. Errors(string property. Name); }
RIA Services Validation Options We have 3 options: 1. Data. Annotations attributes 2. Server update method exceptions 3. Client async service method calls
Data Annotations Attributes • Live in System. Component. Model. Data. Annotations namespace – Also used by ASP. NET MVC and Dynamic Data • Contains a few standard attributes: – – [Required] [Range] [Reg. Ex] [String. Length] • And then there’s the Custom one… – [Custom. Validation] – Define static method with appropriate signature: public static Validation. Result Validate. City. Name(string new. City. Name, Validation. Context context)
Data Annotations how-to • WCF RIA Services will automatically evaluate data annotation attributes on both client and server side • For Entity Framework generated types on server, define metadata class and put attributes on its properties • Client generated entities contain same attributes • Custom. Validation methods must be put in a. shared. cs file so they get copied to the client side
Server Validation Exceptions • Some things have to be evaluated on the server – Can wait until update batch is submitted to evaluate validation rules server side – Throw Validation. Exception from insert, update, or delete method – RIA Services automatically associates the error with the individual entity and hooks into the validation client side – Will still be raised as an exception from the Submit. Changes method, must handle – Can also inspect properties of the Submit. Operation • Entities. In. Error collection
Client Async Calls • Expose [Invoke] methods on domain service • Call method with value to be changed, get back result • Set validation errors manually – Entity. Validation. Errors. Add
VALIDATION DEMO
Summary • RIA services opens up oppurtunity to build ntier apps with ease • Built on power of WCF • Key concepts explained • Works in combination with MVVM • Supports several validation types
Q&(HOPEFULLY)A
THANKS!
Hello WCF RIA Services Gill Cleeren Microsoft Regional Director Silverlight MVP Ordina Belgium
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U. S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. Partner logo to go here

















































