Anda di halaman 1dari 37

Data Access with Silverlight 2

John Papa
ASPSOFT, inc.
www.aspsoft.com

john@johnpapa.net
www.johnpapa.net
INETA
The Face of .NET
● World-wide association of .NET User Groups
– 860+ groups world-wide
– Over 810,000 developers
● Started and run by .NET User Group leaders
● Sponsors and supports both .NET and other
professional user groups all around the world
● Sponsors
– Microsoft
– Verio
– New sponsors welcome!
● North America (NorAm) INETA Speaker Bureau
consists of 67 world-renowned speakers
www.ineta.org
20,000 Foot View
● What is in Silverlight 2 ?
● XAML based data binding
● Notifications
● Converters
● Data Services
– Self Describing
– Nimble
What’s New And Notable?
● Framework Languages and .NET Support
● Isolated Storage
● Non Contractual Data Services: JSON, REST,
SOAP/WS-*, POX, and RSS Web Services
● Cross Domain Network Access
● LINQ to Objects and LINQ to XML
● StackPanel, Grid and Panel Layout Support
● Full suite of Controls
● Deep Zoom Technology
4 Flavors of LINQ
● LINQ to Objects
– Use and abuse on the Silverlight client with any
type of object collection
● LINQ to XML
– Use and abuse on the client to consume XML
from a variety of data services
● Server based business rules
– LINQ to SQL
– LINQ to Entities
Manual Binding
● Push and Pull
– Pull a value from a data source property into a
control
– Push a value from a control to a data source
property
● Repetitive code
● Manual process
● Can write a binding class to handle it
Where to Bind ?
● Type XAML in Visual Studio
● Type .NET code in Visual Studio
● Type XAML in Expression Blend
● Use Expression blend Designer
XAML Binding
● Automated binding
● Bind a FrameworkElement to a data source
● Dependency Property
● DataContext
● Binding Modes
● Rule #1
– The target of data binding must be a
FrameworkElement
Rule #1
● The target of data binding must be a
FrameworkElement
Rule #2
● The target property of data binding
must be a Dependency Property
– data bound properties whose value is
determined at runtime
Dependency Property At Work
public static readonly
DependencyProperty FillProperty;

public Brush Fill


{
get { return (Brush)
this.GetValue(FillProperty); }
set { base.SetValue(FillProperty,
(DependencyObject) value); }
}
Dependency Property Terms
Binding Extensions Syntax
<someFrameworkElement property="{Binding}" .../>

<someFrameworkElement property="{Binding pathvalue}" .../>

<someFrameworkElement property="{Binding Path=pathvalue}"


.../>

<someFrameworkElement
property="{Binding oneOrMoreBindingProperties}" .../>

<someFrameworkElement property
="{Binding pathvalue, oneOrMoreBindingProperties}" .../>
Binding Extension Properties
● Converter
● ConverterCulture
● ConverterParameter
● Mode
● NotifyOnValidationError
● Path
● Source
● ValidatesOnExceptions
RunTime Binding
● FrameworkElement.SetBinding method
– Sets the binding

● Create a new Binding(“bindingName”)


● Set the binding source
● Associate the binding target to the
binding source
RunTime Binding - Example
var firstNameBinding = new
Binding("FirstName");

firstNameBinding.Source = person;

tbFirstName.SetBinding(
TextBox.TextProperty,
firstNameBinding);
DataContext is the Binding Hub
DataContext
● Mystical and magical data source? No.
● Allows you to set a data source for a
specific context
– In other words you can …
● Have different data sources for different target
controls
● Inherit data sources
● Supports 1 way, 1 time, and 2 way bindings
Binding Modes
OneTime OneWay TwoWay
Target is Updated Yes Yes Yes
When DataContext is
Set

Target is Updated No Yes Yes


When Source
Changes
Source is Updated No No Yes
When Target
Changes
Binding Mode Roles
INotifyPropertyChanged
● PropertyChanged event

● OneWay and TwoWay listen for it

● Easy to implement
– keep it clean
ObservableCollection<T>
● Lists listen to ObservableCollection<T> for
changes

● Implements INotifyPropertyChanged

● Implements INotifyCollectionChanged
– Notifies when list contents change
Converters
● Can convert values to and from

● Value in data source property may need


conversion when bound to target

● IValueConverter interface
– ConvertTo
– ConvertBack
Consuming Data Services
Silverlight Data Services
● Services that describe themselves
– SOAP
– WCF with contracts

● Services that do not describe themselves


– REST (REpresentational State Transfer)
● More intricate requests
● Can return JSON or XML
● Astoria / ADO.NET Data Services uses REST
– POX
● Plain Old XML
Silverlight and WCF Service
● ServiceContract
● OperationContract
● DataContract
● DataMember
Setup of WCF
● Silverlight Enabled WCF Service
– Auto sets binding

● Otherwise, set basicHttpBinding in


web.config
Serialization and Referencing
● Add Service Reference
– Get the contract

● Conversion to ObservableCollection<T>

● All calls are async


Cross Domain Issues
● Silverlight client calls a remote server’s
service
● Service is on different domain than the
Silverlight client’s hosted server
● Rejection by default
● Must allow service to be called
– REST, WCF, SOAP
● Format:
– http://msdn.microsoft.com/en-
us/library/cc645032(VS.95).aspx
Cross Domain Policies
● Silverlight looks for both
– ClientAccessPolicy.xml (Silverlight)
– CrossDomain.xml (Flash format)
● File must be in the web root
– NOT APP ROOT!

● Use sniffer to detect problems


– i.e. Page Not Found error
ClientAccessPolicy.xml
<?xml version="1.0" encoding="utf-
encoding="utf-8"?>
<access-
<access-policy>
<cross-
<cross-domain-
domain-access>
<policy>
<allow-
<allow-from http-
http-request-
request-headers="*">
<domain uri="*"/>
</allow-
</allow-from>
<grant-
<grant-to>
<resource path="/" include-
include-subpaths="true"/>
</grant-
</grant-to>
</policy>
</cross-
</cross-domain-
domain-access>
</access-
</access-policy>
More Restrictive Policy
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from
http-request-headers=“SOAPAction, Content-Type">
<domain uri="http://johnpapa.net"/>
</allow-from>
<grant-to>
<resource path="/MyAwesomeServices/"
include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
Amazon Web Services (AWS)
● Can search and interact with Amazon
– Find books, for example
● Register to get a key
● Use REST services via their API
● Consume XML response
– LINQ to XML
ItemSearch Request
Parameters
Amazon Associates Web
Service (AWS) Request
Data Services with Silverlight 2
● Due out in Dec 2008
● O’Reilly Publishing
● Lots more on:
– Binding Tips
– Cross Domain
– REST, WCF, POX
– ADO.NET Data Services

● http://johnpapa.net
● john@johnpapa.net

Anda mungkin juga menyukai