Dev @ Work

A day in the life of a developer

Baking IL With DynamicMethod

May 31st, 2009 by Trilobyte

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 »

Two old articles

April 30th, 2009 by Trilobyte

I just added two of my old articles about the Windows Template Library:

WTL PictureBox

WTL Resourceless Dialog Toolkit

You can also find these articles on the CodeProject and CodeGuru.

Asynchronous Payment Provider Implementation Pattern

March 17th, 2009 by Trilobyte

PaymentToday at Liones I started working on the integration of a payment service provider (PSP) for a web shop implementation. While I was thinking about the architecture I discovered that it is actually very straightforward and that most of the application flow is reusable in different implementations with other PSPs.

Take a look at the UML diagram below; you will notice that there are actually two flows:

  • One synchronous, which guides the user trough the payment screen
  • One asynchronous, which notifies when the payment state changed (payment is confirmed or canceled)

Read the rest of this entry »

Lucene.NET articles

March 14th, 2009 by Trilobyte

I just published the Lucene.NET articles I have been working on. I hope you like them.

Here is an overview of the articles I have written:

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

Cheers!

BitArray Calculating Cardinality

February 20th, 2009 by Trilobyte

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 Trilobyte

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.

Designing an Online Shopping Cart, an Architects Tale

February 16th, 2009 by Trilobyte

Webshop Cart

For my work at Liones I am writing a technical design for a shopping cart. The requirements are fairly basic, except for one: the shopping cart must be generic to support any payment service provider (psp) and customer relationship management (CRM) system. Also there might be several different product types in the future, like: subscriptions, downloads, pay per use, etc.. These requirements make the shop very complex and a great challenge.

I started with an investigation on the internet about this subject and I quickly found out that there aren’t much articles on this topic around, but I found a few very interesting ones though. Next I started to draw some UML diagrams; I added some best practices like the Money class.

Read the rest of this entry »

Model View ViewModel ( M-V-VM )

February 15th, 2009 by Trilobyte

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 »

Dogs eat Homework, Cats eat Source Code

September 1st, 2008 by Trilobyte

In short: no they don’t. Students are making up excuses for not doing their homework and developers do the same when their work isn’t done either. People, in general, blame anything but themselves for not getting their work done.

The truth, however, is that everyone is responsible for their own actions. As developers we should provide solutions instead of excuses. Of course we are allowed to make mistakes or an error in judgment, but we have to admit that honestly and try to offer options.

We can blame clients, project leaders, our lead developers, our architects, our office manager or that new trainee but in the end you, and only you, are responsible for your own work.

So next time you think about making up an excuse, think about solutions first. You will sound smarter that as a matter of a fact you will be smarter!

Python, without using Python

August 26th, 2008 by Trilobyte

Today I faced a problem at work: I had to come up with a flexible, extendable architecture for a XML-RPC client service application. Both the client and server code had to be integrated into existing applications which makes the impact of introducing new message handlers painful if not designed properly.

While thinking about those requirements I quickly thought of a concept I saw in Python: Try to call find a callable method, otherwise do the default action. In Python you can use ‘getattr(…)’ to get a possible method of a class and with ‘callable(…)’ you can check if the method is actually a method.

You get something like this on your dispatcher object:

def Dispatch(self, methodName, arguments=None)
   method = getattr(self, methodName, None)
   if callable( method ): method( arguments )

I implemented this extensible architecture in exactly the same way. It was fairly easy to do, painless and it is extensible.

Learning multiple programming languages/concepts pay off!