Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using CSLA .NET for Silverlight to Build Line-of-Business Applications : Page 3

CSLA.NET for Silverlight provides Silverlight developers with a multitude of features from the full version of CSLA, including data binding, validation and business rules, authorization, abstract data persistence, consistent coding, and business logic encapsulation.


advertisement

User Interface Support

CSLA has robust support for build rich user interfaces in Silverlight using CSLA's data-binding capabilities and custom Silverlight controls and classes.

Data Binding

Data binding in Silverlight is almost syntactically identical to WPF. For example, to bind a TextBox to a company name, you bind its Text property to an underlying data source, while specifying the path to a target property as shown below:

<Grid x:Name="LayoutRoot" DataContext="{StaticResource CompanyData}"> <Grid.RowDefinitions> <RowDefinition Height="35"/> <RowDefinition Height="35"/> <RowDefinition Height="35"/> <RowDefinition Height="35"/> <RowDefinition Height="*"/> <RowDefinition Height="35"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="150"/> <ColumnDefinition Width="200"/> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <TextBlock Text="Company Name:" TextAlignment="Right" HorizontalAlignment="Right" Grid.Column="0" Grid.Row="0" Margin="6,6,6,6"/> <TextBox x:Name="CompanyNameTextbox" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" Margin="6,6,6,6" Text="{Binding Path=Data.CompanyName, Mode=TwoWay}" IsReadOnly="True"/> <csla:PropertyStatus RelativeTargetName="CompanyNameTextbox" Property="CompanyName" Source="{Binding Path=Data}" Grid.Column="2" Grid.Row="0" HorizontalAlignment="Left"/>

The preceding code specifies only a path to the CompanyName property because the DataContext is set on the outer control (the Grid). It specifies the binding mode as TwoWay so as to propagate user changes to the underlying object.

The CslaDataProvider Class

As you probably noticed, the preceding code binds all the controls to a static resource called CompanyData. More specifically, it binds the grid to that resource, and then the controls specify only the property name to which they are bound. Here's the CompanyData resource definition:

<csla:CslaDataProvider x:Key="CompanyData" ManageObjectLifetime="True" IsInitialLoadEnabled="False" ObjectType="Company, Library ..." PropertyChanged= "CslaDataProvider_PropertyChanged" FetchFactoryMethod="GetCompany" CreateFactoryMethod="CreateCompany" DataChanged= "CslaDataProvider_DataChanged" />

CSLA .NET for Silverlight provides many features, such as data binding, authorization business, and validation rules, flexible data access, and N-level undo
CslaDataProvider is a CSLA .NET for Silverlight framework control that serves as a UI wrapper for business objects, enabling simplified syntax in XAML and greatly reducing the need for code-behind to perform functions on this business object. This control can automatically load the data into the object (although in the example IsInitialLoadEnabled is set to false). You can also specify the object you would like to use. You have to do this using an assembly-qualified class name due to lack of support for x:Type markup extensions in Silverlight. You can also specify what method to call when creating a new object (CreateFactoryMethod) or filling the object with data (FetchFactoryMethod). You can also subscribe to events that the object raises. The DataProvider knows how to save changes or cancel user changes via Save and Cancel methods. Finally, it can work in conjunction with the InvokeMethod object to further reduce the need for code-behind, as you'll see next.

The InvokeMethod Class

The InvokeMethod class is new in CSLA .NET for Silverlight. Its purpose is to simulate commanding, which is missing in Silverlight. You can attach InvokeMethod to a control, have it listen for a specific event, and invoke a specified method on another object in response to the event. Here's an example:



<Button x:Name="SaveButton" Content="Save" csla:InvokeMethod.MethodName= "Save" csla:InvokeMethod.Resource= "{StaticResource CompanyData}" csla:InvokeMethod.TriggerEvent= "Click" IsEnabled="{Binding Source= {StaticResource CompanyData}, Path=CanSave, Mode=OneWay}" />

The preceding example attaches an InvokeMethod object to the Save button. When a user clicks this button, the InvokeMethod invokes the Save method on CompanyData (the CslaDataProvider object). You can use InvokeMethod in other situations as well, invoking methods on arbitrary objects at run time. However, InvokeMethod integrates particularly well with CslaDataProvider, enabling/disabling controls based on the state of the data source. In the preceding example, SaveButton gets enabled only after a user has changed the data. Additionally, the IsEnabled property is bound to the CslaDataProvider's CanSave property. As a result, the button is enabled only when the object is valid, and has pending changes.

The Property Status Control

The PropertyStatus control lets you display visual cues related to a particular property of your business object. Specifically, it can:

  • Display tool tips with messages for all broken validation rules.
  • Display a busy animation for any outstanding asynchronous operations.
  • Disable, or make read-only a control that is also bound to the same property.
Simply declare this control in your XAML like this:

<csla:PropertyStatus RelativeTargetName="CompanyNameTextbox" Property="CompanyName" Source="{Binding Path=Data}" />

The Object Status Control

This control's purpose is to let a designer create storyboards based on a business object's status. While controls such as MethodInvoke or the CslaDataSource give you ways to easily enable/disable controls through binding, the ObjectStatus control supports a broader range of interactivity by using the Visual State Manager (covered in more detail in a future article).

You've seen the basics of using CSLA .NET for Silverlight to develop business applications. To reiterate, the primary goal is to allow developers to focus on their business problem, not the complex technologies behind the scenes. Therefore, CSLA .NET for Silverlight provides features such as data binding, authorization business, and validation rules, flexible data access, and N-level undo that help developers create robust, feature-rich Silverlight applications.

CSLA can also neatly encapsulate business logic inside business objects, allowing them to be reused in other .NET applications, such as Windows Forms, WPF, or ASP.NET—with few or no code changes. The reverse is also true, because you can adapt existing CSLA .NET-based applications for Silverlight with minimal code changes. Follow these links to download all the source code used in the article and get more information on CSLA.NET and CSLA.NET for Silverlight.



Rockford Lhotka is the author of several books, including the Expert Visual Basic .NET and C# Business Objects books. He is a Microsoft Software Legend, Regional Director, MVP, and INETA speaker. He is a columnist for MSDN Online and contributing author for Visual Studio Magazine, and he regularly presents at major conferences around the world, including Microsoft PDC, Tech Ed, VS Live!, and VS Connections. Rockford is the Principal Technology Evangelist for Magenic Technologies, a company focused on delivering business value through applied technology and one of the nation's premiere Microsoft Gold Certified Partners. For more information go to www.lhotka.net.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap