marcumka
marcumka

Reputation: 1695

DRYing up object instantiation in C#

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.

Upvotes: 1

Views: 412

Answers (3)

Eoin Campbell
Eoin Campbell

Reputation: 44268

Whats wrong with Constructors ?

public class WidgetPlatform
{
    public Widget LeftmostWidget { get; set; }
    public Widget RightmostWidget { get; set; }

    public WidgetPlatform()
    {
        this.LeftMostWidget = new Widget();
        this.RightMostWidget = new Widget();
    }

    public WidgetPlatform(Widget left, Widget right)
    {
        if(left == null || right == null)
            throw new ArgumentNullException("Eeep!");

        this.LeftMostWidget = left;
        this.RightMostWidget = right;
    }


    public String GetWidgetNames()
    {
        return LeftmostWidget.Name + " " + RightmostWidget.Name;
    }
}

Upvotes: 5

FlySwat
FlySwat

Reputation: 175583

public class WidgetPlatform
{
     public Widget LeftWidget
     {
         get;
         private set;
     }

     public Widget RightWidget
     {
         get;
         private set;
     }

     WidgetPlatForm(Widget w1, Widget w2)
     {
         if (w1 == null || w2 == null)
             throw new ArgumentException();

         this.LeftWidget = w1;
         this.RightWidget = w2;          
     }

     // Etc
}

Upvotes: 3

user25306
user25306

Reputation: 421

There is a school of thought that it is best to encapsulate your constructors, and it seems that it would be approperiate here. reference:

http://www.amazon.com/Emergent-Design-Evolutionary-Professional-Development/dp/0321509366

You might consider:

public class WidgetPlatform
    {
        /// <summary>
        /// Hide the constructor.
        /// </summary>
        private WidgetPlatform(Widget left, Widget right)
        {
            this.LeftmostWidget = left;
            this.RightmostWidget = right;
        }

        public Widget LeftmostWidget
        {
            get;
            private set;
        }

        public Widget RightmostWidget
        {
            get;
            private set;
        }

        public static WidgetPlatform GetInstance(Widget left, Widget right)
        {
            return new WidgetPlatform(left, right);
        }
    }

In GetInstance you can check for null, and decide what to do about it, or not ... you have lots of options at this point.

Hope that helps ...

Upvotes: 0

Related Questions