Servergeek
Mickey Williams' weblog


Powered by Blogger Pro™

Friday, April 11, 2003

The Usual Pattern For... Creating a Strongly Typed Collection


Until the next version of the .NET Framework (with generics) is released, you might think that you're stuck with framework collections that are typed for elements of type object. Which opens obvious doorways where buglets can slip through. It doesn't have to be that way - here's some basic boilerplate code for assembling a strongly-typed collection. In this case, a collection of Foo:
using System;
using System.Collections;

namespace Servergeek.Collections
{
	[Serializable]
	public class FooCollection: CollectionBase
	{
		public int Add(Foo aFoo)
		{
			return InnerList.Add(aFoo);
		}

		public void Remove(Foo aFoo)
		{
			InnerList.Remove(aFoo);
		}

		public Foo this[int index]
		{
			get { return (Foo)InnerList[index]; }
		}

		public void CopyTo(Foo[] target, int index)
		{
			InnerList.CopyTo(target, index);
		}

		public bool Contains(Foo element)
		{
			return InnerList.Contains(element);
		}

		public int IndexOf(Foo element)
		{
			return InnerList.IndexOf(element);
		}

		public void Insert(int index, Foo aFoo)
		{
			InnerList.Insert(index, aFoo);
		}		
	}
}
Serializable, passes inspection with fxCop, works well with the BCL, exposes an indexer, and gives you a type-specific collection.

Wednesday, April 09, 2003

Implementing Designers for ASP.NET Custom Controls


Maybe I was just a bit clueless, but when I first started developing custom web controls, I spent a lot of cycles trying to figure out how designers work. Here's the boilerplate example for a custom designer. First, derive a designer class from ControlDesigner. This is more or less what most of my custom control designers look like:
public class MyCtrlDesigner: ControlDesigner
{
	internal DataModel GetDataModel()
	{
		DataModel dm = new DataModel();
		// Fill in data model here...
		return dm;
	}

	public override string GetDesignTimeHtml()
	{
		string result = null;
		MyCtrl theCtrl = Component as MyCtrl 
		if(theCtrl != null)
		{
			// Render a table with dummy values using the properties
			// from the control
			StringWriter stringWriter = new StringWriter();
			HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

			DataModel model = GetDataModel();

			MyCtrlView view = new MyCtrlView();
			view.Render(theCtrl, htmlWriter, model);
			
			htmlWriter.Flush();
			result = stringWriter.ToString();
			htmlWriter.Close();
			stringWriter.Close();
		}
		return result;
	}
}
The designer delegates all of the rendering to a view class, in this example, MyCtrlView. This class understands how to render the control when provided with an instance of the DataModel, a reference to the control, and an instance of HtmlTextWriter. This keeps the developer experience more in-sync with what gets rendered on the page. Anyhow, to enable the designer, you need to add an attribute to the main control class, like this:
[Designer(typeof(MyCtrlDesigner))]
public class MyCtrl : System.Web.UI.WebControls.WebControl, 
                            IPostBackDataHandler, IPostBackEventHandler, INamingContainer
{
	...
}

.NET Framework 1.1 Final is Available


It's here! The redistributable bits are here. As of a few minutes ago, you need to take care with links within the pages - they seem to point you to content that hasn't been updated since the beta.

Forcing a Runtime Version for ASP.NET Web Apps


Brad, guy of .NET, is asking how to force an ASP.NET app to pick a specific version of the runtime. You do this by specifying the ASP.NET ISAPI filter that services your pages. If you pick the 1.0 version, the 1.0 version of the runtime handles your pages - if you pick the 1.X version, well, you get the idea. An article on MSDN describes the details.

Tuesday, April 08, 2003

Rotor Community Site


The Rotor community site is online. [All things distributed] Yahoo!

Monday, April 07, 2003

CSP Online


Tony Hoare's most excellent book, Communicating Sequential Processes is online as a PDF here. Via LtU.

On Repaving...


Now Scoble really is turning into a developer - he had to repave his system, and spent more than a couple of hours getting his system back to par. I'm always loading new code and hardware on my systems, and I've just given up on expecting stability. If living with Alpha and Beta releases has taught me anything, it's that you should always save a checkpoint of your system's basic configuration. I use DeployCenter from PowerQuest - it takes me very little time to roll back to either a clean OS install or my basic configuration (XP, Word, etc.) It's brain-dead easy to use, reliable, quick, and stays the hell out of the way when I'm not using it. Very recommended.

Home