Back to blogging, but I moved

I haven't been blogging for quite some time now, but I'm back to blogging at simoneb.github.com. There have been many changes in my professional life so the new topics I'll be blogging about may or may not be of interest, although I'd appreciate if you took the time to read through them!

There's a new rss feed, but for those who were following me already I will be pointing the current feed to the new one soon, so you don't need to change anything in your subscription. For those who weren't following me the new feed to subscribe is this one.

CS2 - a personal source code search engine

As I blogged some time ago I have been developing a source code search engine called CS2. I completed it lately and, although it's not as feature-rich as others - say, Koders Pro Edition - it works pretty fine and suites the needs of whomever needs to search through code stored on its machine. Actually, it's neither as heavy as Koders Pro as well, and plus it's free and open source.

It works as an ASP.NET 2.0 website and lets you request indexing folders and singular files, as well as search through them with a simple syntax. By default searches are performed in full-text fashion, but you can restrict queries on particular fields. The syntax is documented in the website. It's multithreaded so indexing and searches can occur at the same time.

It can be customized using the Properties.config file in the Configuration directory, by setting the folder where the files of the index are stored as well as the update interval. It's capable of automatically removing from the index files which no longer exist on the file system as well as update those which have been edited.

At the moment it can parse and index only C# source code but it's written upon Windsor inversion of control container and can be easily extended just by adding a new parser. If anyone's interested I will post about the complete process of adding a new parser.

To get started just download the zipped website from the project homepage and run it. Then type the path of a folder containing C# source code files on your machine in the corresponding input field and wait some time for the indexing process to complete (you won't see the progress since it occurs on another thread so that you can perform searches concurrently with indexing). Its duration depends upon the number of files to be parsed and indexed. Once indexing is complete updating the page shows the number of documents the index contains. Click on the link on the upper-right edge of the page to show the search syntax.

kick it on DotNetKicks.com

New SQLite provider for ELMAH

I bet you know what ELMAH is, don't you? ELMAH stands for Error Logging Modules and Handlers, and its main and only purpose is to log exceptions occurring in ASP.NET web applications, within which it integrates with just a couple of entries in the Web.config file. It's got a lot of documentation and it's open source, so for any additional information check out its website.

Until yesterday it let you log exceptions in memory, on xml files and SQL Server, but a highly requested feature was to use SQLite as a backing store. Although I didn't know SQLite much it has a cool .NET wrapper published on Sourceforge which exposes its features to the managed world, so I took on the reported feature request and implemented it. SQLite doesn't offer all of the features provided by SQL Server, but it's almost completely SQL92 compliant so it has all that's needed, and much more.

So why to use SQLite instead of SQL Server or xml files? SQLite is a relational database engine, so it's much faster than xml files, and compared with SQL Server at the moment our tests didn't show any performance penalty; plus, it's free! Paging through about 15.000 stored exceptions via ELMAH user interface resulted to be lightning fast!

Since SQLite syntax isn't widely documented, if anyone's interested I will blog about it and the steps I went through to integrate it with ELMAH. Otherwise, the source code is available on ELMAH Subversion repository.

kick it on DotNetKicks.com

IoC with Windsor Container part IV live

The fourth and last article of the series about IoC is online and available for reading here. Any feedback is welcome, and please rate the article! Each article links the other articles of the series so the reading should be straight from beginning to end.

kick it on DotNetKicks.com

Posted 30 August 2007 11:54 AM by simoneb | 2 comment(s)
Filed under: ,
IoC with Windsor Container part III live

The third article of the series about IoC is online and available for reading here. Any feedback is welcome, and please rate the article!

kick it on DotNetKicks.com

Posted 11 August 2007 04:17 PM by simoneb | 2 comment(s)
Filed under: ,
IoC with Windsor Container part II live

The second article of the series about IoC is online and available for reading here. Any feedback is welcome, and please rate the article!

kick it on DotNetKicks.com

Posted 31 July 2007 08:45 PM by simoneb | no comments
Filed under: ,
IoC with Windsor Container on DotNetSlackers

I've written a 4 parts article series about Inversion of Control and Dependency Injection; the first part is already online here, the following parts should be online soon. It's a comprehensive overview of most of the features offered by Castle Windsor Container, starting from the basics and on to more advanced topics.

I hope you'll enjoy, so feel free to leave feedback!

kick it on DotNetKicks.com

Posted 24 July 2007 02:41 PM by simoneb | 2 comment(s)
Filed under: ,
Indexing and searching source code with Lucene.Net

During the past week I've been working on a university project for my course of Information Retrieval, and although the project proposal from the teacher was to implement an xml parsing application written in Java I thought I would put in use the skills I developed on my own with the .NET framework to implement something more useful.

My idea was to create a homemade source code indexing and search service, so I started fiddling with Lucene.Net, CastleProject, C# Parser and a couple other open source projects to see what I could come up with. There are already a lot of services which allows to search source code online, see Krugle, Google Code Search and Koders among others.

Well, of course I couldn't use one of them as my course project, so I started implementing my own. I called it CS2 - C Sharp Code Search, and its source code is available under the MIT license on its Google Project Hosting website. I think it's a good example of the usage of Lucene.Net and CastleProject's IoC container in a wanna be real life project.

At the moment only the indexing part is implemented and you can see it working launching the console application project contained in the solution. The index created is compatible with the Lucene family implementations, so it can be browsed using an application like Luke, until I implement the searching part.

At the moment the features it implements are indexing C# source code files by parsing them and retrieving information like class, method and property names so that they can be searched against, as well as full-text search. It is extensible by implementing parsers for other languages, I've built it to make it pretty straightforward. It remembers the files indexed and periodically checks for modifications or file deletions. The console project comes with a full logging mechanism which shows what the program is actually doing. It is highly configurable via configuration files, see App.config and the files in the Configuration directory, used mostly for Castle Windsor configuration.

Let me know what you think! I'll say more about it in the next weeks... and please, don't hand it to your Information Retrieval teachers until I've delivered it to mine ;)

kick it on DotNetKicks.com

Posted 29 June 2007 06:09 PM by simoneb | 9 comment(s)
Filed under:
How to sort a generic List<T>

After reading this post from Steven Smith I thought I should write something about it.

Sorting a generic List<T> is pretty straightforward if you know how to do it. With C# 2.0, anonymous methods come at hand, as well as the little known Comparison<T> delegate (check out this post for more information about this class as well as other useful classes new to C# 2.0).

Ok, let's suppose we have a product class (let me save some space by using C# 3.0 syntax).

class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
}

When we have a list of products we may want to sort it on the ProductName property before displaying it to the user. This can be accomplished with the Sort method of the List<T> class, which defines several overloads. The most handy in this case is the Sort(Comparison<Product>) method and the result is easily achieved with a couple lines of code.

List<Product> products = new List<Product>();

products.Sort(delegate(Product p1, Product p2)
{
return p1.ProductName.CompareTo(p2.ProductName);
});

So far so good, but what if we need to sort our list in several places during the execution of our program? Do we have to write that code each time? Actually no, since we can use the parameterless Sort() method of our list class. What this method does is use the "default comparer" to sort the list. So what's this default comparer? It's the comparer that's automatically created if we implement the IComparable<T> interface. This way we can centralize the sorting logic into our class, and just call the parameterless Sort() method on it whenever we need it sorted on the ProductName property.

public class Product : IComparable<Product>
{
[...]

public int CompareTo(Product other)
{
return ProductName.CompareTo(other.ProductName);
}
}

Ok, now what if we want to be able to sort it on the other two properties, ProductID and UnitPrice? Do we have to write an anonymous method each time as we did in the beginning? Of course no, since there's a useful trick which prevents us from needing to do that. We can define two static Comparer<Product> properties in our product class, and supply them as parameters to the Sort(Comparer<T>) method of our list whenever we need it sorted on something which is not the default sorting logic.

public class Product : IComparable<Product>
{
[...]

public static Comparison<Product> PriceComparison =
delegate(Product p1, Product p2)
{
return p1.Price.CompareTo(p2.Price);
};

public static Comparison<Product> IDComparison =
delegate(Product p1, Product p2)
{
return p1.ProductID.CompareTo(p2.ProductID);
};

[...]
}

Since they are static they can be used simply like so: products.Sort(Product.PriceComparison) or products.Sort(Product.IDComparison), which will respectively sort the list by price and id.

Below is the full code of the Product class.

public class Product : IComparable<Product>
{
private int id;
private string prodName;
private decimal price;

public static Comparison<Product> PriceComparison = delegate(Product p1, Product p2)
{
return p1.price.CompareTo(p2.price);
};

public static Comparison<Product> IDComparison = delegate(Product p1, Product p2)
{
return p1.id.CompareTo(p2.id);
};

public int ProductID
{
get { return id; }
set { id = value; }
}

public string ProductName
{
get { return prodName; }
set { prodName = value; }
}

public decimal UnitPrice
{
get { return price; }
set { price = value; }
}

public Product(int id, string prodName, decimal price)
{
this.id = id;
this.prodName = prodName;
this.price = price;
}

#region IComparable<Product> Members

public int CompareTo(Product other)
{
return ProductName.CompareTo(other.ProductName);
}

#endregion

public override string ToString()
{
return string.Format("Id: {0} Name: {1} Price: {2}", id, prodName, price);
}
}

kick it on DotNetKicks.com

Posted 20 June 2007 04:19 AM by simoneb | 35 comment(s)
Filed under:
ASP.NET Internals - the second article is available for reading

The second article of my series about ASP.NET Internals is up. Before reading I reccomend giving a look at the first one however.

It took me a while to write it since I had to fiddle into the ASP.NET infrastructure with several profilers to get to understand the undocumented parts, but it was fun. In this article I've talked about the interaction between ISAPI extensions and managed code as well as the setup mechanism of AppDomains.

Next part will cover the principal managed part of ASP.NET, the HTTP Pipeline.

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 | 8 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 | 10 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:
.NET interview question - when is a singleton not a singleton?

I've read some posts lately with questions people use to ask or have been asked during a job interview, so I wanted to post a tricky question too.

I've talked about singletons in the past, and what we use to think is that a classic singleton class has to be unique into a specific AppDomain. Let's take this simple implementation of the singleton pattern, which is thread safe, too: [edit: made the class sealed to avoid confusion]

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; }
}
}

There's at least one situation where you can find yourself having multiple instances of the singleton class in the same AppDomain, can you tell when?

kick it on DotNetKicks.com

Posted 30 April 2007 01:14 AM by simoneb | 11 comment(s)
Filed under:
DotNetSlackers is growing and paying you!

DotNetSlackers.com is one of the best .NET news providers there are, it aggregates a lot of feeds from the most interesting bloggers out there and provides them either as a dynamic aggregate RSS feed or a newsletter service which are much easier to follow since grouping all the interesting stuff into a single channel, evolving continuously and adding new content day by day. The service is highly customizable since you can choose to filter the content you receive by tags and keywords.

I got to know DNS about a year ago when following a link on Scott Guthrie's blog - need I say anything more? - and I always found it just what I needed to stay up to date with the daily news from the .NET world. 

Driven by Sonu Kapoor and powered by CommunityServer, DNS provides a vast range of services, like forums, newsgroups, web services, a blogging platform and lately even articles written by well known authors explicitly for DotNetSlackers.

Some days ago Sonu announced that DNS is going to pay article's authors as much as $100 per article, and if you've got something to say about .NET and want to write about it I highly encourage you to look at the submission guidelines and submit your article proposals! As I already did ;)
 

kick it on DotNetKicks.com

More Posts Next page »

This site

Search

Go

This Blog

News

Syndication

Sponsors

  • MaximumASP