May 2007 - Posts

Searching source code on Koders.com

Lately Phil Haack has blogged about his new job at Koders.com. As far as I can see they provide a search engine for open source code and let people submit their repositories so that their code can be indexed.

I've developed some interest in code searching since I'm going to implement a code search engine for my Information Retrieval course here in Italy, and hope Phil will give me some ideas in his future blog posts about Koders. In fact, I'm going to use Lucene.NET for indexing the source files - which I think they are using, too - and maybe I can learn some good practices and get some tips from them. 

Though it doesn't have a slick interface as Krugle, Koders provides a service for querying its engine from the web, returning the search results as RSS, so I thought I could write a wrapper on top of it. I've come up with a fluent interface query class based on strongly typed classes generated with the ASP.NET RSS Toolkit.

Koders service allows refining search results by programming language, license, logical placement of the code in the source file (class name, method name, interface name) and by file name.

In the wrapper I created the querying is a two-step process. First you need to create a KodersQuery class supplying the parameters of the search, then you use one of the strongly typed classes generated by the ASP.NET RSS Toolkit to load and parse the results:

KodersQuery q = new KodersQuery()
.ProgrammingLanguage(ProgrammingLanguage.Csharp)
.SearchTerms("Subtext");

KodersRss rss = KodersRss.Load(q);

The KodersRss class wraps the xml returned by the query and provides the items found via the KodersRss.Channel.Items property.

Given the profileration of LINQ to X implementations it would be cool to create such an API for search source code, either on Google Code Search or Koders, but I'm still waiting for the final bits of LINQ before to attempt doing it. In fact I've seen the source code of some LINQ to X libraries and it looks like they won't compile with Orcas if they were created with the May CTP.

At the moment the source code is on my svn repository.

kick it on DotNetKicks.com

Posted 22 May 2007 02:49 AM by simoneb | 7 comment(s)
Filed under:
When is a singleton not a singleton? Serialization!

Some days ago I wrote about the issue of having multiple instances of a singleton class in the same AppDomain. As for the implementation I gave and as emerged from the feedback to the post, this can happen when that class is serialized and deserialized or when it is instantiated via reflection. This is the original implementation:

public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();

// Private constructor to prevent external instantiation
private Singleton()
{ }

public static Singleton Instance
{
get { return instance; }
}
}

In case of serialization, there's actually a workaround explicitly provided by the .NET framework. This consists in implementing the ISerializable interface to provide a custom serialization mechanism.

Upon serialization, the GetObjectData method of the ISerializable interface is called to get the custom data needed for serializing the object. These data is set by setting up an appropriate SerializationInfo object. What's needed is a way to instruct the formatter about the identity of the object to be serialized, so that when it's deserialized it will be a reference to the same object - which we need to be unique. This can be accomplished by using the SetType method of the SerializationInfo object passed as a parameter to the GetObjectData method.

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.SetType(...); // accepts a Type parameter
}

What's the type we need to supply to the SetType method? It's not typeof(Singleton) - no need to specify that though, it's implicit - because in that case, upon deserialization, a particular constructor that the class has to define is called. The following is the signature of the constructor called by default when deserializing a class which implements the ISerializable interface:

Singleton(SerializationInfo, StreamingContext);

If no type is set using the SerializationInfo.SetType method into the GetObjectData method (called upon serialization), the above constructor is called on deserialization to populate the object being deserialized with custom data. If such a constructor is not implemented a SerializationException exception is thrown.

We won't need to implement that constructor since we want to avoid instantiating the class, but instead we need to pass to the SerializationInfo.SetType method the type of a helper class capable of returning the right instance of our singleton class. Again, the framework provides an interface explicitly designed for this task, called IObjectReference.

namespace System.Runtime.Serialization
{
public interface IObjectReference
{
object GetRealObject(StreamingContext context);
}
}

This interface lets you specify that the class implementing it doesn't create new objects, but instead returns a reference to another object. This is good for us, since we can implement this interface by returning, via the GetRealObject method, a reference to the singleton instance of the Singleton class, thus preventing the formatter from instantiating a new object during deserialization.

So our helper class ends up having an implementation like in the following code snippet: 

[Serializable]
internal class SingletonSerializationHelper : IObjectReference
{
public object GetRealObject(StreamingContext context)
{
return Singleton.Instance;
}
}

Going back to the Singleton class, the original code now is edited to make use of the SingletonSerialization helper class:

[Serializable]
public class Singleton : ISerializable
{
private static readonly Singleton instance = new Singleton();

// Private constructor to prevent external instantiation
private Singleton()
{ }

public static Singleton Instance
{
get { return instance; }
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.SetType(typeof(SingletonSerializationHelper));
}
}

What this code does when an instance of the class is serialized and deserialized can be summarized as follows:

  1. Upon serialization, the GetObjectData method of the Singleton class is called and the formatter is instructed that the type of the object being serialized is actually not Singleton, but instead SingletonSerializationHelper.
  2. Upon deserialization, the formatter knows that it needs to instantiate an object of type SingletonSerializationHelper, this being a class implementing the IObjectReference interface. Therefore, instead of calling its constructor, the GetRealObjectMethod is called, and the right, singleton instance of the Singleton class is returned.

Again, note that if the SerializationInfo.SetType method wasn't called during serialization, when deserializing the formatter would have tried to call the overloaded constructor of the Singleton class, and if it wasn't provided a SerializationException exception would have been thrown. The reason why it isn't called upon deserialization is that we make the formatter believe that the type of the class it will have to deserialize is of type SingletonSerializationHelper in place of Singleton.

This example appears on the MSDN Library in a couple of pages about serialization, although I learned about this workaround when reading an old article written by Jeffrey Ritcher on the MSDN Magazine. 

kick it on DotNetKicks.com

Posted 04 May 2007 03:37 AM by simoneb | 9 comment(s)
Filed under:
ASP.NET Internals - follow my article series

A few days ago I wrote about the chance of writing technical articles for DotNetSlackers. My article has been published today and it's the first of a series of 3 or 4 about the ASP.NET infrastructure. In this article I talked about IIS and the process model implemented by ASP.NET when running on different versions of IIS. In the next one I'll tackle the ASP.NET pipeline and in the last one the compilation mode and page lifecycle, thus trying to cover all the aspects concerning the life cycle of a web request. I kept one eventual fourth article to talk about topics I didn't cover in the former three, like the new features introduced by ISS 7 and threading.

You can read the first article here. If you like the article please leave a vote, and well, feel free to leave a comment for any questions.

Posted 02 May 2007 01:39 PM by simoneb | no comments
Filed under:
The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.

This site

Search

Go

This Blog

News

     

    CS2
    Checkout CS2, my academic project about indexing and searching personal source code with Lucene.Net.

    Windows Developer Power Tools

Syndication

Sponsors

  • MaximumASP
  • Packet Sniffer
  • conference calls glossary