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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s