Make immutable objects mutable

In .Net the String object is an immutable object, which means that it’s basically constant and can’t be changed. You can’t change a string, you can only create new strings. The various methods that the String class have never directly change the string they operate on, instead they return a new string.

Dim myString As String = "Hello World"
Dim mySecondString As String = myString.Substring(0, 5)
Console.WriteLine(myString) 'Hello World
Console.WriteLine(mySecondString) 'Hello

Even though we developers modify strings all the time, which can make them look mutable, they aren’t. What happens is that a new string is created and the reference variable we use simply points to the new string on the heap.

Make the string mutable

OK, the title of this article isn’t really true since you can’t make a string mutable, however because of a strange behavior in the VB implementation of extension methods, you can make it appear as if it was. Note that this is only possible in VB, to the best of my knowledge, C# does not have this behavior. This little known fact is that the argument of the extension method, the first parameter, can be passed by reference. Have a look at this example code:

<Extension()> _
Public Sub MutableInsert(ByRef str As String, _
                         ByVal position As Integer, _
                         ByVal text As String)
  If position >= str.Length Then
    str = str & text
  ElseIf position <= 0 Then
    str = text & str
  Else
    str = str.Substring(0, position) & text & str.Substring(position)
  End If
End Sub

Public Sub Main()
  Dim myString As String = "Hello world"
  myString.MutableInsert(5, " cruel")
  Console.WriteLine(myString) 'Hello cruel world
End Sub

Being a .Net developer for many years, this behavior looks weird to me. The fact that something that appears as an instance method actually can change the instance itself. But in some occasions it could actually be pretty useful. Take the following code as an example:

<Extension()> _
Public Sub EnsureNotNull(ByRef str As String)
  If str Is Nothing Then
    str = String.Empty
  End If
End Sub

Public Sub ChangeString(ByVal str As String)
  str.EnsureNotNull()
  Console.WriteLine("str Is Nothing = {0}", str Is Nothing) 'False
End Sub

Public Sub Main()
  ChangeString(Nothing)
End Sub

Personally I’m not sure that this feature is a good thing. If I can change a string from Nothing (or null) to String.Empty what stops me from doing the opposite? Well, absolutely nothing (no pun intended).

<Extension()> _
Public Sub EvilMethod(ByRef str As String)
  str = Nothing
End Sub

Public Sub Main()
  Dim msg As String = "Hello world"
  msg.EvilMethod()
  Dim newMsg As String = msg.Substring(0, 5) 'throws a NullReferenceException
  Console.WriteLine(newMsg)
End Sub

There you have it, the evil programmer have struck again.

Have fun.

Go for the Generic brand…

Before .Net 2.0 the only way that you could use a generic type was to use System.Object. Although it’s been 4 years since the release of .Net 2.0 I still see developers using the System.Object way too often, which leads to unnecessary boxing/unboxing and casting. So in this article I’m going to explain how you can use Generics in your classes and methods.

What are Generics?

I entitled this article “Go for the Generic brand” because outside the world of programming the term generic means something that is not tied to a particular brand name. You may buy a bottle of shampoo without it having one of the more recognizable names printed on its label but you can still use it and expect it to clean your hair. Generics in programming works in a similar manner, you can refer to a class without it being related to a particular Type, but we can still use it in a type-safe manner.

However unlike generic brands of merchandises, that often are thought of as inferior to well known household brands, Generics in .Net are usually a good thing. I said usually a good thing because you should not look at generics as a way to replace other techniques such as method overloading, but rather as a way to replace the usage of System.Object.

A quick example

Below is a small example on what a generic class could look like.

Public Class MyGenericClass(Of T)
  Private _value As T
  Public Property Value() As T
    Get
      Return _value
    End Get
    Set(ByVal value As T)
      _value = value
    End Set
  End Property
End Class

The (Of T) part after the class name tells the compiler that this is a generic class. The T itself is merely a placeholder of a type, any type. The name T doesn’t really matter, you could just as well have typed (Of MyType) if you wanted to, however it has become a general convention to just use the capital T, unless the class expects more than one type.

In C# the same class would look like this:

public class MyGenericClass<T>
{
  private T _value;
  public T Value
  {
    get{ return _value; }
    set{ _value = value;}
  }
}

C# uses the angle brackets instead of the Of keyword as VB does, but other than that they work exactly in the same manner.

So let’s see how this class can be used. When we create a new object from the class we must provide the type we want to use.

Dim myString As New MyGenericClass(Of String)
Dim myInteger As New MyGenericClass(Of Integer)
myString.Value = "Hello World"
myInteger.Value = 123
Console.WriteLine(myString.Value)
Console.WriteLine(myInteger.Value)

Here I create two instances of the MyGenericClass one of the String type and the other of the Integer type. The Value property will then become of that particular type, so trying to do the following would create a compile error (or a runtime error if you have Option Strict set to Off) since I’m trying to assign a string value to an integer type.

myInteger.Value = "Hello"

This generic class is in no way restricted to only primitives and strings, you could use it with any type. Below I use a StringBuilder.

Dim myStringBuilder As New MyGenericClass(Of StringBuilder)
myStringBuilder.Value = New StringBuilder
myStringBuilder.Value.AppendLine("A string")
myStringBuilder.Value.AppendLine("Another string")
Console.WriteLine(myStringBuilder.Value)

Generic Methods

The whole class doesn’t have to use generics, you can use generics on a single method as well.

Public Function CreateArray(Of T)(ByVal size As Integer) As T()
  Dim array(size) As T
  Return array
End Function

Public Sub Main()
  Dim myIntArray As Integer() = CreateArray(Of Integer)(5)
  Dim myStringArray As String() = CreateArray(Of String)(5)
End Sub

This can be especially useful for extension methods, when you want to add a generic method to any type.

Type constraints

Even though the code in a generic type definition should be as type-independent as possible it is sometimes useful to add some constraints to limit the number of types that are possible. A good example is if you for example need to compare items to be able to sort them in which case they must implement the IComparable interface.

Public Class MyComparer(Of T As IComparable)
  'Insert code...
End Class

Apart from interfaces the type of constraints you can use are:

  • The type must be of a type of, or inherit from, a class.
  • The type must expose a parameterless constructor.
  • The type must be a reference type, or it must be a value type.

Let’s have a look at the first, the the type must be of, or inherit from, a specified class. Let’s say you have the following classes:

Public Class Identity
  Private _id As Integer
  Public Property ID() As Integer
    Get
      Return _id
    End Get
    Set(ByVal value As Integer)
      _id = value
    End Set
  End Property
End Class

Public Class Person
  Inherits Identity

  Private _firstName As String
  Public Property FirstName() As String
    Get
      Return _firstName
    End Get
    Set(ByVal value As String)
      _firstName = value
    End Set
  End Property

  Private _lastName As String
  Public Property LastName() As String
    Get
      Return _lastName
    End Get
    Set(ByVal value As String)
      _lastName = value
    End Set
  End Property
End Class

Public Class Company
  Inherits Identity

  Private _name As String
  Public Property Name() As String
    Get
      Return _name
    End Get
    Set(ByVal value As String)
      _name = value
    End Set
  End Property

  Private _city As String
  Public Property City() As String
    Get
      Return _city
    End Get
    Set(ByVal value As String)
      _city = value
    End Set
  End Property
  'more properties goes here...
End Class

So basically we here have an Identity class that expose an ID property and then we have a Person and a Company class that both inherit from the Identity class. Now let’s say that you want to create a collection class that only accepts objects of the Identity class or one of its descendants, it could then look something like the following.

Public Class IdentityCollection(Of T As Identity)
  Inherits List(Of T)
End Class

You can specify more than one constraint if you like, in which case you list the different constraints inside curly braces.

Public Class IdentityCollection(Of T As {Identity, IComparable})
  Inherits List(Of T)
End Class

To require an accessible parameterless constructor you would include the New keyword in the list. For a reference type include the Class keyword, and for value types you would include the Structure keyword.

Generic collections

Generics are mostly used with collections, and the .Net team have included a bunch of various generic collections for us, including list, stack, queue, and dictionary collections. They are all part of the System.Collections.Generic namespace.

In .Net 1.1 we where more or less forced to use the ArrayList collection type. However that excepted items of the System.Object type which means that casting and, if you use value types, boxing/unboxing needs to be performed. That can be very inefficient, especially in For Each loops. Using the List(Of T) we don’t have to do any of that.

Dim myList As New List(Of Person)

Conclusion

So in this article I’ve covered most of what you need to know about using generics. If you haven’t started using generics yet, I hope this article will get you started.

Have fun.

GetType and TypeOf confusion

Both VB and C# have an operator called TypeOf (or typeof in C#) but they perform two completely different things.

In VB there are also two kind of GetType() calls, the object.GetType() method which is part of the .Net framework and the VB language specific GetType() operator.

Are you confused yet? Don’t worry, in this article I will try to explain the difference between these operators and the object.GetType() method.

The TypeOf, typeof, GetType operators

The VB TypeOf operator is used together with the Is keyword and is used for checking if an object is of a particular type.

result = TypeOf x Is String

If “x” above is a string then “result” would be True otherwise it is set to False. This operator have existed in VB since long before .Net was born. The equivalent for the TypeOf operator in C# is simply called the is operator.

result = x is string;

The typeof operator in C# on the other hand returns an instance of the System.Type class containing type declarations of the type you pass to it.

Type t = typeof(string);

The VB equivalent of the C# typeof operator is the GetType operator.

Dim t As Type = GetType(String)

The reason for the different names are simply because of the fact that TypeOf was already a reserved keyword in VB and I guess the C# team didn’t have VB in their mind when they designed C#. 🙂

The difference between Object.GetType and the GetType operator

On a trivial level, the Object.GetType() method operates on an object instance while the GetType (and C#’s typeof) operator operates on a type.

Dim s As String
Dim t As Type = s.GetType()
Dim t2 As Type = GetType(String)

There are no differences between “t” and “t2” in the above code. So why do we have to have both of them? Well, you might not know what type a certain reference is made of in which case you obviously can’t use the GetType operator since that requires that you pass the type. Have a look at the following example:

Public Class MyBaseClass
End Class

Public Class MyDerivedClass
  Inherits MyBaseClass
End Class

Module Test
  Public Sub ShowType(ByVal obj As MyBaseClass)
    Dim t As Type = obj.GetType()
    Dim t2 As Type = GetType(MyBaseClass)
    Console.WriteLine(t)
    Console.WriteLine(t2)
  End Sub

  Public Sub Main()
    Dim myObject As New MyDerivedClass
    ShowType(myObject)
    Console.ReadLine()
  End Sub
End Module

In this example the ShowType method takes a MyBaseClass parameter and t2 uses the GetType operator to get the System.Type representation of MyBaseClass. So when you write out t2 to the console it will write MyBaseClass. However when you use the GetType method on the obj parameter it will write MyDerivedClass to the console. It is legal to pass a reference to MyDerivedClass to the ShowType method since it inherits from MyBaseClass so that will be a widening conversion. So inside the ShowType method we will never know if the parameter contains the type we have declared it as or if it contains any subclass of it, to find out we need to use the Object.GetType method.

Another difference between the method and the operator is when you’re dealing with value types.

Dim i As Integer
Dim t As Type = i.GetType()
Dim t2 As Type = GetType(Integer)

In order to call i.GetType() above i must first be converted to an Object which means a boxing conversion has to be done. Using the operator no boxing conversions are made which in this case makes that call faster.

I hope this removes some of the confusion about this subject, if not please feel free to leave a comment.

Have fun!

Boxing and Unboxing in .Net

In this article I will try to explain the concepts of Boxing and Unboxing. There are two types of objects within the .Net framework, value types and reference types. Value types are stored on the stack while reference types are always stored on the heap. Converting a value type into a reference type is called Boxing while converting it back from a reference type to a value type is called Unboxing.

Value Types

Value types are primitive types like System.Int32 (Integer in VB, int in C#) or System.Double. These types are mapped directly to the Framework Class Library (FCL) and are all derived from System.ValueType. Apart from the regular primitive types (integer, long, single, double, char, and so on) Enumerations and Structures also inherits from System.ValueTypes so they are also stored on the stack.

Reference Types

All classes are reference types and they are stored on the heap. So when you create a new instance of a class using the new keyword, memory are allocated on the heap for the object and the memory address is returned. Strings are a special case since they are also reference types, even though they are often treated as if they where value types, but that’s not the case. The difference is that you don’t have to create an instance of a string in the same manner as you would with another class.

Reference types are also Nullable, meaning that you can set the reference to Null (Nothing in VB).

Widening and Narrowing conversions

Before we go into the Boxing and Unboxing I would like to take a moment to explain the difference between a widening and a narrowing conversion. A widening conversion is always safe since there is never any risk of any data loss. For example converting an Integer into a Long (System.Int64) is a widening conversion. There is no integer value that doesn’t fit into a Long so you can always do that. However doing the opposite, converting a Long into an Integer is more risky since there are a chance that you will lose some data, since a Long can contain a number that is too high (or too low) to fit into an integer.

You can always use an implicit conversion when you do a widening conversion.

Dim i As Integer = 3
Dim l As Long
l = i 'Implicit conversion

However to do the opposite, a narrowing conversion, you need to do an explicit conversion.

i = CType(l, Integer) 'Explicit conversion
'or
i = CInt(l) 'Explicit conversion

If you don’t do an explicit conversion when you’re doing a narrowing conversion you will get a compilation error so your project will not compile (Note, VB can allow you to do an implicit conversion if you have Option Strict set to Off, something I strongly advice you not to have, C# however will never allow it). If the Long value does not fit into the Integer you will however still get a runtime error since the system will then throw a System.OverflowExeption. So when you do a narrowing conversion you must be sure that it can be done.

Doing Boxing/Unboxing

As mentioned earlier Boxing refers to the conversion of a value type to a reference type.

Dim i As Integer = 3
Dim o As Object
o = i 'Boxing the Integer into an object (reference type)

Above I do an implicit conversion of an integer to an object. This is possible since it’s a widening conversion so there is no risk of any data loss. However doing the opposite, converting the object to an integer is a narrowing conversion, where there is a risk that data will be lost.

i = o 'Implicitly doing a narrowing conversion (not allowed)
i = CInt(o) 'Explicitly doing a narrowing conversion (allowed)

In C# if you have boxed a particular type you must unbox it to the same type. You can not box an integer to an object and then unbox it to a long.

int i = 3;
long l;
object o;
o = i;
l = (long)o; //will throw a System.InvalidCastException

For the above to work, you must first unbox it to an integer and then cast it to a long.

int i = 3;
long l;
object o;
o = i;
l = (long)(int)o;

However the VB’s type conversion functions (CType and the more specific CInt, CLng, CDbl and so on) does allow this.

Dim i As Integer = 3
Dim l As Long
Dim o As Object
o = i
l = CLng(o) 'allowed

End words

I hope that you by now have a better understanding of Boxing and Unboxing. So let me end this article by saying that you should avoid doing this if possible. Doing type conversion is time consuming, and that is especially true when it comes to Boxing and Unboxing since data have to be transferred back and forth between the stack and the heap. Using Generics, which has existed in .Net since version 2.0, is a much better approach than having to deal with value type to reference type conversion.

I will cover Generics in a later article.

Have fun.

Top 10 signs that you are a geek

Here’s a compilation of my personal view of top 10 signs that you are a geek, in descending order.

9. You see a guy drink a pint of beer in less than 8 seconds and you are impressed by his download speed.

8. You don’t say that people are standing in a long line outside a popular night club but that they are caught in a slow serial queue. Not that you would ever find yourself in that situation since you don’t have a social life.

7. You wear a T-shirt that reads <body> on the front and </body> on the back and you don’t understand why people doesn’t find it funny.

6. You know that the answer to the riddle: “If only you and DEAD people knows HEX, how many people knows HEX?” is 57,006, and you also think that this is one of the funniest jokes you’ve ever heard. Likewise you also think that the statement “There are 10 kinds of people in the world, those that know binary and those that doesn’t” is hilariously funny.

5. You think that the correct term for a buoy is a floating point.

4. You tape down two of your child’s fingers just to teach him/her to count in octo.

3. One of your most treasured belongings is your 30 year old TI-34 calculator.

2. You don’t understand why (most (apart from yourself)) people think that using nested parenthesis when they type is bad practice.

1. You tweet hateful arguments and post several blog articles about how bad your current phone carrier is just because they wouldn’t give you a phone number containing 7331 in it.

0. All your “Top 10” lists are zero-based.

So there you have it. Have fun!

Nifty extension methods

One of my favorite features in .Net 3.5 is the ability to extend a class using extension methods. Earlier you could only extend a class either by creating a new class that inherits from the first class or you could create a partial class. Of course if you have the source code of the original class you could just write the new code directly in it and recompile it. In many cases you don’t have access to the source code of a class and if the class is also sealed (NotInheritable in VB) you would not be able to extend its behavior using the other methods either. The string class is a typical example of a class that is sealed and you normally wouldn’t be able to extend at all. That is until extension methods entered the scene.

In VB extension methods must reside inside a module and be decorated with the Extension attribute (available in the System.Runtime.CompilerServices namespace), in C# they need to be defined in a static class and use the this keyword on the first parameter (the method does not have to be decorated with the Extension attribute in C#).

Let say that you want to extend the string class with a method that allows you to do a case-insensitive comparison with another string. Many programmers use the ToUpper or ToLower methods on the strings before comparing them, but that doesn’t always give the correct result and you should avoid doing that. The reason is that these methods are not always culture aware. Take the ß (the sharp S or double S in German) character for example. It is considered to be a lower case character which in upper case should be written as two S’es. Straße (street) versus STRASSE, but calling ToUpper on the word Straße does not return STRASSE, so comparing the two spellings using the ToUpper or ToLower methods will fail. A much better approach would be to use the CaseInsensitiveComparer class (from the System.Collections namespace).

OK, so let’s look at the code for what that extension method could look like.

Imports System.Collections.Generic
Imports System.Runtime.CompilerServices

Public Module ExtensionMethods
<Extension()> _ Public Function EqualsText(ByVal str As String, ByVal equalsTo As String) As Boolean Dim sc As New CaseInsensitiveComparer Return (sc.Compare(str, equalsTo) = 0) End Function End Module

As you can see it simply looks as a small helper function that accepts two strings and returns a boolean. The thing is that when you use this as an extension method, the first parameter type tells the compiler what kind of object you are extending. So when you call it you would only pass in one parameter as you can see in the image below.

image

Enough introduction show some more nifty extension methods

One of the top features in VB9 is that you can work with XML literals, in my opinion one of the best features since IntelliSense was introduced back in VB5. Even if you don’t work much with XML (does people like that still exist?) you can still use them in your source code. Take this simple code for example:

Dim sql As String = <query>select CustomerName from customer where CustemerID=@CustomerID</query>.Value

Here I actually create an XElement object using an XML literal, and then convert it to a string by calling the Value method, which returns the inner value of the element. OK so what’s the benefit of doing that instead of simply putting the text inside quotes? Well, in the above example there are no benefits at all, but your query is probably much longer than what’s shown above.

Dim sql As String = <query>
                      update customer set
                        CustomerName=@CustomerName,
                        Street=@Street,
                        City=@City,
                        Zip=@ZipCode,
                        State=@State
                      where CustomerID=@CustomerID
                    </query>.Value

OK, the above is still a very straight forward query but the idea here is that it’s much easier to read the query when you have it on different lines which you can’t do in VB without doing a lot of string concatenations. The problem with the above is that all whitespaces are contained within the string and there are several spaces at the beginning of each line. So why not write an extension methods that trims off the spaces?

    <Extension()> _
    Public Function TrimmedValue(ByVal xml As XElement) As String
        Return String.Join(vbLf, (String.Join(vbLf, ( _
             From s In xml.Value.Split(New Char() {Convert.ToChar(10)}) _
             Select s.Replace(vbTab, " ").Trim()).ToArray)).Split( _
             New Char() {Convert.ToChar(10)}, StringSplitOptions.RemoveEmptyEntries))
    End Function

Now you can use the TrimmedValue extension method instead of the Value method. Please note that you need to import the System.Xml.Linq namespace to work with XML literals.

Generic types

Extension methods also works with generics. Say you want to paginate an IEnumerable(Of T). There are actually already extension methods available to do this, they are called Skip() and Take().

Dim page = someIEnumerableType.Skip(30).Take(15)

The above will skip the first 30 elements and return the next 15. But let us combine that into one single call.

    <Extension()> _
    Public Function Paginate(Of T)(ByVal list As IEnumerable(Of T), _
                                   ByVal skip As Integer, _
                                   ByVal take As Integer) As IEnumerable(Of T)
      Return list.Skip(skip).Take(take)
    End Function

It’s now easy to paginate anything that implements the IEnumerable(Of T), which means any array or collection.

    Dim names As String() = {"Anders", "Bart", "Ceasar", "David", _
                             "Eric", "Fabian", "Gunnar", "Harald", _
                             "Ingemar", "Joacim", "Karl", "Lennart", _
                             "Martin", "Nick", "Olaf", "Peter", "Quintus", _
                             "Rolf", "Stephen", "Thor", "Ulf", "Valdemar", _
                             "Wiktor", "Xerxes", "Yngve", "Zach"}
    Dim page = names.Paginate(10, 5) 'returns "Karl" through "Olaf"

If you have used LINQ you probably familiar with anonymous types. They are a great way of creating a new object without first having to type the source for it, since the compiler takes care of that for you. But one of the problems with anonymous types is that they are nameless which makes it hard to create a new one of the same type or to create a list of them if you only have a single instance. However using generics and a simple extension method you can turn any type into a list. Deborah Kurata recently posted this code on her blog that demonstrated how you would do that.

    <Extension()> _
    Public Function ToList(Of T)(ByVal type As T) As List(Of T)
      Return New List(Of T)
    End Function

Of course that would only create a list of a specific type, it would not add anything to the list. If you like you could extend the code by adding the value to the list.

    <Extension()> _
    Public Function ToList(Of T)(ByVal type As T) As List(Of T)
      Dim list = New List(Of T)
      list.Add(type)
      Return list
    End Function

So what if you want to test if a type is Nullable?

    <Extension()> _
    Public Function IsNullable(Of T)(ByVal obj As T) As Boolean
      If obj Is Nothing Then
        'if it is Nothing already, it must obviously be nullable
        Return True
      End If
      Dim type = GetType(T)
      If Not type.IsValueType Then
        'if it's not a value type it must be a reference type and reference types are always nullable
        Return True
      End If
      'Nullable.GetUnderlyingType returns Nothing if the type passed to it isn't Nullable
      Return (Nullable.GetUnderlyingType(type) IsNot Nothing)
    End Function
More ideas

As you probably have noticed most of the above examples are pretty short helper functions that are turned into extension methods. The advantage is that you can use the object oriented dot syntax and get IntelliSense to help you make sure you spelled your code correctly. Of course there is no rule that states that an extension method must be brief, but I find that they usually are.

So what more can you do? How about this:

  • Extend a StringBuilder with a ToString method that accepts a delimiter and returns a string that is a delimited list of each line in the StringBuilder.
  • If you often need to store user inputted data in a database, maybe you should extend the String class to have a ToSecureSqlString that for example escapes all single quotes (using two single quotes).
  • Create an extension to the Control class that can do a recursive search for a child control.

You will soon notice that you have quit a large toolkit of nifty extension methods ready to be used in any of your projects.

Have fun!

kick it on DotNetKicks.com

Oracle gets decorated by Microsoft… with an Obsolete Attribute

About a week ago Microsoft announced that the System.Data.OracleClient namespace will be deprecated in .Net 4.0. However the fact is that “deprecated” doesn’t mean it will disappear completely anytime soon. In fact the official statement clearly states the following:

Microsoft will continue to provide hotfixes for critical issues in System.Data.OracleClient as per the standard support policy for .Net Framework 4.0. We will also continue to make critical bug fixes in future service packs for .Net Framework 4.0.

So it will probably hang around for another 8-10 years so your old application doesn’t have to be updated for some time yet. But you probably don’t want to write new .Net 4.0 applications with it. Instead you should probably search for third-party providers.

So why is Microsoft making this move?

Some evil tongues may claim that Microsoft is simply just bored with developing for their competitor, and maybe they are, but that’s not the whole truth. Microsoft have always encouraged their partners and others to provide third-party solutions for their development tools and languages. The official statement also says:

We learned that a significantly large portion of customers use our partners’  ADO.NET providers for Oracle;  with regularly updated support for Oracle releases and new features. In addition, many of the third party providers are able to consistently provide the same level of quality and support that customers have come to expect from Microsoft. This is strong testament of our partners support for our technologies and the strength of our partner ecosystem.  It is our assessment that even if we made significant investments in ADO.Net OracleClient to bring it at parity with our partners based providers, customers would not have a compelling reason to switch to ADO.Net OracleClient.

This is probably true, but I don’t believe that this fact is the complete reason for this decision. Microsoft is now pushing the Entity Framework (and it’s light-weight cousin: LINQ to SQL) and there have never been any promises from Microsoft about any EF support for Oracle. However Oracle themselves have made promises about that, but according to a recent blog post by Julie Lerman (a fellow MVP and the author of the excellent book Programming Entity Framework) this doesn’t seem to happen very soon:

Oracle continues to promise their EF support, though there is still a wait and wonder policy on their end. Their .NET guy, Christian Shay (who spoke at DevConnections in the Data Access track last November) has not updated his blog since September.

Julie also said that DataDirect recently refreshed their beta of Connect for ADO.NET Entity Framework provider for Oracle.

What does this mean for you and me?

As I mentioned earlier, “deprecated” does not mean “deletion”. You can probably continue to support your old applications that uses the OracleClient for many years yet. But consider using a third-party provider for new .Net 4.0 applications. For myself, this doesn’t effect me at all since at my current position at Interactive Medica we use SQL Server and I don’t mind using third-party tools for new development if needed.

pimp itkick it on DotNetKicks.com

Using the CodeDom to do scripting with .Net

Introduction

The company I work for, Interactive Medica, provides SaaS business solutions for the pharmaceutical industry. Many of our clients receive data from third-parties which they need us to import into our system. This is pretty straight-forward and common in many industries. The data arrives in different formats, CSV files (or other delimited text files), Access database, Excel Spreadsheets, and so on. Sometimes the files are zipped and sometimes not. Sometimes the files are uploaded to us via FTP and at other times we need to pull the data, also usually via FTP.

Any developer have at some time or another been acquired to do something like the above, shuffling data from one source to another. Luckily, using ADO.Net, it’s not very hard to do so, as long as you have the correct connection string. Anyhow, when I first started at IM they only had a few of these import programs, which all worked but the problem was that they were all hard coded, by which I mean that any changes made to the file that needed to be imported meant that we had to open up the source code for the import program and make the changes, recompile and deploy it back to the server. This was more than tedious. Some of them were also rather old, written in VB6 in a VBScript style manner, meaning no Option Explicit, new variable names created at the fly and the developer seemed to think that reusing one variable more than once wasn’t necessary when you could create a new one, all with nifty names like Foo, Q, P, S1, M12, and so on that didn’t give a clue at what they contained or was used for. Global scooping also apparently seemed like a good idea, and of course the original developer had since long left the building (the company).

OK, I’m sorry since this wasn’t supposed to be yet-another-ranting about the perils of maintaining old proprietary code. When I was asked to create a new handful of import tools I was determent to make them easier to maintain. What I wanted to do was to create a number of classes that contained all method necessary to do the importing regardless of the data source. All information about the file and where it should be imported to would be in a config file, easily editable without the need to open up the source code. When all this was done, I educated my colleague, that are responsible for monitoring these tools, on how to use them. He isn’t a developer but he does know some VB, and using the framework I created he’s been able to create new import tools by himself.

What I didn’t realize then was how the number of necessary imports was going to grow, and grow fast. The problem is that I never created one program that could use different config files at different points in time. So the program had to be copied, sometimes changed a bit for a new customer import, and deployed as a new EXE file that run on the server at scheduled points in time. I think that we now have around 40 almost identical programs running on the server. So now they are also starting to be a mess to administrate and maintain. So it’s pretty obvious that something needs to be done.

Already two years ago when I first created this import tool framework I was thinking that this should really be done with some clever scripting language. Whenever a change is made in the file that we need to import we could simply open up the script in any text editor make the necessary changes and save it again without the need to launch Visual Studio on my local machine, check out the source code from our source control system, make the changes, test, build and compile it, check in the source and deploy the application on the server. Well, several choices are available. I could have used cool stuff like Python or even PHP but, no that would require their runtimes to be deployed on the server… Not a biggy you say, well it is when it’s going up on a live environment. It have to be lab tested first, with all kind of rigorous evaluation, new documentation have to be written and approved and all that. There was just not time for anything like that since our customer wanted the information yesterday. What we already have on the server is the .NET framework, live with it chump. OK, so how about scripting for .NET then? Well, we always have VSA (Visual Studio for Applications) but that requires a lot of work. Another quicker solution is to use System.CodeDom and reflection, so that’s the path I choose.

Creating the contract

The System.CodeDom.Compiler namespace contains the abstract class CodeDomProvider from which language specific providers inherits. In my case I’m interested in the VbCodeProvider which resides in the Microsoft.VisualBasic namespace, you could also use the CSharpCodeProvider if you wish. The namespace also contains the CompilerParameters class with which you would set, yes you guessed it, parameters necessary to compile the source into an assembly. I will get to these classes shortly, but first let’s have a look at how we would create a contract between a script file and the scripting engine that we will use to run the script. To keep it as simple as possible I created a new Class library project which I named Scripting. This library only contain an Interface with one single method called ScriptMain() which all scripts must implement.

Public Interface IScriptable
  Sub ScriptMain()
End Interface

With that in place a minimal script that does absolutely nothing would look like this:

Public Class MyScript
  Implements Scripting.IScriptable

  Public Sub ScriptMain() Implements Scripting.IScriptable.ScriptMain
    'script code goes here
  End Sub
End Class

Using the CodeDom

Now for the fun part. How to run the script. For the sake of this article I’m going to create a small demo application, consisting of a Windows Form with one button and a multi-line textbox.

image

You would write the script in the textbox and hit the button to run it. To this project I also add a class we will name ScriptEngine. I also added a reference to the above mentioned class library containing our interface. The ScriptEngine class will import the System.CodeDom.Compiler and the System.Reflection namespaces plus our Scripting namespace containing the IScriptable interface.

As I mentioned earlier I’m going to use the VBCodeProvider to compile the script. By default the provider will compile the source to a .Net 2.0 assembly. To be able to use version 3.5 of the .Net framework we need to supply some provider options to the constructor of the class.

Dim providerOptions = New Collections.Generic.Dictionary(Of String, String)
providerOptions.Add("CompilerVersion", "v3.5")
Dim provider As New VBCodeProvider(providerOptions)

Now that we have our code provider we must also add some parameters. The compiler parameters tell the provider how we want our assembly to be built. In our case we do not want to create an executable file but instead just create an in memory assembly. We also want to add references to some other commonly used assemblies like System.Windows.Forms and others, most importantly we add a reference to our Scripting.dll which contains the IScriptable interface. To be able to add this class library it either have to be added to the GAC or exist in the same folder as our executable (I use the latter). We can also provide some compiler options like Option Explicit, note that the /OptionInfer flag can only be used if we use the v3.5 Framework compiler.

Dim parameters As New CompilerParameters
With parameters
  .GenerateExecutable = False
  .GenerateInMemory = True
  .IncludeDebugInformation = False
  .ReferencedAssemblies.Add("System.dll")
  .ReferencedAssemblies.Add("System.Windows.Forms.dll")
  .ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
  .ReferencedAssemblies.Add("Scripting.dll")
  .CompilerOptions = "/OptionExplicit+ /OptionStrict- /OptionInfer+"
End With

OK, we now have everything set up to compile an assembly out of our script. The CodeDom provider have a number of methods to do this, for example CompileAssemblyFromFile() or CompileAssemblyFromSource(). In this demo we don’t have any source file stored on our hard drive so we will use the latter of these two methods that accepts a string as the source. As a matter of fact it actually accepts a ParamArray of strings if we would have several sources of code that we wanted to compile into the same assembly.

Dim result As CompilerResults
result = provider.CompileAssemblyFromSource(parameters, source)

As the first argument we pass the CompilerParameters we just created and then the string containing our source code. If the returned CompilerResults doesn’t contain any compiler errors we are ready to run our script.

If Not result.Errors.HasErrors Then
  Dim script As IScriptable = FindScriptable(result.CompiledAssembly)
  If script IsNot Nothing Then
    script.ScriptMain()
  End If
End If

Here we call a little helper function named FindScriptable() that uses reflection to find a reference to our IScriptable interface that our script had to implement. If found we simply call the ScriptMain() method to run our script.

The full source code for our script engine will look as follows:

Imports System.CodeDom.Compiler
Imports System.Reflection
Imports Scripting
Public Class ScriptEngine
  Public Function RunScript(ByVal source As String) As Boolean
    Dim providerOptions = New Collections.Generic.Dictionary(Of String, String)
    providerOptions.Add("CompilerVersion", "v3.5")
    Dim provider As New VBCodeProvider(providerOptions)
    Dim parameters As New CompilerParameters
    With parameters
      .GenerateExecutable = False
      .GenerateInMemory = True
      .IncludeDebugInformation = True
      .ReferencedAssemblies.Add("System.dll")
      .ReferencedAssemblies.Add("System.Windows.Forms.dll")
      .ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
      .CompilerOptions = "/OptionExplicit+ /OptionStrict- /OptionInfer+"
    End With
    Dim result As CompilerResults
    result = provider.CompileAssemblyFromSource(parameters, source)
    If Not result.Errors.HasErrors Then
      Dim script As IScriptable = FindScriptable(result.CompiledAssembly)
      If script IsNot Nothing Then
        script.ScriptMain()
        Return True
      Else
        Return False
End If Else Return False End If End Function Private Function
FindScriptable(ByVal assembly As Assembly) As IScriptable For Each t As Type In assembly.GetTypes() If t.GetInterface("IScriptable", True) IsNot Nothing Then Return DirectCast(assembly.CreateInstance(t.FullName), IScriptable) End If Next Return Nothing End Function End Class

Now all that is left is to call the RunScript() method from our little editor. So in the Click event of the button I have the following code:

Dim engine As New ScriptEngine
engine.RunScript(TextBox1.Text)

In this simple demo the RunScript() method simply return true or false on success or failure. In a real application you probably want to get more information why the compilation of your script failed. In that case you can let the method return a CompilerErrorCollection instead. You get this collection from the CompilerResults class.

Return result.Errors

You can then loop through this collection and give information about each compiler error including the error message and the line the error occurred on.

Extending the script engine

You now probably want to add your own classes to the script engine. The easiest way of doing that is just to add your classes to the Scripting class library since we already add a reference to that in the script. I only used shared methods in my classes just to make the scripts easier to create, but you could do as you wish. To keep my scripts tidy of any extra “noise” I also simply required that the script contained a public sub called ScriptMain() so that it doesn’t even have the Class or the Implement IScriptable declaration in the script file. I simply add that to the source string before I pass it along to the CodeDomProvider.

I also used the ICSharpCode.TextEditor that is part of the SharpDevelop project to build my own custom IDE with syntax highlighting.

image

Happy scripting!

pimp itkick it on DotNetKicks.com

How to prevent Extension methods from appearing as global functions

C# have static (or shared) classes which VB lacks. Instead VB uses modules which are almost the same thing. One thing differs though and that is that modules makes its public methods appear as global accessible functions, meaning you don’t have to type the ModuleName.MethodName() when calling such a member. You can instead simply call MethodName() directly.

A disadvantage of this fact is that extension methods, that must exist in modules in VB, will appear in IntelliSense as global functions and not only as a method of the type you extended.

Let’s say you have the following:

Public Module ExtensionMethods
  <Extension()> _
  Public Function EqualsText(ByVal str As String, ByVal equalsTo As String) As Boolean
    Dim sc As New CaseInsensitiveComparer
    Return (sc.Compare(str, equalsTo) = 0)
  End Function
End Module

The above extension method allows you to do case-insensitive comparison of two strings in the following manner:

If myString.EqualsText("Hello world") Then
  'do something
End If

The above will be true if myString contains “HELLO WORLD”, “hello world”, or even “hEllO wOrLd”.

The problem is that this method will show up in intellisense even if you don’t use it as a member of a string.

image

However in this case you probably just want the EqualsText function to appear as a member of a string.

image

So is it possible to prevent this extension method from appearing as a globally accessible function?

The answer is yes. You decorate your module with the EditorBrowsable attribute (available in the System.ComponentModule namespace). This attribute is really meant to hide methods or properties from appearing in IntelliSense but it can be used at the class level as well. Note, I do not recommend using this attribute on modules that contain anything but extension methods since it effectively hides everything in that module. 

 

<EditorBrowsable(EditorBrowsableState.Never)> _
Public Module ExtensionMethods
  <Extension()> _
  Public Function EqualsText(ByVal str As String, ByVal equalsTo As String) As Boolean
    Dim sc As New CaseInsensitiveComparer
    Return (sc.Compare(str, equalsTo) = 0)
  End Function
End Module

Now EqualsText() will still appear as a member of a string but not as a global function.

kick it on DotNetKicks.com

What I don’t like about iPhone

A couple of weeks ago I caved in to peer pressure and got myself an iPhone 3G 16GB (probably very stupid since the 3rd generation iPhone is released next week). Before I tell you what I don’t like about this phone I should probably mention that I do love the iPhone, but nothing is ever so good that it doesn’t have room for improvements. I’m also aware that many of the things I will comment on here are being addressed in version 3 but I’m still amazed that some of these features are not available already.

OK, here’s the list:

  • Copy and Paste.
    Why in the name of everything that is holy didn’t the iPhone support Copy and Paste from version 1? Come on, I got my first Nokia phone in the early ‘90s and it had Copy and Paste back then.
  • MMS
    Que? A 3G camera phone that doesn’t allow me to send MMS? Come on…
  • On Screen Keyboard
    Why can’t I tilt the phone while writing a SMS and get the wide-screen keyboard layout like I can in the web browser? I have big fingers and want the slightly larger buttons I get that way. The same goes for typing notes, or adding appointments to the calendar.
  • Low resolution camera.
    This will only be slightly better in the next version, but come on, most new phones have at least a 5 megapixel camera, some have a lot more.
  • Video
    Since the camera itself is able to record video why wasn’t that available earlier?
  • True GPS
    While driving I need to hear the directions being read to me. I don’t always have a passenger beside me that can read them to me.
  • Step into text
    The magnifying glass that appears when you press and hold your finger over the text area you’ve just typed in is pretty cool but not very intuitive, it took me a while before I noticed and learned how to step back into a line of text to correct a typo without deleting everything I’d typed after it. I’ve also noticed that sometimes this magnifying glass appears in the wrong spot so that you actually don’t see where you put the text caret. This is especially true when browsing the web and typing in web forms.
  • I like that the on screen keyboard adopts itself when I type an address into the address field of the Safari browser. Replacing the space bar with three other buttons containing the period, slash, and a “.com” button. This is great since an URL can’t contain any spaces while dots and slashes are pretty common. Switching over to the numeric keyboard from here also shows less buttons than while typing anywhere else. Also a good idea, only show buttons with valid characters. However the thing is that parentheses are valid characters and some addresses do use them (IMHO a bad habit but still valid) but there are no parentheses buttons on the keyboard.
  • BUG!
    Sometimes, granted not very often, after synchronizing the phone with iTunes all downloaded applications stops working. They might show their splash screen and then simply die. When that happened to me I did a Google search and found that many others had had the same problem… Some of them going back more than a year back. Why haven’t this been fixed with new updates to iTunes? Is it just because Apple have the motto: It just works (and when it doesn’t we don’t want to hear about it).