May 27, 2011

Programming - Rule #1 follow your data from the UI to the Database and back

It can save you a lot of time :).
I had to fix a bug with a value not updating on the UI.
I attached with the debugger and followed the code to the Save() method of the manager.
Then I started looking for some problems in the database (asynchronous triggers failing etc.).

I wasn't quite sure where the problem is for a while.
Then I decided to hit F11 on the Save() method of the manager with the hope to see what query it was generating.

Guess what?
It wasn't the query but the fact that the Manager was overriding the Base Manager Save() method, as it needs to convert a DTO object to DataObject in order to save it.
Well I didn't even need to read through all the properties to know for sure that the property that wasn't updating was missing in the list of assignments in the convert method (in order to defend myself I would say that 99.9% of our managers aren't overriding save methods, it is very unlikely so I didn't considered that to be the problem).

Happy coding and remember Assumptions are your worst enemy :). They are the things that waste most of your time.

May 26, 2011

Serializing object to JSON returns empty string?

Consider the following code:

string JSONObject = string.Empty;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(this.GetType());
using (MemoryStream ms = new MemoryStream())
{
    serializer.WriteObject(ms, this);
    // Donchev - we need to reset because the write has gone to the end of the stream
    // and the StreamReader.ReadToEnd() won't seek to the beginning of the stream again.
    using (StreamReader sr = new StreamReader(ms))
    {
        JSONObject = sr.ReadToEnd();
        sr.Close();
        sr.Dispose();
    }
    ms.Close();
    ms.Dispose();
}
return JSONObject;

It seems perfectly correct but have a few problems.

And the first one is very important – it is based on wrong assumption and it is returning empty string because of that.

I will let you tackle it for a while to see if you can find where the problem is.

Ready?

If not – I’ll tell you – the problem is that when I wrote this code, I was expecting the ReadToEnd() method of the StreamReader class to auto reset the stream to its beginning, it sounds for some reason to me that this method it is saying - “Read the whole thing.”.

Unfortunately this assumption evaluated to false.

The method actually says - “Read from where you are to the end of the thing.”.

So before calling JSONObject = sr.ReadToEnd();

I needed to simply call:

ms.Seek(0, SeekOrigin.Begin);

In order to reset the pointer to the beginning of the stream.

Voilla!

The other question I asked myself is why the pointer is at the end of the stream (after the string is empty and not some portion of the stream, it should be at the end).

The answer – the serializer.WriteObject() method is probably not resetting it to be at the beginning after it completes its job.

Not quite sure who is responsible for resetting the stream – the caller or the callee, we can have a long discussion about this.

Anyway – just wanted to share this with you. I know it is very old thing but I am trying to write for much of the things that made an impression on me.

May 23, 2011

Silverlight 4 - catching uncaught exceptions from external components

I was playing with an external component in Silverlight 4 and got an unhandled exception while debugging.

The problem was that I wasn't able to see what's going on because the debugger won't show anything (it doesn't have the source of the exception) and because the exception was triggered internally in event in the component trying to catch it to see what is the exception had no effect as well.

Then I opened the App.xaml file and found the following:

if (!System.Diagnostics.Debugger.IsAttached)

Pretty neat, huh? :).
The method signature was as following:
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

All I needed to do now was to put a breakpoint on the if statement and then expect the ApplicationUnhandledExceptionEventArgs.ExceptionObject to see what the exception was.

Thought you may have some hard times debugging problems with Silverlight.