MVC Framework for .NET

I have just finished watching Scott Guthrie’s demonstration on a prototype of a Model View Controller framework for ASP.NET which looks totally awesome. This framework will allow developers to clearly seperate the presentation layer and business logic layer by using view controllers to manage what data is rendered back to the view. It also has a routing engine which allows you to map uri’s to controller’s and methods (with argument passing) which means that ASP.NET applications will now be extremely easy to unit test.

Scott Hanselman has the video’s on his blog here which require Silverlight to run. Alternatively, check out the comments section (third comment) for direct downloads.

Extension Methods

I have been looking closely at the new language features available in VS 2008 Beta 2 and VB 9 (click here to download Visual Studio 2008 Beta 2) and while LINQ is undoubtebly the most awesome feature, I thought that I would blog about some of the smaller features that have been introduced either as a direct result of the LINQ implementation or just because they are nice to have.

One of the features that first grabbed my attention is Extension methods. In a nutshell, extension methods allow you to attach a method onto any .NET type and utilise that method as if it was part of the type out of the box. If you consider the String type, there are many methods attached to the String type that allow you to retrieve it’s length, convert the string to upper or lower case, determine if it contains a specific value and so on. However, as with most things in life, there is always something missing. For example, it would be nice if we could proper case the string or determine if the string contained a valid value such as an email address. In the past, this type of functionality would be left to a helper class that we as developers would create ourselves.

However, with the introduction of extension methods, we can now turn our helper class methods into extension methods of the necessary type and use those methods in the same way that we would any method of the native .NET type.

To create an extension method in VB 9 we must use a Module as all extension methods must be shared, also the method must be decorated with the <Extension() > attribute. So taking the two examples above (proper casing a string and determining if a string is a valid email address format) we might create a module called “StringExtensions” as follows:



Option Strict On

Imports
System.Runtime.CompilerServices

Namespace
MyExtensions

    Module StringExtensions

        <Extension()> _
        Public Function ToProperCase(ByVal s As String) As String

            Dim stringValue() As String = s.Split(” “c)
            Dim returnValue As New System.Text.StringBuilder

            For i As Int32 = 0 To stringValue.Length – 1
                If stringValue(i).Length > 1 Then
                    returnValue.Append( String.Format(“{0}{1} “, _
                        stringValue(i).Substring(0, 1).ToUpper, _
                        stringValue(i).Substring(1, stringValue(i).Length – 1).ToLower))
                End If
            Next

            Return returnValue.ToString.TrimEnd

        End Function

        <Extension()> _
        Public Function IsValidEmailAddress(ByVal s As String) As Boolean

            Dim regEx As New System.Text.RegularExpressions.Regex( _
                “^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$”)
            Return regEx.IsMatch(s)

        End Function

    End Module

End
Namespace


Note that in the two method examples above, the first argument (and only argument in these examples) is of type string, this is because the first argument of any extension method must be of the type that you are extending.

Now that we have this extension method module, all that we need to do in order to utilise it is import the necessary namespace into our declaring code and utilise the new methods:



Option Strict On

Imports
MyExtensions.StringExtensions

Public
Class extensionMethodsForm

    Private Sub properCaseButton_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles properCaseButton.Click

        Dim properCaseString As String = “hello world!”
        MessageBox.Show(properCaseString.ToProperCase)

    End Sub

    Private Sub isValidEmailButton_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles isValidEmailButton.Click

        Dim mailAddress As String = “joe.bloggs@somedomain.com”
        MessageBox.Show(mailAddress.IsValidEmailAddress.ToString)

    End Sub

End
Class


Note also that these extension methods appear in Intellisense just as any other method of a type would, which makes this easier for developers of a team to discover new type methods:


Intellisense showing extension method


Another area where I think extension methods will prove extremely useful is when wishing to extend a custom class without creating a new version or inheriting from an existing version in order to add extra functionality.

Just another Microsoft MVPs site