Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Visual Studio LightSwitch: implementing and using extension methods with Visual Basic

As you know Visual Studio LightSwitch allows building business applications quickly and easily with an approach of type Data + Screens = Business Applications. This approach simplifies a lot the application development, even for developers who are new to programming, but necessarily requires some restrictions on customizing some application parts.

In reality, LightSwitch applications are 100% .NET applications running on the Silverlight 4 platform, meaning that you can reuse a lot of your existing skills about .NET and managed languages, such as extension methods. For example, imagine you have a simple entity named Contact which exposes a property called WebSite of type String, which stores the contact's web site address, if any:

Now imagine you want to implement a custom validation rule which ensures that the entered Web address is valid. Among several alternatives, such as regular expressions or Windows API, for the sake of simplicity you can use the System.Uri.IsWellFormedUriString method, which returns True in case the runtime can construct a well formed Uri from the passed string. So, in the Properties Window click Custom Validation to enable the Visual Basic code editor, where you handle the Validate event as follows:

        Private Sub WebSite_Validate(ByVal results As EntityValidationResultsBuilder)
            ' results.AddPropertyError("")
            If Me.WebSite IsNot Nothing AndAlso System.Uri.
                IsWellFormedUriString(Me.WebSite, UriKind.Absolute) = False Then
                results.AddPropertyError("The supplied Uri seems not to be valid")
            End If
        End Sub

If you are curious to see how the user interface displays validation errors, just run the application and try to enter an invalid web site (for example try with http:/ instead of http://). For now let's focus on the code. The goal is now making code more elegant and readable by adding to the String type an extension method that performs the check against the Web address. As you already know, in Visual Basic you implement extension methods via modules (or static classes in C#). In LightSwitch the developer experience is quite different from the one in the "classic" Visual Studio 2010, so you cannot add new code files except for the following two tricks:

  • enabling the code editor and defining the module (or a class if required) inside an existing code file, such as Contact.vb in the current example. After all, .NET code files can contain multiple object definitions and this happens in LightSwitch too
  • switching from the Logical View to the File View, by clicking the appropriate button in Solution Explorer; then locate the Common\User Code subfolder and there use the classic Add New Item command in order to add a new code file. Notice that in Beta 1 you can only add text files or class files, so you need to manually replace the object definition in code.

Since the first trick is the simplest, go into the Contact.vb code file (which should be still opened from when you wrote the custom validation rule), then write the following module:

    Module Extensions
        Function IsValidWebAddress(ByVal webAddress As StringAs Boolean
            Return System.Uri.IsWellFormedUriString(webAddress, UriKind.Absolute)
        End Function
    End Module

Cool! the background compiler corretcly recognizes the code and makes the new extension method available in the String type. In fact, you can now rewrite the custom validation rule as follows:

        Private Sub WebSite_Validate(ByVal results As EntityValidationResultsBuilder)
            ' results.AddPropertyError("")
            If Me.WebSite IsNot Nothing AndAlso Me.WebSite.IsValidWebAddress = False Then
                results.AddPropertyError("The supplied Uri seems not to be valid")
            End If
        End Sub

At this point the code is really more elegant and readable, moreover we had a demonstration about the fact that we can reuse our existing .NET skills in LightSwitch applications too.


Print | posted on giovedì 4 novembre 2010 13:08 | Filed Under [ Visual Basic Visual Studio LightSwitch ]


No comments posted yet.

Post Comment

Please add 3 and 1 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET