Statistics
Visits: 105083
Page Visits: 139172
Active visitors (monthly): 5364
Feed Count: 45
Most visited page: Adding value as a technical analyst
Category : Silverlight

Silverlight in Windows 8

I stumbled upon a link that talks about the end of Flash and Silverlight: http://debuggerdotbreak.wordpress.com/2011/11/11/the-bell-tolls-for-flash-and-silverlight-isnt-far-behind/

I do not think that Silverlight is dead yet. There are a lot of developers with WPF / XAML skills who are comfortable building Silverlight apps. It is preferrable to use Silverlight for line-of-business (LOB) applications within the Windows desktop environment. But, there are some conflicting messages about Windows 8.

The metro UI in Windows 8 is primarily targetted for the tablet users. Windows 8 cold boots in 4 seconds. Metro itself seems to be really good. But, what is bad for Silverlight developers is that Metro is based upon HTML 5 and not Silverlight. The Metro UI in Windows 8 has a browser that does not support any plugins including Silverlight. If you want to experience Silverlight apps in Windows 8, you will have to click on the desktop tile, and use the browser from there. So, using Silverlight for consumer applications is not a good idea. But for business apps, it is still a very good choice.

The other thing that is somewhat confusing is that Windows 8 does not run apps built for Windows phone. If you buy a mobile device and a tablet both running Windows, the chances are that you may have to buy two versions of the same app - one for the tablet and another for the mobile. This can lead to a slightly different user experience. Also, from a developers perspective, developers have to write different implementations for the mobile and the tablet.

This is early days as Windows 8 is available for developers to preview. I am sure a lot will change before Windows 8 goes live. Silverlight applications will run from the browser on the desktop tile. Also, Silverlight Out of browser applications will run as a tile from the Metro UI. So, Silverlight will live for sometime. I am looking forward for the next release of Silverlight - Silverlight 5 for more directions on its future.

 

Permalink | No Comments | Leave your comment
 

Data validations in Silverlight RIA applications

After a lot of reading, I have finally sort of figured out how to do data validations in Silverlight applications that use WCF RIA services.

Data validations should be performed both on the Silverlight application (that runs on the browser) as well as WCF RIA Service layer that runs on the web server. As of now, RIA applications support DataAnnotations as the main way of validating data. When a RIA service is created (say. CategoryService), a CategoryService.metadata.cs is also created. Within this file, a partial Entity class (Category) is defined. This is the place where you put all your data annotations.

When a Silverlight app is built, the Silverlight side entity classes are generated. This includes the data annotations defined in the server. Also, the class is derived from Entity abstract class which implements INotifyDataErrorInfo interface.

CustomValidation attributes are used whenever a validation requires access to the database. Here is a golden blogpost which describes exactly how it works: http://jeffhandley.com/archive/2010/05/26/asyncvalidation-again.aspx

CustomValidation when triggered from the Silverlight side is asynchronous. If you look at the above post, the service returns a Sucess. And then, if it finds any error, it asynchronously updates the ValidationErrors for the Entity. If the user does a Submit or Save before the async operation completes, the same validation should be performed at the server using the same CustomValidation attribute. Again, the above post is just amazingly cool as it explains how to do it.

There are other things you have to handle. The SubmitChanges of the DomainContext is asynchronous. So, any failures or success should be handled asynchronously in the callback method.

Validations using Data Annotations (in Silverlight or service) raise exceptions. In the Silverlight side, the framework knows how to invalidate the control state. When validations fail in the server, you will have to add the error to the ValidationErrors collection of the Entity class. This will invalidate the control.

While using data annotations, you will have to somehow let the ViewModel or the Entity know that there are errors. This is not automatically done.

Final tip: Do not try to roll your own custom framework using IDataErrorInfo or INotifyDataErrorInfo. It costs a lot of money. And you will have to do it at the service layer also.

Permalink | No Comments | Leave your comment
 

More rant on data validations for Silverlight applications using RIA

Models in Silverlight can be validated in three ways (atleast):

  1. Data Annotations
  2. IDataErrorInfo
  3. INotifyDataErrorInfo

Data Annotations are attributes attached to the model properties. Each of the attributes have a IsValid() method which fire an exception if validation fails. Because the validation logic is based on exceptions, it does not offer good performance - especially, if the model contains a lot of properties, all failing exceptions.

 IDataErrorInfo attempts to use an indexer to retrieve the validation error for a model property. IDataErrorInfo is simplistic - Only one error per property, and the error message can only be a simple string.

INotifyDataErrorInfo is Silverlight 4 way of handling validations. It can handle multiple errors per property and allows complex classes to be returned as error objects. It also notifies the UI that there is an error on the property. In this way, checks can be done async on the server, eg duplicate name checks.

How does RIA handle validations? When a DomainService is created, a DomainService.metadata.cs file is created. This file contains a partial <Entity> class which defines the metadata for the Entity. (Entity framework emits the other partial <Entity> class with data mappings and Key / Constraint information). This metadata class is where we can define the data annotations.

When we do a build of the Silverlight project, the metadata information from the RIA (with validation attributes) is combined with the entity class from the Entity framework to create a new <Entity> class within the Silverlight application. What is troublesome is that the <Entity> class derives from an abstract Entity base class which implements a lot of interfaces including INotifyDataErrorInfo. In my opinion, the auto-generated code is messy from a design view point. The auto-generated Silverlight <Entity> class has the same type name as the <Entity> class in the server side but a completely different implementation.

In my opinion, the validations using RIA is messy. I understand that we have to do validations both on the Silverlight end as well as the RIA service end. (Or am I mistaken?) I am not sure if the different approaches using INotifyDataErrorInfo and IDataErrorInfo are built to take care of this. In any case, with RIA, Data annotations seems to be the way to go.

But again, I am still exploring.

 

Permalink | No Comments | Leave your comment
 

Silverlight - Using MVVM pattern with WCF RIA services

I have picked up enough XAML, and Silverlight to write some good code for Silverlight applications. I prefer to use the MVVM pattern in the presentation layer, WCF RIA services for the service layer, and Entity Framework Code First for the data access layer. Getting all of this to work well together is some sort of nightmare for beginners. Sadly, there are no reference implementations either.

Entity framework and RIA uses EntitySet<T> as the collection class. I am more comfortable with ObservableCollection<T> or PagedCollectionView<T>. Even, the MVVM reference implementation - PRISM uses the ObservableCollection<T>.

For data validations, I prefer the IDataErrorInfo interface. This is more preferrable to Data Annotations. With RIA Services, the Entity class is auto-generated. So, I am not sure where I can put in my validations.

Another key problem with using MVVM model for beginners is the navigation. The Silverlight Business Application template solves this problem with the NavigationService class. If you are not using the Silverlight navigation framework and prefer to use the best practices from the PRISM framework, then navigation could be a nightmare. Try putting a simple "Are you sure to delete?" confirmation messagebox using the MVVM pattern.

Overall, learning MVVM, RIA, EFCF and putting them to work together is not the most pleasant experience as a developer. I hope to write a mini-reference implementation soon (A simple CRUD interface in Silverlight using MVVM, RIA, and EFCF).

Permalink | 1 Comment | Leave your comment