This code:
public class WidgetPlatform
{
public Widget LeftmostWidget { get; set; }
public Widget RightmostWidget { get; set; }
public String GetWidgetNames()
{
return LeftmostWidget.Name + " " + RightmostWidget.Name;
}
}
doesn't contain any repetition worth worrying about, but it isn't particularly robust. Because the Widgets aren't null-checked, we're leaving an opening for a bug. We could do a null check, but that feels like work. Here's what I really want:
public class WidgetPlatform
{
[Required]
public Widget LeftmostWidget { get; set; }
[Required]
public Widget RightmostWidget { get; set; }
public String GetWidgetNames()
{
return LeftmostWidget.Name + " " + RightmostWidget.Name;
}
}
Ideally, it would cause a compile error (the best sort of error) if the object was instantiated without setting the Widgets, but that seems like a tall order. Is there a way to make this syntax work that at least throws an error on instantiation? There's a (relatively) obvious way to do it with reflection if all of the null-checked objects inherit from the same type, but without multiple inheritance that will get ugly pretty fast.