Developing with WCF RIA Services Quickly and Effectively Nikhil Kothari Software Architect,...

26
Developing with WCF RIA Services Quickly and Effectively Nikhil Kothari Software Architect, Microsoft Corporation Blog: http://www.nikhilk.net Twitter: @ nikhilk

Transcript of Developing with WCF RIA Services Quickly and Effectively Nikhil Kothari Software Architect,...

Developing with WCF RIA Services Quickly and Effectively

Nikhil KothariSoftware Architect, Microsoft Corporation

Blog: http://www.nikhilk.netTwitter: @nikhilk

Agenda

• Getting started on an application– End-to-end introduction

• Going beyond the basics– Implementing MVVM/ViewModel– Designing the application logic

• Tips and guidelines along the way• Sneak peek at future possibilities

The RIA Services Vision

• Simplify n-tier data-driven application development

• A higher level framework on top of existing building blocks– LINQ, Data Access Technologies, WCF, Metadata

• Provide an application architecture that scales to fit needs of breadth line-of-business applications

What would you like to see?

• http://www.nikhilk.net/RIA-Services-MIX10.aspx• Pick a real-world scenario; skip the drag/drop

– An example of when UI is not mapped one-to-one with a database table

– An example of projections across tables– An example where DAL types are not passed to the client– An example of using a stored procedure– When to use [Invoke] vs. Insert/Update/Delete methods?– How to implement the combobox/lookup table scenario?

• How to make RIA Services work with MVVM?

DEMOAn End-to-End Introduction to RIA Services

BookClub Application

Application

DB

Services

Data Access Layer

AppLogic

Presentation

Logic

Network

DomainServiceStateless CRUD + App Logic

class BookShelfServiceIQueryable<Book> GetBooks()void ShareBook(Book)void UpdateBook(Book)void UnshareBook (Book)Validation, authorization, business rules

Data Model class Book

DomainContextStateful view + Bindable Data

class BookShelfContextEntitySet<Book> Books { get }EntityQuery<Book> GetBooksQuery()

Entitiesclass BookData members + Validation

Data Model +Metadata +

Shared Code

DomainService.Query Invoke GetBooks() Compose query, execute, serialize resultsDomainService.SubmitChanges Authorize, Validate, Execute, Persist

var q = Context.GetBooksQuery()Context.Load(q);

Context.Books.Add(newBook);Context.SubmitChanges();

Queries and ChangeSets

Thinking ViewModel aka MVVM

• A pattern for separating application behavior out of the user interface

• Motivation– Facilitate designer/developer workflow, and

independent design/development activity– Encapsulate state and operations of application– Improve maintainability– Facilitate testability

ViewModel + RIA Services

Application

Data ModelServices, Business

Rules

View ModelApp State, Operations, Notifications

Presentation

XAML, UI Logic

<UserControl.DataContext> <local:BookShelfModel /></UserControl.DataContext>

<ItemsControl ItemsSource=“{Binding Books}” />

<Button Content=“Load” Command=“loadCommand” />

public class BookShelfModel : INotifyPropertyChanged { BookShelfContext _bookShelf; public IEnumerable<Book> Books { get; } public void LoadBooks(string) { // use _library to perform search }}

public class Book : Entity { … }

public class BookShelfContext : DomainContext {

public EntitySet<Book> Books { get; }}

Sketching the ViewModel

Books SelectedBook(two-way)

SelectedBook

LoadBooks()

LoadMoreBooks()

SaveBooks()

ShareBook()

UnshareBook()EditBook()

DEMOImplementing the ViewModel Pattern with RIA Services

The Role of DomainServices

• Encapsulate reusable application logic and expose it as a set of CRUD– Meaningful tasks unique to your application

• Interesting implementation scenarios– Constraining results– Validation, authorization and business rules– Shaping data– Aggregating data– Caching– Triggering business processes– …

Designing DomainServices - Identifying the Tasks

GetBooks

UpdateBookUnshareBook

ShareBook

Designing DomainServices -Identifying the Tasks

RequestBook

GetBooksOfDay SearchBooks

GetLatestBooks

Designing DomainServices – Presentation Model Types

DomainServices + Entities

Layering your DomainServices

Data Access Layer + DAL Types

BookClub ObjectConte

xt

Reference DataService

BookClub Service

BookMembe

rReques

tChecko

utCatego

ry

BookInfo

IQueryable<Category> GetCategories()

IQueryable<BookInfo> GetBooks()IQueryable<BookInfo> SearchBooks()IQueryable<BookInfo> GetBooksOfDay()void RequestBook(int bookID)

Category

Amazon

BookShelf Service

Book

IQueryable<BookInfo> GetBooks()void ShareBook()

void UnshareBook()void UpdateBook()

DEMOWalkthrough of an Interesting DomainService Implementation

Recap: Tips and Guidelines

• Constrain your queries– Parameters, business rules, result limits

• Define specific operations on your domain service that match your application semantics

• Use validation to enforce rules, and provide useful user experience– Member, Entity, Operation, Server-only, Async

• Use authorization to restrict access to data and operations• Handle errors – any Load/Submit could fail• Separate out reference data to facilitate sharing and optimal

loading

Recap: Some More Tips

• Focus on the tasks that define your application– Customize your DomainServices– Group logically related tasks per DomainService

• Create custom entity types designed for use between client and server when appropriate– Expose sufficient and minimum amount of

information– Aggregate, re-shape, project on the server– Consider security, band-width and latency

Scaling your DomainServices

AppLogic

DatabasesADO.NET,ORMs (LTS, EF, …)

Lists/ObjectsRepository(NHibernate, …)

ServicesREST/SOAP(Azure, …)

.NET ClientsSilverlight, Phone,WPF

Standards ClientsJavaScript

Server RenderingHTML, Sitemaps

ServicesSOAP, XML,JSON, OData

Unit Test Code

DEMORIA Services + Windows Phone 7

Related Talks

• CL08 – Microsoft Silverlight 4 Business Applications

• CL50 – Search Engine Optimization for Microsoft Silverlight

• FT15 – Accessing Data in a Microsoft Connected Web Application

• EX14 - Understanding the Model-View-ViewModel Pattern

• RIA Services Meetup at 8pm today in 24001

Roadmap and Further Information

• Bits– Today: Feature-complete RC build that works with SL4 and

VS2010 RC– V1: Ship with SL4 on top of .NET 4/VS2010– Hosters starting to get on-board (eg. DiscountASP, OrcsWeb)

• More Information– http://silverlight.net/riaservices - RIA Services Online– Blogs

• http://www.nikhilk.net• http://blogs.msdn.com/brada• http://jeffhandley.com

Call to Action

• Get a jumpstart on your next Silverlight-based LOB/data-driven app– RIA Services builds on WCF and Silverlight 4 and provides

end-to-end solutions– Takes care of services infrastructure, binding infrastructure

• Implement well-architected manageable application, and work well for designer/developer workflow– RIA Services is complemented by patterns such as task-

centric design approaches and ViewModel

Q&A

© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.