Empty Type Parameters

The aggregated Event and DelegateCommand from the Composite Application Guidance (aka Prism) require a type specification for their parameter or payload. This is sometimes unnecessary when no information is published, such as raising an event to indicate that something has finished loading. One approach is to simply specify object as the type parameter and use null as the value.

public class RecordsLoadedEvent : CompositePresentationEvent<object> {}
_eventAggregator
	.GetEvent<recordsloadedevent>()
	.Publish(null);

However, I’m a big believer of making your intentions obvious using code. Specifying an object as the parameter hardly makes it clear that it’s not used. Any other developer using it would have to dig through the code to check that the object supplied doesn’t have any significance.

We see another instance of this problem in the .NET Framework itself. By convention every event is fired with a sender and an argument. The event argument is often not used and instead of publishing null, the framework provides an Empty value – EventArgs.Empty.

Generalising that idea, we can create an Empty type with a constant value.

public class Empty
{
	private static readonly Empty _value = new Empty();
	
	public static Empty Value
	{
		get { return _value; }
	}
	
	private Empty()
	{
	}
}

Using this for our aggregated events and commands now makes it obnoxiously clear to consumers that it’s not being used.

public class RecordsLoadedEvent : CompositePresentationEvent<empty> {}
_eventAggregator
	.GetEvent<recordsloadedevent>()
	.Publish(Empty.Value);