1 / 44

Microsoft Silverlight, WCF RIA Services and Your Business Objects

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

claus
Download Presentation

Microsoft Silverlight, WCF RIA Services and Your Business Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Deborah Kurata is... • Consultant and President of:InStep Technologies, Inc. • Author of: • “Doing Objects” series • Best Kept Secrets in .NET • Doing Web Development • Software designer/developer • Microsoft MVP • DeborahK@insteptech.com • http://msmvps.com/blogs/DeborahK/

  3. InStep Technologies is... • Consulting • Software architecture and design • Custom software development of Windows, Web and Silverlight applications • Mentoring services to get you started and keep you going • Web site: www.insteptech.com

  4. You Are... • Using WCF RIA Services? • Already using it in an application • With Entity Framework • With your own business objects • Just getting started? • Evaluating it?

  5. This Talk Is… • Overview of WCF RIA Services • Accessing your Business Objects from your Silverlight application • Drag and drop approach • Refactored to MVVM • Tips and tricks for getting the most from WCF RIA and your business objects with Silverlight • Entity relationships • Validation

  6. Silverlight Line of Business Application Deborah Kurata Consultant www.insteptech.com demo

  7. Web Site Silverlight

  8. Web Site Accessing Data

  9. Web Site WCF WCF Service

  10. Web Site WCF RIA Domain Service

  11. Web Site WCF RIA + BOs Business Objects Domain Service BOs

  12. WCF RIA Service • Provides the tools to • Retrieve data using methods in your POCO classes • Save data using methods in your POCO classes • Call non-entity methods in your POCO classes • Access POCO class properties via generated client code • Does NOT: • Leverage any code in the property setters of your POCO classes • Requires attributes instead • Perform any initialization from your POCO classes • Does not execute the constructor until submit

  13. WCF RIA vs WCF • Automatic configuration of the underlying WCF Service • Auto-generation of client-side DTO classes • Ready for binding to the UI • Propagation of attributes (and code) from BL to client • Shared validation • DataContext for managing state and change tracking • Query, Update, Insert, and Delete operations • Plus the WCF-style Invoke • Automatic enforcement of validation rules

  14. WCF RIA Services in Context • Silverlight adds an ASP.NET application • The ASP.NET application is used to communicate between your Silverlight application and your business objects • Leverages the known ASP.NET server architecture • Generated code in the Silverlight client uses WCF to call methods in the Domain Service

  15. Installing WCF RIA Services • Silverlight 4 • Visual Studio 2010 SP1 comes with Silverlight 4 • Visual Studio 2010 comes with Silverlight 3 • WCF RIA Services V1.0 SP 1 • Included with Visual Studio 2010 SP1 • Silverlight Toolkit • Provides controls, themes, and more • http://silverlight.codeplex.com

  16. Building a LOB Application • Build the business objects (POCOs) • Including unit tests • Add a Silverlight Project • Enable WCF RIA Services • Creates the ASP.NET project • Code the ASP.NET application • Set a reference from the ASP.NET application to your business object component • Domain Service classes • Build the Silverlight UI (Views) and classes (ViewModels)

  17. Building a LOB Application using Drag and Drop Deborah Kurata Consultant www.insteptech.com demo

  18. Building the Business Objects • Build your business objects in a class library component • VB or C# • Decorate your classes for WCF RIA Services • Set a Reference • System.ComponentModel.DataAnnotations

  19. Key Attribute • Every entity accessible to Silverlight must have a key defined • Must be a public property • Normally the same as your entity’s database key field [KeyAttribute()] public intCustomerId { get; internal set; }

  20. Other Important Attributes • Display • Defines how the property is displayed when using drag and drop • Validation • Required • Range • Regular Expression • StringLength • Custom [Required(ErrorMessage="A Last Name must be entered.")] public string LastName

  21. Tips: • Metadata class • Exclude attribute • System.ServiceModel.DomainServices.Server • Shared files • Properties defined with an Enum • Linked resource files <ItemGroup> <EmbeddedResource Include="..\ACM.BL\Resources\ValidationErrorResources.resx"> <Link>Resources\ValidationErrorResources.resx</Link> <LogicalName>ACM.BL.Resources.ValidationErrorResources.resources</LogicalName> </EmbeddedResource> </ItemGroup>

  22. Adding a Silverlight Application • Enable WCF RIA Services • Adds two projects • Silverlight • Web

  23. Building the Silverlight Application • Drag and drop • Data Sources Window populated with domain context info • Uses a DomainDataSource • Code behind • Call domain context methods to populate the data • Bind to the result • MVVM • Call domain context methods to populate the data • Bind to the result

  24. Tips • View generated code • Click Show All Files • Generated_Code folder • Exposes 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

  25. Tips • Partial classes • Extend the business object • Partial methods • Perform validation • Work with calculated properties • Design-time data

  26. Building the Domain Service Classes • Built in the ASP.NET project • Set a reference to your business object component • Build one Domain Service class for each primary business entity • Provides the “link” between your business objects and Silverlight

  27. Add Wrapper Methods (Query) • Must be a method, not a property using ACM.BL; ... [EnableClientAccess()] public class CustomerDomainService : DomainService { public IEnumerable<Customer> GetCustomers() { return Customers.Retrieve(); } }

  28. DomainService Methods • Available Methods: • Query (Get, Fetch, Query, Retrieve, Select) • Update (Update, Change, Modify) • Insert (Insert, Add, Create) • Delete (Delete, Remove) • Invoke • Named Update • Matched via Convention (name and signature) or Attribute • Not overloadable • Wraps the call to the appropriate BO method

  29. Generated Classes • Entity • Generated class based on the domain entity • Entity can be EF, Linq to SQL, or POCO • Used to pass data through WCF • Domain Context • Generated class that makes the WCF service calls • Makes queries and tracks entity state public sealed partial class Customer : Entity {} public sealed partial class CustomerDomainContext : DomainContext {}

  30. Domain Service • Source Entity • Validation Attributes • Metadata Classes • Shared Code • Resource Files • Generated Entity (DTO) • Generated Domain Context

  31. Relationships • Complex Types • Non-entity/No KeyAttribute • Example: Phone, Address • Association • Parent/Independent child • Example: Customer/Invoice • Composition • Parent/Dependent child • Example: Invoice/Invoice Item [Association("Customer_Invoice", "CustomerId", "CustomerId")] [Include] public List<Invoice> InvoiceList [Association("Invoice_InvoiceLineItem", "InvoiceId", "InvoiceId")] [Composition] [Include] public List<InvoiceItem> InvoiceItems

  32. Validation: Simple Single Field • Add attribute to the server-side business object property • Example: Last Name • Automatically copied to the Silverlight client • Validated: • When leaving the field • Before submitting to the server

  33. 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 class • Add a shared file to contain the custom logic • Automatically copied to the Silverlight client • Validated: • When leaving the field • If the attribute is associated with a property • Before submitting to the server

  34. Validation: Silverlight Client • Model • Implement 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 submit • Must also call on Insert/Update operations

  35. Validation Tips • Use the ValidationResult to define validation errors • Issue: Attribute based validation on a ComplexObject • Only 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"}));

  36. Silverlight, RIA Services & POCO • You can decorate your Plain Old CLR objects (POCOs) for use by Silverlight • Accessing your POCOs requires a set of Domain Service classes in the ASP.NET application • Tips and Tricks • Lots of options for validation

  37. Related Content • TLC – DEV Product Demo Station: Wednesday 12:30 – 3:30 • MID311: Windows Communication Foundation RIA – Ready for Business • DEV209: From Zero to Silverlight in 75 Minutes • DEV337: Moving Your App and Skills from Windows Forms to Microsoft Silverlight (and WPF) • WPH306: Building Windows Phone Applications with Microsoft Silverlight and XNA • WPH312: What’s New for Windows Phone Development with Microsoft Silverlight? • DEV389-HOL: Using WCF RIA Services • DEV390HOL: Using the MVVM Pattern in Microsoft Silverlight Applications

  38. 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/

  39. Resources • Connect. Share. Discuss. http://northamerica.msteched.com Learning • Sessions On-Demand & Community • Microsoft Certification & Training Resources www.microsoft.com/teched www.microsoft.com/learning • Resources for IT Professionals • Resources for Developers • http://microsoft.com/technet • http://microsoft.com/msdn

  40. Complete an evaluation on CommNet and enter to win!

More Related