Use Token Handle Resolution API to get the Metadata for Reflection in .NET 2.0

One of the most common uses of reflection is in the use of plug-ins, such as toolbars or hosting third party plug-in functionality. A developer I worked with recently was unaware of the appropriate uses of reflection (let’s call him Bugsy!). I’d like to direct him to the words of Joel Pobar, who you might say is an expert on this topic (Joel is a Program Manager on the common language runtime (CLR) team at Microsoft, at least he was till last week!):

When Should You Use Reflection?
You should always think carefully about how you’re using reflection. Using reflection occasionally without enforcing strict performance criteria is probably fine. If reflection APIs are only invoked when you’re calling the part of your app that loads and invokes a third-party plug-in, then the cost should be reasonable. However, if your scenario involves a high-volume ASP.NET Web site that requires good throughput and response times, and if it makes significant use of the heavy reflection APIs in your “fast path” (the code in the application that must run very fast and is used repeatedly), you should really consider an architectural review to decide if you’ve made the right decisions on your use of reflection.

Where was he using reflection? Between the business rule and data access layers in an ASP.NET 2.0 financial application! (and no, he seemingly had not heard of code generation, despite several attempts to enlighten him). That architectural review sounds appropriate…

Relative performance of Invocation Mechanisms (image from MSDN article)

Scott Hanselman has a Hanselminutes webcast on the subject here.

How can you improve the speed of reflection based calls? J.D. Meier’s post details the use and the performance benefits of the new .NET 2.0 reflection API:

“RuntimeMethodHandle works approximately twice faster than compared to equivalent GetXxx API call, if the MemberInfo is not present in the back-end .NET cache”

To round off this post, here’s yet another free resource from Microsoft “Improving .NET Application Performance and Scalability” (all 1150 pages of it!)

“Information is segmented by roles, including architects, developers, testers, and administrators, to make it more relevant and actionable. This guide provides processes and actionable steps for modeling performance, measuring, testing, and tuning your applications.”

By complete coincidence, I’ve been talking to a colleague about .NET performance tuning and I’m hoping to put an article together in the near future, so stay tuned.

Indexing for Developers

Greg Linwood gave an excellent presentation of his SQL Server “Indexing for Developers” talk at the Perth .NET User Group last night. He covered the use of clustered and non-clustered indexes and how they actually work ‘under the hood’.

I believe that it is important for developers and architects to understand these topics, as databases are often created without regard to how they will be used in production, with the effect that production systems do not perform as well as they could do.

Greg is presenting another talk this evening (details here) aimed at database administrators (but also relevant to developers) on managing indexes. If you work with SQL Server, I strongly recommend you attend these talks when Greg presents them at a local User Group near you.

RSSBus

If you don’t receive the MSDN Flash email, you may not have come across RSSBus. It allows creation of RSS feeds from a variety of sources, such as databases, spreadsheets, e-mails and folder content. It looks very interesting and I can think of a number of uses within an enterprise.

Another .NET Developer Resource

Fancy a few nuggets, without the secret herbs and spices? I stumbled across the MSDN Nuggets site at Microsoft UK. It has been created by the Microsoft UK Developer and Platform group, and looks interesting. As a nice touch, the monthly archives are downloadable as a single zip file (why can’t you do this with webcasts by track on the main Microsoft site? Please?).

Don’t have the time to read a 10-page how-to article or watch a full length
webcast? Try an MSDN Nugget, a webcast that takes you step-by-step to
discovering new functionality or exploring a hot developer topic, all in 10-15
minutes. View them online now or download for later reference.

Mike Ormond‘s blog has links to online/offline viewing tools here.

The Broken Window Effect

Why should you fix a code problem as soon as you find it? If things break, a classic example are unit tests, and they do not get fixed, then other breaks appear and get left broken. This effect has a tendency to escalate to the point where people ignore unit tests, eliminating the benefit that units test provide.

The effect was coined as applied to vacant buildings. As soon as a single window is left broken, others will follow and the effect actually spreads out from the building into the neighbourhood. It is easy to imagine that this effect is real; just apply it to the house you live in. If you leave something that requires attention, you are more likely to leave other things as well (I have experienced this effect first hand!).

This principle of Fixing Broken Windows was documented in the US, and has its critics due to the fact that in society it is hard to eliminate/identify all other contributing factors.

Andrew Hunt and David Thomas used “Fixing Broken Windows” as a metaphor for avoiding software entropy in software development in their book, The Pragmatic Programmer. Next time you are coding and you see something that is broken, maybe you should err on the side of caution and fix it?

(Not to be confused with the Parable of the Broken Window)

BindingList

Patrick Altman reminded me of a question I raised on the [aus-dotnet] list several weeks ago, regarding inheriting from BindingList rather than List. BindingList is designed for data binding and data changed event notifications. If you derive from List and you want to be notified when someone changes the collection, you will have to implement this yourself. David Kean has an excellent blog post on this subject and why FxCop encourages exposing Collection over List here.

The reason I mention this is because at a company where I was working recently, the programmer responsible for writing framework code was not aware of this. Thanks to David Kean, Bill McCarthy, Paul Stovell and Mitch Denny for their input.

What is a Lifehack?

Lifehack.org is one of those places you go when either you want to think about something other than programming (perish the thought!), or examine and possibly improve some area of your life. Creating Hardworking Idiots caught my eye:

Today’s fast-paced, macho style of organizational culture creates, and then
fosters, the hardworking idiot. Indeed, I think it takes a great many sound,
useful, hardworking, and clever people and turns them into idiots by denying
them the time or the opportunity to think or use their brains.

Elementary, Dr. Solomon!

Mark Russinovich has understandably not blogged for a while, as I daresay he was a little busy with his recently taken role at Microsoft. He breaks this drought in Holmes-esque fashion with this excellent post sleuthing his way through the labyrinthine windows internals using Filemon. The article mentions the simple but very effective technique of loading lengthy traces into Excel to discard time and sequence related columns and using Windiff to quickly find answers.

If I had to name a Windows programmer who has had a huge impact in the developer community, it would be Mark and the essential tools from SysInternals.

Congratulations, Rob!

Congratulations to Rob Farley on being awarded his MVP. Rob is active in the newsgroups, his local Adelaide based SQL Server user group and developer community and is always willing to help out someone with a problem (I’ve probably missed a stack of other things). Great news Rob, it’s well deserved.