Wednesday, December 28, 2005

C# inheritance

The can be done in a number of programming methodologies. Now for something like the waterfall approach, inheritance is all planned out. With other more dynamic programming methodologies it's not so obvious, which methods should be overwritten and which shouldn't be.

Java is a language where if something is overwritten in the child the parent's version will automatically execute the child code's view. In C# the designers have decided to go for a literal approach when it comes to the language and inheritance. Much like C++ which is understandable considering it translated to IL as well as C# and VB.NET.
Take the following example. If the class car is derived from automobile and automobile has a method called TopSpeed. Now if the virtual and override keywords aren't used then if somebody assigns a car to type automobile the code for automobile will be executed when the method TopSpeed is executed.

I believe this is a case of bad language design. If a child class is to have a method with the same name as parent's method it should implicitly override the parent method. This begs the question if one is writing libraries in the infrastructure of a project that are to be used by developers, should all methods be made virtual? I believe if you wish to allow flexible extensibility by your programmers then a strong argument might be made for it.

Thursday, December 22, 2005

The importance of contextual metadata... (Part 1)

As the amount of information in the world increases so does the need to be able to better search for information. The two main tools that we have at our aid to improve this are statistical metrics and metadata (ignoring for things such as anchor weighting since I'm assuming a non-web enviornment and constant boosting from certain authors due to the fact I'm assuming that all information is important).

When I say statistical metrics I'm referring to things such as a search for "soy or linseed", now statitically one word will occur more often than the other, so it should probably rated more highly and a document containing both soy and linseed is more important than one containing just one of the search terms. etc...

The next aid to finding interesting results is metadata. Now pre-existing metadata for a document is nice to have but is often incorrect or inaccurate, so we have to judgements on how much weight we give to pre-existing metadata must be made usually on a case by case basis (referring to an inspection of the data to be searched over).

Next we have created metadata. This data helps to define things about the document it's self. For example people or places can be extracted. This allows us to drilldown on pre-existing values in searches. Other contextual information can be gathered from the text of the document, such as identifing a title of a document or a heading and making it more important.
A search for bush a gives us? A plant, a president, and a pro footballer. By recognising people we can limit those documents to a president and a pro footballer, by searching for bush inside the people metadata.

Attributes in .NET

Attributes were definitely a bit of a blank for me initially when it came to .NET. Why would you want to have metadata within your code? What would be the point of that? It turns out that it's not that silly, especially when we come to thinking about code in terms of reflection and runtime discovery.

Note: Java has the same abilities as .NET in this area though not as highly documented. http://www-128.ibm.com/developerworks/java/library/j-dyn0429/ explains how java byte code has attributes and that each function is simply an attribute and that custom attributes may exist. Though it appears at this time there doesn't appear to be any real way to utilise this information.

On metadata attributes in .NET it's possible to use these attributes to dynamically find items in classes at runtime. This is of course especially useful if you'd like to dynamically discover load classes at runtime. Think plugins...

First of all create a custom attribute. This is simply done deriving a class from the System.Attribute class. You can use the Attribute AttributeUsage to say that it's only valid for classes and interfaces.
Write a custom interface that uses your custom attribute for the interface declaration.
Implement your interface. It is now possible with reflection to find all instances of classes that implement that interface using reflection. Simply load the current assembly (Assembly.ExecutingAssembly) and get all the types. For each type attempt to get all instances of our custom attribute. If the attribute exists then we know that the class implements our custom interface. (Also check to make sure that the class isn't abstract or an interface that we you can actually create an instance of the class).

And now you have seen how reflection can be used to find and dynamically instantiate classes. This method is used (though slightly differently), to find webservice methods. You may also see http://www.xml-rpc.net/ this uses the same sort of reflection to know what methods to marshall on a website. So there you have it. Attributes and reflection.