Wednesday, September 20, 2006

SysAdmin: When your computer becomes forgetful

BusinessSometimes your computer crashes without reason. It happens at any time, for no particular reason.
Other times you’re trying to install a new OS on a brand new PC and at some point, it fails, reboot itself or just hangs.

A couple years ago I had this really depressing experience with a brand new system I was building. All the components were newly bought, but installing the OS (a Linux distro) used to fail almost at the end of the process.
No matter how many times I tried, I could never get to the end of it.
By clever subterfuge I managed to get it installed only to have it crash on a regular basis.
It was unstable, unreliable and after two days wasted banging my head against the walls, I gave up…

Memtest86 Well, no for too long. A flash of inspiration came to me and I popped in the install CD and ran the only utility that was available at the prompt: memtest86.
That simple tool is a godsend (if I may appropriate the word from the believers. I promise to give it back). After running its various memory tests for 10 minutes it reported errors in one of the RAM sticks installed on the motherboard.
All that aggravation for a puny bit that was not remembering its state…
I promptly returned the RAM and tested the new one for a few hours until I was confident there was no issue with its chips and went my merry way to install the OS I so desperately needed. All went without a hitch.

So my advice is: go to the memtest86 website. Burn the bootable ISO and test your PC from time to time, especially if you have strange intermittent issues that you can’t pin down to a simple software problem.
You’d be amazed at the number of times I had to ditch a stick of RAM… memtest86 saved by sanity countless times.
By the way, consider donating a little bit if it helped you too. That’s always cheaper than a session with a shrink…

Add comment   |   Filed under :  Hardware, Software, sysadmin

Monday, September 18, 2006

Software: Cheap Microsoft Licenses

BusinessI’m not particularly pro -Microsoft but I’m not against it either.
I love Linux, got my RHCE (Red Hat Certified Engineer) a bit more than a year ago and I love Open Source, Linux and all things GNU.

The only thing I really dislike about Microsoft is its marketing, its pricing, its Genuine (Dis)Advantage that nags me every time I need to install something and its lack of openness when it comes to inter-operability with other competing implementations (here I’m particularly thinking about its network protocols that the Samba team tries to decipher and re-implement as an Open Source platform).
On the other hand, Microsoft is made of great programmers, great minds that you can watch on Channel 9 and read on their insightful blogs.

Microsoft is really (I mean REALLY) pro-developer: they understand that the Operating System alone is nothing without lots of applications sitting on top of it, and they offer developers a lot of goodies.
One such useful programme is +Microsoft Empower for ISV_. It’s a simple membership that allows a small software company (ISV meaning Independent Software Developer) to own a number of licenses for its internal use at a fraction of the price it would normally cost.
What you get is pretty wide for a small business: 5 licenses for Windows XP (whatever version), 5 licenses for Microsoft Office, 1 license for Windows 2003 Server and Exchange, SQL Server, SharePoint Portal, a MSDN Premium Subscription that covers almost anything else, including 5 licenses for Visual Studio 2005 Pro.
You also get access to MSDN downloads, beta software and tons of libraries, SDK, etc. A MSDN subscription alone is about US$2000…
The Empower ISV programme is quite cheap and depend on the country you are in.
I paid mine HK$4,260 about US$530.

To get all this you need to register as a Microsoft Partner (that’s free), then apply for the Empower for ISV programme by making a promise to release a commercial software within 2 year at most. You need to give some details and pay your due. After a few days, you get confirmation if your application is accepted or not.
As far as I know, you need to be a company and have a company website, but that may not be mandatory in all regions.
The software you get is the normal US version plus whatever local version there are for your region. My bunch of DVD came in Japanese, Cantonese, Mandarin, some in Korea and Thai too.
At least, the English version is supplied. For some software, you also get the multilingual version that include European languages as well.
You manage your licenses by login under your MSDN account.

Really, it’s a nice touch from Microsoft to give us poor developers access to all this for such a reasonable price. I can only encourage other small software companies and independent developers to do the same.

Add comment   |   Filed under :  Business, Programming, Software

Tuesday, June 13, 2006

.Net: The limits of using Reflection

technology02.pngReflection is a hugely useful technology: it allows you to get inside objects and get their intimate details, modify their values and even rewrite part of their code.
Today I wanted to build a simple treeview control that would display the hierarchical structure of an arbitrary object by going through its properties and building a tree-like structure. Useful to visualise the internals of an object and see changes at runtime.

Using PropertyInfo you get enough information about a particular property to decide if you want to display it or recurse into it if it’s a complex type that contain other properties.

The only big issue I faced was when dealing with indexers.

The issue with Indexers

Indexers are defined in code like that:
We’ve defined an overloaded indexer: one takes a DateTime, the other just a string representation of a date.

We could use them like that for instance:
Now, let’s say we want to print all property names and their values for an arbitrary object.
Let’s try on our myDay intance:
So, that part is easy: we iterate through all properties of myDay and print their Name and they value.
Problem is, when we reach the indexer property, we get a TargetParameterCountException on line 8.

We need to detect the indexer before attempting to get its value.
To do that, we need to modify our foreach loop to:
Here we attempt to get the indexer parameters for every property. If we’re dealing with an indexer, the parcoll array will contain a list of ParameterInfo related to that particular indexer.

So what really is this parcoll and its ParameterInfos?

A ParameterInfo contain information about the parameter of an indexer property.
The parcoll array will contain one entry for each overloaded indexer.
In our concrete example this is what we get:

  • pi.Name will be Items, this is standard for all indexer properties.
  • pi.PropertyType is the type of the returned class of our indexer, in our case, a System.String.
  • parcoll.Length will be equal to 2 because we have 2 overloaded indexers.
  • parcoll[0].Name will be date.
  • parcoll[1].Name will be datestr.
  • parcoll[0].ParameterType will be System.DateTime.
  • parcoll[1].ParameterType System.String.
  • parcoll[0].Member and parcoll[1].Member are in fact references to pi itself

That’s a lot of nice information about our indexer, but what if we want to get a value from it? Consider the definition for GetValue:
That second parameter is used to get a value from the indexer.

So, how do we use it?

If you know valid keys that you can feed to the indexer, then you just do that (assuming that pi is the PropertyInfo for the indexer).
Or, if you prefer to use a DateTime: But what if you don’t know anything about the values contained by the indexer?
Well, unless you use attributes to decorate the indexer or the underlying storage field with specific custom information, you’re basically screwed.

To understand why, just consider how the indexer is implemented:
When you pass it a DateTime or a string as a parameter, it just passes it on to the getter/setter as the value.
What is done with that is up to the implementer.
If you want to maintain a collection of some kind, then you need to use private fields to hold the keys and their values.
.Net doesn’t store the values passed to the indexer or the values returned by it anywhere.

Consider how .Net doesn’t give you any means of iterating through the keys and values of an indexer.
You can’t do a for(;;) or a foreach() loop, you can’t even know how many items are maintained through the indexer by directly questioning it.
So, it certainly is normal that you cannot get anything out of the indexer without knowing exactly how to access it.

The Observer Effect

In many fields of science, the act of measuring something, whether it’s an electronic circuit or a psychological experiment, can actually influence the observed phenomenon.
This is unsurprisingly called the Observer Effect.

In .Net, Reflecting on object can be very much the same.

Consider the use of Reflection when it comes to getting information about an object:
You use metadata describing the object to get more meta data information about its members You can use that meta data to direcly query the state of an object.

If you limit yourself to getting metada, then no harm is done: you’re not querying the object instance itself but the metadata about its class.

If you start querying an actual object instance for its values then you are actively interacting with it.
In most cases, if you’re just getting the value of Fields or Properties defined as follow, then it’s fine:
Using reflection, we can get the values of both _meeting and Meeting without any side-effect to the object.

Now consider this:
Now what we’ve got here is a case of lazy loading or lazy initialization.
The consequence is that by querying the Collection property we’re actually creating it, with a potential for a lot of side-effects, like the change of other properties, here maybe related to opening the database connection for instance.

So, when getting values of the properties of an object instance, we can actually completely change its state.
Whether this is a concern or not really depends on what you’re trying to achieve: you may not be able to achieve being a passive observer using Reflection unless you know about the construction of the particular class you’re dealing with.
Using Attributes, you could give hints about which Property not to query and which Field to get instead. In our case, we could add a custom attribute like [ImplementedBy("_collection")] to our Collection property for instance.

If you want to do something generic, then you’re out of luck it and have to deal with that fact.
One possible way to minimize the effect would be to not query Properties that only have a getter and no setter, but that’s not a guarantee that all properties that implement a getter/setter are going to play nice.
Then there is also the -probably rarer- case of the Property that only has a setter.
Then there is also the case of the property getter that has some in-build security and will return different things depending on who’s calling…

All this seems pretty obvious once you understand the whole picture but it’s easy to get so impressed and dazzled by the power of Reflection that you don’t realise its natural limits.

7 comments   |   Filed under :  .Net, Programming

Sunday, June 11, 2006

WordPress: Formating and colouring Code

technology01.pngWordPress is pretty good, but it comes with no code formatting tool, yet colouring facilities. I like the simplicity of dp.SyntaxHighlighter for displaying source code in web pages: it works with major browsers and degrades fairly well.

Its particularity is that is does its painting magic on the client side. This can be a drawback in some instances, where the client browser has JavaScript disabled for instance, but since the code to paint is located within <textarea> tags, it will still display no matter what.

Configuring _dp.SyntaxHighlighter_to work in WordPress is pretty easy: just add the relevant code in your WordPress templates for header and footer according to the instructions found on the website and you’re done.

The issue though is making it possible to insert code when writing a post.
First of all, I doubt that there is any elegant way to make one of the rich HTML editors work well, so you’ll have to go back to basics and edit in HTML (WordPress at least hides the <br> and <p> tags so its’ not too bad).
Then there is the issue of WP adding the <br> and <p> to replace linefeeds, and this is where it becomes annoying: if you insert <textarea> tags, everything in it will have the extraneous formatting code that will display verbatim on screen, sprinkling your nice source code with ugly html tags.

The solution I found was to use a small WordPress plug-in called codeautoescape and available from:
http://priyadi.net/archives/2005/09/27/wordpress-plugin-code-autoescape/

I then modified it a bit to take care of the <textarea> tag and the fact that we needed to take the attributes of the tag into account (dp.SyntaxHighlighter uses the class attribute to configure options for how to display the block of code).

You can download the modified version of the script: codeautoescape

To install it, just right-click and download from the link, rename it to codeautoescapte.php then install it in your /wp-content/plugins folder and activate it within the Plugins menu of WordPress.


Some examples of dp.SyntaxHighlighter in action:

C#

JavaScript

XML / HTML

PHP

SQL

7 comments   |   Filed under :  Web Design

Friday, June 9, 2006

XPO: eXpress Persistent Objects

technology02.pngXPO is an Object Relational Mapping .Net product from Developer Express, a cool company designing cool tools. It’s a programming component whose job is to abstract access to database while allowing the developer to concentrate on a simple object-oriented interface instead.

Continue Reading 1 comment   |   Filed under :  .Net, Programming, XPO

Next Posts Previous Posts


about

This is a simple technical weblog where I dump thoughts and experiences from my computer-related world.
It is mostly focused on software development but I also have wider interests and dabble in architecture, business and system administration.

Most Recent Posts

Categories

Links

Feeds