Microsoft Silverlight, WCF RIA Services and Your Business Objects

44
Microsoft Silverlight, WCF RIA Services and Your Business Objects Deborah Kurata Consultant InStep Technologies, Inc www.insteptech.com DEV210

description

DEV210. Microsoft Silverlight, WCF RIA Services and Your Business Objects. Deborah Kurata Consultant InStep Technologies, Inc www.insteptech.com. Deborah Kurata is. Consultant and President of: InStep Technologies, Inc. Author of: “Doing Objects” series Best Kept Secrets in .NET - PowerPoint PPT Presentation

Transcript of Microsoft Silverlight, WCF RIA Services and Your Business Objects

Page 1: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Microsoft Silverlight, WCF RIA Services and Your Business ObjectsDeborah KurataConsultantInStep Technologies, Incwww.insteptech.com

DEV210

Page 2: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Deborah Kurata is...

Consultant and President of:InStep Technologies, Inc.Author of:

“Doing Objects” seriesBest Kept Secrets in .NETDoing Web Development

Software designer/developerMicrosoft [email protected]://msmvps.com/blogs/DeborahK/

Page 3: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

InStep Technologies is...

ConsultingSoftware architecture and designCustom software development of Windows, Web and Silverlight applicationsMentoring services to get you started and keep you going

Web site: www.insteptech.com

Page 4: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

You Are...

Using WCF RIA Services?Already using it in an application

With Entity FrameworkWith your own business objects

Just getting started?Evaluating it?

Page 5: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

This Talk Is…

Overview of WCF RIA ServicesAccessing your Business Objects from your Silverlight application

Drag and drop approachRefactored to MVVM

Tips and tricks for getting the most from WCF RIA and your business objects with Silverlight

Entity relationshipsValidation

Page 6: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

demo

Silverlight Line of Business ApplicationDeborah KurataConsultantwww.insteptech.com

Page 7: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Web SiteSilverlight

Page 8: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Accessing Data Web Site

Page 9: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Web Site

WCF Service

WCF

Page 10: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Web Site

Domain Service

WCF RIA

Page 11: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Web Site

Business Objects

WCF RIA + BOs Domain Service

BOs

Page 12: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

WCF RIA Service

Provides the tools toRetrieve data using methods in your POCO classesSave data using methods in your POCO classesCall non-entity methods in your POCO classesAccess POCO class properties via generated client code

Does NOT:Leverage any code in the property setters of your POCO classes

Requires attributes insteadPerform any initialization from your POCO classes

Does not execute the constructor until submit

Page 13: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

WCF RIA vs WCF

Automatic configuration of the underlying WCF ServiceAuto-generation of client-side DTO classes

Ready for binding to the UIPropagation of attributes (and code) from BL to client

Shared validationDataContext for managing state and change trackingQuery, Update, Insert, and Delete operations

Plus the WCF-style InvokeAutomatic enforcement of validation rules

Page 14: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

WCF RIA Services in Context

Silverlight adds an ASP.NET applicationThe ASP.NET application is used to communicate between your Silverlight application and your business objects

Leverages the known ASP.NET server architectureGenerated code in the Silverlight client uses WCF to call methods in the Domain Service

Page 15: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects
Page 16: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Installing WCF RIA Services

Silverlight 4Visual Studio 2010 SP1 comes with Silverlight 4Visual Studio 2010 comes with Silverlight 3

WCF RIA Services V1.0 SP 1Included with Visual Studio 2010 SP1

Silverlight ToolkitProvides controls, themes, and more

http://silverlight.codeplex.com

Page 17: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Building a LOB Application

Build the business objects (POCOs)Including unit tests

Add a Silverlight ProjectEnable WCF RIA ServicesCreates the ASP.NET project

Code the ASP.NET applicationSet a reference from the ASP.NET application to your business object componentDomain Service classes

Build the Silverlight UI (Views) and classes (ViewModels)

Page 18: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

demo

Building a LOB Application using Drag and DropDeborah KurataConsultantwww.insteptech.com

Page 19: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Building the Business Objects

Build your business objects in a class library componentVB or C#

Decorate your classes for WCF RIA ServicesSet a Reference

System.ComponentModel.DataAnnotations

Page 20: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Key Attribute

Every entity accessible to Silverlight must have a key definedMust be a public propertyNormally the same as your entity’s database key field[KeyAttribute()]public int CustomerId { get; internal set; }

Page 21: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Other Important Attributes

DisplayDefines how the property is displayed when using drag and drop

ValidationRequiredRangeRegular ExpressionStringLengthCustom

[Required(ErrorMessage="A Last Name must be entered.")]public string LastName

Page 22: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Tips:

Metadata classExclude attribute

System.ServiceModel.DomainServices.ServerShared filesProperties defined with an EnumLinked resource files

<ItemGroup> <EmbeddedResource Include="..\ACM.BL\Resources\ValidationErrorResources.resx"> <Link>Resources\ValidationErrorResources.resx</Link> <LogicalName>ACM.BL.Resources.ValidationErrorResources.resources</LogicalName> </EmbeddedResource></ItemGroup>

Page 23: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Adding a Silverlight Application

Enable WCF RIA ServicesAdds two projects

SilverlightWeb

Page 24: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Building the Silverlight Application

Drag and dropData Sources Window populated with domain context infoUses a DomainDataSource

Code behindCall domain context methods to populate the dataBind to the result

MVVMCall domain context methods to populate the dataBind to the result

Page 25: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Tips

View generated codeClick Show All FilesGenerated_Code folderExposes the application services from the ASP.NET application and the types from the BOs to Silverlight Not much in here until you create Domain Service classes

Page 26: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Tips

Partial classesExtend the business object

Partial methodsPerform validationWork with calculated properties

Design-time data

Page 27: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Building the Domain Service Classes

Built in the ASP.NET projectSet a reference to your business object componentBuild one Domain Service class for each primary business entityProvides the “link” between your business objects and Silverlight

Page 28: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Add Wrapper Methods (Query)

Must be a method, not a propertyusing ACM.BL;...

[EnableClientAccess()]public class CustomerDomainService : DomainService{ public IEnumerable<Customer> GetCustomers() { return Customers.Retrieve(); }}

Page 29: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

DomainService Methods

Available Methods:Query (Get, Fetch, Query, Retrieve, Select)Update (Update, Change, Modify)Insert (Insert, Add, Create)Delete (Delete, Remove)InvokeNamed Update

Matched via Convention (name and signature) or AttributeNot overloadableWraps the call to the appropriate BO method

Page 30: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Generated Classes

EntityGenerated class based on the domain entityEntity can be EF, Linq to SQL, or POCOUsed to pass data through WCF

Domain ContextGenerated class that makes the WCF service callsMakes queries and tracks entity state

public sealed partial class Customer : Entity{}

public sealed partial class CustomerDomainContext : DomainContext{}

Page 31: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

• Domain Service • Source Entity• Validation Attributes• Metadata Classes• Shared Code• Resource Files

• Generated Entity (DTO)• Generated Domain Context

Page 32: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Relationships

Complex TypesNon-entity/No KeyAttributeExample: Phone, Address

AssociationParent/Independent childExample: Customer/Invoice

CompositionParent/Dependent childExample: Invoice/Invoice Item

[Association("Customer_Invoice", "CustomerId", "CustomerId")][Include]public List<Invoice> InvoiceList[Association("Invoice_InvoiceLineIt

em", "InvoiceId", "InvoiceId")][Composition][Include]public List<InvoiceItem> InvoiceItems

Page 33: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Validation: Simple Single Field

Add attribute to the server-side business object propertyExample: Last Name

Automatically copied to the Silverlight clientValidated:

When leaving the fieldBefore submitting to the server

Page 34: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Validation: Custom

Add Custom attribute to one or more server-side business object properties

Example: Phone Number and Description

OR add Custom attribute to the classAdd a shared file to contain the custom logicAutomatically copied to the Silverlight clientValidated:

When leaving the fieldIf the attribute is associated with a property

Before submitting to the server

Page 35: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Validation: Silverlight Client

ModelImplement partial method in partial class

Single or multiple field validation (Email Address)

ViewModel (asynchronous validation)Implement on property changed

Single field, multiple field, or invoked server method (IsNameDuplicate)

Validated:Client Only

Not automatically checked on submitMust also call on Insert/Update operations

Page 36: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Validation Tips

Use the ValidationResult to define validation errors

Issue: Attribute based validation on a ComplexObjectOnly occurs automatically on save, not on leaving the field

if (!string.IsNullOrWhiteSpace(value) && !value.Contains("@")) this.ValidationErrors.Add (new ValidationResult("Email address must include an '@’.", new string[] {"EmailAddress"}));

Page 37: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Silverlight, RIA Services & POCO

You can decorate your Plain Old CLR objects (POCOs) for use by SilverlightAccessing your POCOs requires a set of Domain Service classes in the ASP.NET applicationTips and TricksLots of options for validation

Page 38: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Related Content

MID311: Windows Communication Foundation RIA – Ready for BusinessDEV209: From Zero to Silverlight in 75 MinutesDEV337: Moving Your App and Skills from Windows Forms to Microsoft Silverlight (and WPF)WPH306: Building Windows Phone Applications with Microsoft Silverlight and XNAWPH312: What’s New for Windows Phone Development with Microsoft Silverlight?

DEV389-HOL: Using WCF RIA ServicesDEV390HOL: Using the MVVM Pattern in Microsoft Silverlight Applications

TLC – DEV Product Demo Station: Wednesday 12:30 – 3:30

Page 39: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Web Track Resources

http://www.asp.net/http://www.silverlight.net/http://www.microsoft.com/web/gallery/http://www.iis.net/http://weblogs.asp.net/Scottgu/http://www.hanselman.com/blog/

Page 40: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Resources

www.microsoft.com/teched

Sessions On-Demand & Community Microsoft Certification & Training Resources

Resources for IT Professionals Resources for Developers

www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn

Learning

http://northamerica.msteched.com

Connect. Share. Discuss.

Page 41: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Complete an evaluation on CommNet and enter to win!

Page 42: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects

Scan the Tag to evaluate this session now on myTech•Ed Mobile

Page 43: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects
Page 44: Microsoft Silverlight, WCF  RIA  Services and Your  Business  Objects