Archive for the ‘.NET’ Category

Patterns – The Template Pattern

August 23, 2006

So this is the first in a series of posts that explain the patterns I use in my day-to-day work. Probably the most common is the template pattern. The purpose of this pattern is to control the way in which the inheritors of your class change its behaviour.

Take for example the following class:

	public class Foo
	{
		public virtual void DoStuff()
		{
			ThisMethodMustBeCalledFirst();
			// Stuff gets done here
		}
		
		private void ThisMethodMustBeCalledFirst()
		{
			// Important initalisation stuff here.
		}
	}

Now imagine that someone comes along and inherits from your lovingly crafted class:

	public class Bar
	{
		public override void DoStuff()
		{
			//New stuff gets done here
		}
	}

It is entirely possible that the user of the Bar class will find that the DoStuff method falls over, because ThisMethodMustBeCalledFirst has not been called. Now this could be solved by putting something like this in your documentation:

It is very important that ThisMethodMustBeCalledFirst is called when overriding DoStuff.

A better way is to make DoStuff() a template method:

	public class Foo
	{
		public void DoStuff()
		{
			ThisMethodMustBeCalledFirst();
			OnDoStuff()
		}
		
		protected virtual void OnDoStuff()
		{
			// Stuff gets done here
		}
		
		private void ThisMethodMustBeCalledFirst()
		{
			// Important initialisation stuff here.
		}
	}

By making DoStuff non-virtual and making it now call the virtual OnDoStuff it is now impossible for an inheritor to change the behaviour of DoStuff without ThisMethodMustBeCalledFirst being called!

So how does this apply to my day-job? Well, the framework I’m working on for the future development of tradesimple will have a base class that will contain all the functionality that is common across all processors. Developers of new processors will override an “OnProcess” method which will be called by the base class as part of a template “Process” method. Not only will this allow us to ensure that certain steps are always followed inside that Process method, but that will also enable us to debug problems more easily by dropping in a debug version of the base class. The Process method inside this base class can do whatever it likes (logging inputs for example) without fear that a badly behaved derived class will override it.

More Resources

  • Obviously there’s the GOF book. Most, if not all the patterns I describe will be in this must-read book
  • Also, the wikipedia article I linked to above has some nice examples.

System.Messaging, COM objects and PropertyBags

August 17, 2006

I’m posting this pretty much as a reminder to myself.

There is a bug in VB6 which means that when you try to write a persistable VB6 COM object to an MSMQ queue using .NET’s System.Messaging then it fails with error 0x800A02E0:

Cannot save an uninitialized class. You must use the global InitProperties method to initialize the class, or load the class from a PropertyBag before trying to save it.

The workaround is to call the InitNew method on the IPersistStreamInit interface of the COM object. In .NET however this is easier said than done. The following goo is needed:


using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

[ComVisible(true)]
[ComImport]
[Guid("7FD52380-4E07-101B-AE2D-08002B2EC713")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPersistStreamInit
{
    // IPersist interface 
    void GetClassID(ref Guid pClassID);

    [PreserveSig]
    int IsDirty();
    [PreserveSig]
    int Load(IStream pstm);
    [PreserveSig]
    int Save(IStream pstm, bool fClearDirty);
    [PreserveSig]
    int GetSizeMax([InAttribute(), Out(), MarshalAs(UnmanagedType.U8)] 
		ref long pcbSize);
    [PreserveSig]
    int InitNew();
}

You can then go ahead and cast your object to IPersistStreamInit and call the InitNew method. The object will now persist to the queue with no problems (remember to set up your Message to use the ActiveXFormatter though).

Source Control How-to

August 17, 2006

A great series of articles on the hows and whys of source control.

read more | digg story