18 December 2007

Programming .NET Components

by mo

Programming .NET Components, 2nd Edition by Juval Lowy
Read more about this title…

This was a pretty sweet read, I think this along with CLR via C# work well with one another. This book goes into a little more depth on threading and issues with concurrency. As well the chapters on remoting are long and full of tonnes of information. Way more then this guy could absorb after an initial read. I have so much to learn about remoting.
This book starts right fr gooom the beginning. What is a component?
“.. a component is a .NET class. For example, this is a .NET component:

public class MyClass {
  public string GetMessage() {
    return "Hello";

I think one of my favorite chapters in this book was on “Interface-Based Programming” and talks about the importance of separating interface from implementation.

“Interface Factoring: When you factor an interface, always think in terms of reusable elements. In a component-oriented application, the basic unit of reuse is the interface.”

“… proper interface-factoring results in more specialized, loosely coupled, fine-tuned, and reusable interfaces, and subsequently, those benefits apply to the system as well. In general, interface factoring results in interfaces with fewer members.”

Another benefit, in my opinion, to interfaces is for learning a system. By checking out the interfaces defined in a subsystem you can get a flavor for what services are offered by each component. It’s much easier to look at the interface contract to see the operations it performs then having to scan through concrete types. I equate this to header files in C. If I look at the header file I can see what operations are available from each library. I don’t need to know how it was implemented, just where to find it.

“Windows Forms base classes make extensive use of ISynchronizeInvoke. The control class relies on underlying Windows messages and a message-processing loop (the message pump) to process them. The message loop must have thread affinity, because messages to a window are delivered only to the thread that created that window.”

Juval shares many different helper classes for getting around some of the more trickier areas of .NET development, like threading, events, remoting and even how to use a mutex to only allow a single instance of a class to run.

internal static class Program {
  private static void Main() {
    if (IsFirstInstance()) {
      Application.ApplicationExit += OnApplicationExit();
      Application.Run(new Form1());

  private static bool IsFirstInstance() {
    _mutex = new Mutex(false, Assembly.GetEntryAssembly().FullName);
    bool owned = _mutex.WaitOne(TimeSpan.Zero, false);
    return owned;

  private static EventHandler OnApplicationExit() {
    return delegate {

  private static Mutex _mutex;