Singleton and Thread Safe Methods on .Net

Español

Recently I had a code review on one of my colleagues project and found this common implementation there:

public sealed class TheClass {
   private TheClass() {}
   private static _theClass value;
   private static object obj = new Object();
    public static Instance Value {
          get {
                 if (TheClass.value == null) {
                        lock (syncRoot) {
                               if (TheClass.value == null) {
                                      TheClass.value = new TheClass();
                               }
                        }
                 }
                 return TheClass.value;
          }
   }
}

Yes we are talking about the classic Singleton pattern here, but not implemented as the proper or, more importantly, safer way. This implementation could permit a double instantation at some point. In short I’ll just recommend to make _theClass volatile, but I know it’s not the unique way to achieve this and is not always the safer way in all platforms. So searching this on the internet I found a lot of discussions so if you are interested on a deep level reading check this links:

volatile and MemoryBarriers
Double-checked locking in .net at Stackoverflow

Alois Kraus even shows the assembled code!
Lazy Vs Eager Init Singletons / Double-Check Lock Pattern

http://www.yoda.arachsys.com/csharp/singleton.html

And of course the Official Microsoft Pattern

Good reading!

Advertisements

System.DirectoryServices.AccountManagement.UserPrincipal Serialization

Español

You can’t serialize this Type, the first reason is simple: Doesn’t have [Serialize] attribute on the base classes, but more important it has a lot of dependencies on non-managed resources that has to be connected to this class. If you could serialize making some tricks the class would expect to connect to Active Directory on the client consumer side, and assuming that the class could connect then it will be non sense to send the class in the first place. It will be more easy that the final client create and connect to the class directly.

So if you need to serialize just “data” of this class like some info on the attributes of an user. The recommended way will be to implement a memento pattern.

When you had a lot of properties and you are not worried of a coupled internal constructor (for example when the class will be serialized for a .Net Web Service and that constructor will not be used on the client consumer side) you could use a internal constructor that get as an argument the original class like:

[Serialize]
public class UserInfo
{
  private string displayName;
  ...

  public UserInfo()
  {
  }

  internal UserInfo(UserPrincipal user)
  {
      this.displayName = user.DisplayName;
      ...
  }
}

This way you can use it in your return methods like this:

return new UserInfo(user);

Good bye JavaScripts Constants?

It’s incredible that Microsoft Internet Explorer (IE) doesn’t support the const keyword from ECMAScript (i.e. JavaScript). And even more incredible that const as a reserved word is deleted from August 26th 2008 ECMAScript 3.1 proposal

If you want to check IE7 behaviour go to IE7 and IE8 beta2 do not support const to define constants in JavaScript

 

Additional References:
JavaScript Constant documentation

My Visual Studio 2008 Favorite Shortcuts and Macros

When I’m programming I spend most of my time in the code editor. I need a powerful IDE with toons of tools, macros and customizations, but first of all a minimalist interface.

I close all toolbars leaving just the top Menu, no toolbars, no tool windows open by default. I have to make that change with every designer. I also remove the Navigation Bar (Tools -> Options -> Text Editor -> All Languages) this speeds up Visual Studio (I don’t know why but is a lot more faster).

In Visual Studio i’m using the following shorcuts and macros to have the minimal user interface tools at any given time:

To select an open Tool Window: Ctrl + Tab
To hide a Tool Window: Shift + Esc

To show the Solution Explorer: Ctrl + Alt + L
To hide the Solution Explorer: Shift + Esc (When is selected, otherwise you can press Ctrl + Alt + L to select it, or Ctrl + Tab to get the Solution Explorer Window Active)

If you want that your shorcuts act as a toogle commands you can create a Macro like this:

Sub ToggleSolutionExplorer()
  Dim w1 As Window = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)
If w1.Visible Then
     w1.Visible = False
    Else
     w1.Visible = True
    End If
End Sub

 

This way you can use the same shorcut to display or hide the tool window. Anyway I find best to use the standard command to show the window that you want to close and then use Shift + Esc. That way you have the same command to close windows and in the worse case you have to to two commands to close a window that isn’t selected, but is easier to remember standard commands.

More shorcuts:

Command Window: Ctrl + Alt + A
Class View: Ctrl + Shift + C
Properties: F4
Error List: Ctrl + |, Ctrl + E (and then F8 to navigate fordward within the list and the code)
Comment Code: Ctrl + K, C
Uncomment Code: Ctrl + K, U
Ctrl-PgUp/PgDn cycles between markup/designer/split views.
F7/Shift F7 cycles between designer view and code view. 
 

Another very useful macro and shorcut that I use is Macros.Samples.Accessibility.DecreaseTextEditorFontSize and Macros.Samples.Accessibility.IncreaseTextEditorFontSize. It’s very handy when you are browsing code, when you show code to other colleagues or when you are projecting your code. My mapping is:

DecreaseTextEditorFontSize: Ctrl + “Num-”
IncreaseTextEditorFontSize: Ctrl + “Num+”

References

Visual Studio .NET Shortcuts for Toggling Tool Windows by Kevin McFarlane
New Window Management Features for Visual Studio 2005 by Sara Ford