Exploring C#: A Generic Object Factory

In my ongoing exploration of the C# programming language (part 1), I came across some Delphi code I had written earlier that I needed to port to C#.

The problem: No class references in C#

The piece of code is inspired by the Factory pattern to produce objects derived from TItem with a given name. This is used to build objects at runtime according to a list of class names in a configuration file. The factory is pretty simple: to register classes with it, one just passes it a name and a class reference which it stores internally. When a new item is to be created, one passes the factory the class name, and the factory will look up the proper class to instantiate. The code for this factory is given below (I apologize for including it here as a picture, but I couldn’t get it to work as text while preserving the proper formatting).

This should be easy enough to port to C#. Or so I thought. As it turns out, C# does not have class references. So something like “class of TItem” as I did above, simply can’t be done. There are generics, of course, but there is no way to store the type parameter in a dictionary the way I did with the class reference in Delphi. TypeOf also doesn’t help much, because while it would allow me to store the Type, use reflection at runtime to look for a constructor on it and then call it, this process is slow and it doesn’t provide any of the compile time checks I would like to have in a strongly typed language like C#.

The solution: generics + delegates

A quick Bing search turned up this excellent Generic Object Factory implementation in C#. I won’t reproduce the code here, so please follow the link, check out the code and come back when you’re done. I’ll be waiting here… You’re back? Good.

When I first saw it, I was amazed at how using the power of generics, this factory is so much more versatile than my Delphi implementation. Via the Key and GeneralProduct type parameters, it can be “configured” to create objects of any type and using not just strings, but maybe an enum or an integer as the key.

As I said, there are no class references in C#, but one doesn’t really need the class reference to begin with, just a way of calling the constructor on that class. This is where C#’s delegates come in handy, as the CreateFunctor type and the Creator method in that code is really just a pseudo-constructor. The type to be created is passed in the SpecificProduct type parameter. And unlike the type parameter, a reference to the pseudo-constructor can be saved in a dictionary.

This factory beautifully combines generic types, generic methods, type constraints and delegates to create an extremely powerful factory that can be used with great ease for all kinds of use-cases. It seems so useful, I wonder why such a class hasn’t been incorporated directly into the .NET framework. It’s things like this I miss the most when I have to go back to Delphi 2006 where I still do most of my coding these days.


Looking forward to programming in C#

For the first couple of years I coded almost exclusively in Delphi with a little Python on the side. And while I still love Delphi, it has not evolved as quickly as other languages in recent years and one has to wonder where Delphi is headed. The most pressing issue is probably the lack of a 64-Bit compiler which has been on the roadmap for years, but has yet to be released in a final form.

C# Books I had been following the development of C# back when I was in college, but the language and the .NET platform in general have made a giant leap forward since my first encounter with C#. There is just so much stuff to learn, it’s a daunting task, but also really exciting.

I find books to be indispensable for my C# education. Pictured above are a couple I personally own. Note that some titles in the picture are still about C# 3.0, while 4.0 is the current version:

  • Concurrent Programming in Windows: A lot of my projects are multithreaded to put the dozens of processor cores modern servers have to better use. This book is extremely dense, but it contains everything one needs to know about multithreaded programming on Windows.
  • Essential C# 4.0: This book gives a really good overview of the C# programming language, especially if you already know another language, this book should be the only one you need read to get up to speed in general C# programming.
  • The C# Programming Language: I bought this book figuring that what better C# book could there be than one written by Anders Hejlsberg, the language’s chief designer. However, this book should really be called “The C# Language Specification” because it is just that; a very technical specification of the various features of the language. It’s great as a reference book, though, if you just need to quickly look up a certain language feature.
  • Framework Design Guidelines: This book contains some great insights into properly designing APIs that are intuitive to use and fit in with the rest of the .NET framework. This book should come in handy for designing internally used libraries. Many guidelines and recommendations presented in the book are not necessarily .NET specific, but could be applied to other object-oriented programming languages as well.
  • Advanced Windows Debugging: The Debugging Tools for Windows are an extremely powerful set of tools for investigating bugs in one’s Windows applications. Until I got the book, I have been hesitant to use them though, because for all their power, these tools have a very steep learning curve. This book is like the missing manual, introducing the most important commands one has to familiar with to get the most out of the debugging tools. this book isn’t .NET specific, though, but may be even more important for native Win32 developers.
  • Windows Internals: This book contains all there is to know about the internal workings of Microsoft Windows. It’s the perfect companion book to Advanced Windows Debugging, because a lot of the details disclosed by the debugger only make sense when one knows how Windows works internally. This book should also be required reading for any Windows system administrator.

While this is already a lot of reading material, I still have a couple of more books on my wish list that I think I should have read before getting serious with C#:

  • Windows Presentation Foundation 4 Unleashed: This book will hopefully show me how to give my applications gorgeous and functional user interfaces using Windows Presentation Foundation, the graphical subsystem of Windows.
  • Essential LINQ: LINQ or Language Integrated Query looks like a very convenient way to perform SQL-like query operations against object-lists, datasets or XML documents in .NET. This is something that is usually a lot of work to do manually, so I’m eager to learn how I could use LINQ to streamline my code.
  • Essential Windows Communication Foundation: WCF is another one of the major building blocks introduced in .NET 3.0 that should make developing service-oriented architectures (SOA) easier. While this is not something I am currently doing, may be there are scenarios where SOA would be useful.
  • Windows Workflow Foundation: WWF along WCF and WPF is the third “foundation” technology in .NET. I think a lot data-driven user applications have an implicit workflow where data is worked on through different activities. I’m curious to see how WWF could be used to keep developers from having to reinvent the wheel every time.