As a bit of fun, some of you will know that I am currently working on a Scalextric track designer. Part of the reason is because I wanted to experiment with a few aspects of the .Net framework, another part is that I wanted a very modular application that I could use for evaluating new technologies and methods as they emerge without having to code all of the supporting parts. I will write more about the various assemblies that make up the track designer in a future post but for now I want to concentrate on my take of an Object Builder.
What is an Object Builder?
In the simplest implementation, an object builder is just another way of creating object instances. There are two common patterns that are used throughout .Net that you should already be familiar with. The first is simply using the object's constructor to create a new instance.
StringBuider myLongString = new StringBuilder();
The second is factory methods.
Guid id = Guid.NewGuid();
If you don't understand either of these, I strongly recommend that you buy a book on programming. You may also want to google. Alot. What I am going to write about is how we can create objects using another method. "Why?" you ask, well if we are creating objects in a manner that does not need to affect the object's implementation then we can add behaviour as our application demands. My object builder provides the following additional functionality:
-
Type Substitution (The consumer can ask for an interface and get an implementation of that interface via custom configuration sections).
-
Singleton enforcement (The object builder ensures that there is only ever one instance of an object).
-
Dependency Injection (The object builder can set property values and pass parameters to the constructor (or the factory method) of the object).
I will cover how we can implement this functionality in future posts, however for now we will just deal with creating instances.
Usage
It is important that usage of the object builder does not involve anything that is too convoluted. One line is more than enough ;-). For my object builder I am aiming for the following code:
static void Main(string[] args)
{
MyClass myClass = Builder.Build<MyClass>();
Console.WriteLine("Name: {0}, Number: {1}.", myClass.Name, myClass.Number);
myClass = Builder.Build<MyClass>("dave", 23);
Console.WriteLine("Name: {0}, Number: {1}.", myClass.Name, myClass.Number);
Console.ReadLine();
}
As you can see, it is only slightly more complicated than a normal factory method in that we need to specify the type of object that we would like to create.
The Code
Please bear in mind that the code examples that follow are designed to be extended - they are completely over the top for this simple demonstration. First things first - we need a method to create an instance of an object. This is actually really simple - we can use the System.Activator class.
internal static class InstanceManufacturer<T>
{
internal static T CreateInstance(params object[] args)
{
//work out what args we need to pass
if ((args == null) || (args.Length == 0))
{
//create an instance with no params
return (T)System.Activator.CreateInstance(typeof(T));
}
else
{
//create an instance with params
return (T)System.Activator.CreateInstance(typeof(T), args);
}
}
}
The next thing we need to do is to expose this method.
public static class Builder
{
public static T Build<T>(params object[] args)
{
return InstanceManufacturer<T>.CreateInstance(args);
}
}
It really is that simple. Now invoking the usage example will give us the following output:
Thats all folks - next time I will show how you can add support for factory methods.
Source code: ObjectBuilder_Intro.zip (29.31 kb)
128891f9-5179-4199-82f3-65800b562f79|0|.0