Microsoft Silverlight, WCF RIA Services and Your Business Objects Daron Yondem Chief Architect | DEVELOAD Software Microsof Regional Director Silverlight MVP INETA MEA President
Daron Yöndem is • Silverlight MVP • Microsoft Regional Director for MEA • INETA MEA President • Book Author about HTML5, ASP.NET AJAX, Silverlight • Architect at DEVELOAD Software • Blog at http://daron.yondem.com
DEVELOAD is • Consulting – Software architecture and design – Custom software development of Windows, Web Windows Phone, Azure and Silverlight applications – Mentoring services to get you started and keep you going • Web site: www.deveload.com
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?
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
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
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
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
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)
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
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 int CustomerId { get; internal set; }
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
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
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
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
Add Wrapper Methods (Query) • Must be a method, not a property using ACM.BL; ... [EnableClientAccess()] public class CustomerDomainService : DomainService { public IEnumerable GetCustomers() { return Customers.Retrieve(); } }
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 {}
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
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
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
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"}));
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