Dev @ Work

A day in the life of a developer

Baking IL With DynamicMethod

May 31st, 2009 by

At the moment I am building a new script engine which is consists of text and expressions embedded in them. Those expressions are basically calls to functions and have any number of arguments. The implementation of those functions is done in C#.

At compile time I don’t know which methods are going to be called so I have to invoke them at runtime. The most obvious way to do that is to use the reflection API of .NET to do a late bound method call. Although, IMHO, this is a perfectly valid solution to this problem it isn’t exactly fast.

System.Reflection.Emit.DynamicMethod is a better alternative for late bound method invocation when it comes to performance. Although it requires you to write Intermediate Language (IL) it is actually quite easy to use.

Read the rest of this entry »

BitArray Calculating Cardinality

February 20th, 2009 by

For my faceted search Lucene.NET implementation I needed to get the cardinality of a BitArray. The cardinality is the number of bits in turned on. I found a VB snippet and I converted that one to C#.

Here is my implementation:

private static readonly byte[] _bitsSetArray256 = new byte[] { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 };

public static int GetCardinality( BitArray bitArray )
{
var array = (uint[]) bitArray.GetType().GetField( "m_array", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ).GetValue( bitArray );
int count = 0;

for ( int index = 0; index < array.Length; index ++ )
{
count += _bitsSetArray256[ array[ index ] & 0xFF ] + _bitsSetArray256[ ( array[ index ] >> 8 ) & 0xFF ] + _bitsSetArray256[ ( array[ index ] >> 16 ) & 0xFF ] + _bitsSetArray256[ ( array[ index ] >> 24 ) & 0xFF ];
}

return count;
}

The reflection access to the private field of BitArray isn’t exactly fast, but I will optimize it later and post the updated snippet right here.

I hope this implementation is as useful for you as it is for me.

Lucene.NET and Facetted Search

February 20th, 2009 by

LuceneFor my work at Liones I has to implement a Lucene.NET search solution, including drill-down/faceted search. This was pretty hard to accomplish because there wasn’t much reference material, at least not for C#, but I just finished the first implementation and it works like a charm. I am not going to discus the implementation in detail because I decided to write some articles about it.

For now, I am thinking about the following articles:

  1. Introduction to Lucene
  2. Indexing basics
  3. Search basics
  4. Alternatives ( did you mean …)
  5. Faceted search / Drill down
  6. Class reference

Let met know if you have got more ideas for articles.

Model View ViewModel ( M-V-VM )

February 15th, 2009 by

Recently I have been working on a Windows Presentation Foundation (WPF) application designed to manage tasks. It is not the first time I developed an application in WPF. However, I was struggling with the same problem I was struggling with before:

The problem: Coupling my views (WPF) to my domain model (C#).

The solution I came up with back then is to make the domain model aware of the need to bind and implement specific functionality like [System.ComponentModel.INotifyPropertyChanged]. By doing this I cluttered my domain model with non domain model code, which made the model difficult to understand and maintain, basically: No Go!

The solution: Model-View-ViewModel pattern

I went out looking for pattern that could help me to eliminate this code cluttering. After doing some research using my favorite search engine I discovered that there was a pattern for this problem: Model – View – ViewModel (MVVM or M-V-VM).
Read the rest of this entry »

Hello NHibernate

June 26th, 2007 by

I have spend the two past days getting to know NHibernate, which is an .NET port of Hibernate: an object-relational mapping framework (ORM) for java and I am really excited about it. The framework allows you to create POCO’s (Plain Old CLR Objects) and store them in a relational database and this is where it gets exciting: it is incredible easy to do so.

NHibernate consists out of three parts: The first part are the POCO’s which interact with other .NET code. The second part is the mapping file, which describes how the POCO’s are mapped to the relational database schema. The third part is the configuration of NHibernate itself, which contains the database server and the connection string.

NHibernate contains a lot more like: querying and caching, but that is beyond the scope of this post.

Read the rest of this entry »

Castle Windsor Component Messaging: Senders & Receivers

June 12th, 2007 by

Here is some more information about the messaging framework I have been working on the last couple of weeks. Although I had less time to work on the framework then I had expected, I managed to get some work done on message filtering, but that is a subject for a next post.

In this post I will explain how you implement a message sender and receiver class. I tried to keep the framework as simple as possible and I think I did a pretty good job, however if you have any comments or ideas, please let me know.

Anyway, enough talk, lets get down to business.

Implementing a message sender

Implementing a sender is very easy, there are two things to do: The first step is to implement the IMessageSender interface or derive from the SenderBase class. Then you need to decorate the sender class with the MessageSenderAttribute. That is it.

Example:

[MessageSender()]
public class TestSender: SenderBase
{
	public void SendMessage()
	{
		TestMessage msg = new TestMessage();
		base.DoSend(msg);
	}
}

The TestMessage which is send in the example code does not contain any information, but you can define your own message class which contains any information you can imagine.

Implementing a message receiver

The implementation of a receiver class is not equal easy as the implementation of the sender class. There are two steps as well: The first step is to implement the IMessageReceiver interface and the second step is to decorate the class with the MessageReceiverAttribute.

Example:

[MessageReceiver()]
public class TestReceiver: IMessageReceiver
{
	public void Receive(IMessage message)
	{
		// do something with the message
	}
}

The receive function will be called when a message is received.

Currently I am working on the implementation of the message filters. The idea is that you can do pre-processing on messages using some clean C# constructs. When I got an implementation ready I will post it on this weblog. That is it for now. See you next time.

WCF Username Authentication

May 31st, 2007 by

In this post I will explain how you can build an Windows Communication Foundation web service and client which use a Username and Password combination to authenticate a user. The most difficult action is to create a X509 certificate which is used to encrypt messages passed back and forward to the server.

In this application we will use WCF’s wsHttpBinding and message level security provided by an X509 certificate. The X509 certificate encryption is required by WCF because the client credentials (username/password) are passed as clear text in the SOAP message.

There is one problem that we will face during this series of posts. WCF is reluctant to accept a test certificate, it requires a lot of extra work to get it done. However once you understand the steps that you need to take, you will find it an repetitive but easy task.

I hope you find this post useful. If you have any questions or comments, feel free to post them as reactions on this post. Enjoy!

Read the rest of this entry »