Thursday, November 24, 2005

Delegate smell cont.

How come that delegates can call private functions, as in (inspired by MSDN):

ElapsedEventHandler eh = new ElapsedEventHandler(OnTimedEvent);
private void OnTimedEvent(object source, ElapsedEventArgs e) { }


== does not equal Equals()

I discovered that for boxed System.ValueType the == operator behaves differently from the Equals() function. When using == with two boxed ValueType-s it will compare their references, thus "always" returning false. Equals(), on the other hand, has been overloaded by Microsoft for the ValuType-s, so that it does a value comparison of the boxed objects, giving the expected result.

I wonder why Microsoft did not also overload the == operator?

Wednesday, November 23, 2005

Deep copy of Hashtable

In order to solve my problem of returning a hashtable by reference (see previous post), I first tried to use Hashtable.Clone() to give me a copy of the hashtable. However since Clone() only does a shallow copy of the hashtable, this did not work since the hashtable contained Objects and changing them in the calling code caused side-effects in the originating class. So what was needed was a deep copy of the hashtable and this proved to be really simple:

private Hashtable mMeasurements = new Hashtable();
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();

clonedMeasurements = (Hashtable)bf.Deserialize(ms);

Are there any concerns regarding this code?

Mocking SAO

I have been unit testing a class that interacted via Remoting with a Singleton. I wished to eliminate the remoting part and use a mock object for the singleton. There was one hinge to that: One of the singleton's public methods returns a Hashtable. The Remoting automatically took care of serializing the hashtable and recreating it at the client. However, when I started using the singleton directly (in the same assembly) as the class under test, the hashtable was returned by reference causing undesired side-effects when the class under test started manipulating the hashtable!

More on the solution shortly...

Same problem arrises for SingleCall objects and CAOs.


I have been exposed to System.Net.Sockets.UdpClient recently. One of its member functions is Receive(ref IPEndPoint). I was wondering why IPEndPoint was passed to the function as a reference (pointer to a pointer actually). So I downloaded the source code for the Mono project, thinking that it would give me a clue. There the object passed in is never used, but a new IPEndPoint created and it passed back to the calling function. This actually makes sense since the returned IPEndPoint contains the IP address of the party sending the UDP package. But why then not define Receive() as Receive(out IPEndPoint)? Thus making the intention clear.

In fact setting IPEndPoint as null before passing it into Receive() works fine.

P.s. It was not necessary for me to pull out the Mono source, the Reflector gave me the code just fine :)

public byte[] Receive(ref IPEndPoint remoteEP)
EndPoint point1;
if (this.m_CleanedUp)
throw new ObjectDisposedException(base.GetType().FullName);
if (this.m_Family == AddressFamily.InterNetwork)
point1 = IPEndPoint.Any;
point1 = IPEndPoint.IPv6Any;
int num1 = this.Client.ReceiveFrom(this.m_Buffer, 0x10000, SocketFlags.None, ref point1);
remoteEP = (IPEndPoint) point1;
if (num1 < 0x10000)
byte[] buffer1 = new byte[num1];
Buffer.BlockCopy(this.m_Buffer, 0, buffer1, 0, num1);
return buffer1;
return this.m_Buffer;

Tuesday, November 22, 2005

Listening to UDP broadcasts

I found the following statement on MSDN:

"The UdpClient class can broadcast to any network broadcast address, but it cannot listen for broadcasts sent to the network. You must use the Socket class to listen for network broadcasts."

However this simple UdpClient:

client = new UdpClient( 1234 );
point = new IPEndPoint( IPAddress.Any, 0 );
while (true)
Console.WriteLine(Encoding.ASCII.GetString( client.Receive( ref point ) ));

works equally well for broadcasted (*.255) as well as unicasted (*.131) udp packages.

I am confused.

Monday, November 21, 2005

Small VS2003 tid bit

I found out an interesting fact about adding references to 3rd party dlls in VS2003 projects (have not tried this in VS2005). If, when browsing to the dll, you go through "My Documents" the dll will be placed in the the project file (*.csproj) with an absolute path. If, however, you store the dll in some 'normal' folder on the hard disk (C-drive typically), then the added reference will placed in the project file with a relative path.

The reason for this behaviour is probably that "My Documents" is treated as a mapped network drive.

This tid bit is not going to be of much interest to you unless you share your project files with other developers, then it will cause you pain if your paths to the project files are different. The lesson is not to store your code under "My Documents" :)

Sunday, November 20, 2005

Introducing mock objects in unit testing

The problem is: During unit testing I wish to replace (mock) a certain class (A) that is being used by the class (B) being tested.
The question is: how is this best done?

I can think of three ways:
  1. Inherit B (with class C) and override the (protected) method that loads up A. In C, A-mock would be loaded instead of A. During testing run the unit tests on C instead of B.
  2. Add public or internal methods to B that allow A being set. During testing give B A-mock instead of A using these new methods.
  3. Use reflection to load A dynamically based on the configuration (in app.config, e.g.). During testing replace A with A-mock in the configuration.
All these ways impose some change to the class, B, under test, but what change is most acceptable?

Unfortunately I don't have the answer to that :)


I took a look at Fitnesse "a software development collaboration tool", I had had some experience with it earlier, but had forgotten how it functioned :)

It helps me to understand what Fitnesse is by breaking it up into two parts:
  • It is an extension to xUnit which allows you to configure the unit tests (without changing code).
  • It is an UI which facilitates the configuration of the tests.
In fact the UI part is presently implemented using a Wiki, but could "easily" be replaced by another technology.

One thing bothered me while reading about Fitnesse, i.e., that the Fitnesse tests are called "acceptance tests". I think it is misleading to say that the customer writes the test, what he is actually doing is configuring the test that have been written by the developer. For me "acceptance test" is where the customer actually defines how and what to test, he should not rely on the developer to write the correct tests. The customer gets the final product and plays with it.

This, however, does not diminish the usefulness of Fitness tests, they do add value to the unit tests, especially if they get the customer more involved in the project. Maybe I am going overboard with this discussion, not having even installed or tested Fitnesse :)

Monday, November 07, 2005

O/R libraries

htomasso suggested in a comment to my last blog entry that I should take a look at NHibernate for my object/relational mapping needs. I remember learning about object oriented databases, but this seems solve the problem as well, i.e., persisting objects to a database (for retrieval).

My current problem is storing data from a hierarchal DTO model to a database. This data is used to calculate summary statistics, doing trend analysis, profiling, and debugging. But the DTOs are never actually retrieved back as DTOs from the database. So, although it looked promising initially (simplified the storing part), I think that O/R libraries are not suited for this scenario.

I did some web-research to find out what was being said as to when O/R libraries apply, but could not find any definite guidelines :(