Alan2
Alan2

Reputation: 24562

Can I shorten or simplify having many constructors in C#?

I got some advice about constructors but now I have many. Can I simplify these with a function or some other way? Here's my class?

public class ContentViewModel
    {
        public ContentViewModel() { }
        public ContentViewModel(Content content)
        {
            this.Content = content;
        }
        public ContentViewModel(string pk)
        {
            this.Content = new Content();
            this.Content.PartitionKey = pk;
            this.Content.Created = DateTime.Now;
        }
        public ContentViewModel(string pk, string rk)
        {
            this.Content = new Content();
            this.Content.PartitionKey = pk;
            this.Content.RowKey = rk;
            this.Content.Created = DateTime.Now;
        }
        public ContentViewModel(string pk, string rk, string user)
        {
            this.Content = new Content();
            this.Content.PartitionKey = pk;
            this.Content.RowKey = rk;
            this.Content.Created = DateTime.Now;
            this.Content.CreatedBy = user;
        }
        public Content Content { get; set; }
        public bool UseRowKey { 
            get {
                return this.Content.PartitionKey.Substring(2, 2) == "05" ||
                       this.Content.PartitionKey.Substring(2, 2) == "06";
            }
        }
        public string TempRowKey { get; set; }

    }

I'm using c# 4. Someone said about named and optional arguments. Will that help me?

Upvotes: 1

Views: 337

Answers (4)

Marco
Marco

Reputation: 57573

Try this:

    public ContentViewModel() { }
    public ContentViewModel(Content content)
    {
        this.Content = content;
    }
    public ContentViewModel(string pk): this(new Content)
    {
        this.Content.PartitionKey = pk;
        this.Content.Created = DateTime.Now;
    }
    public ContentViewModel(string pk, string rk): this(pk)
    {
        this.Content.RowKey = rk;
    }
    public ContentViewModel(string pk, string rk, string user): this(pk, rk)
    {
        this.Content.CreatedBy = user;
    }

or you could try:

    public ContentViewModel(Content content = null, string pk = null, 
                            string rk = null, string user = null)
    {
        this.Content = content ?? new Content();
        if (pk != null) this.Content.PartitionKey = pk;
        if (rk != null) this.Content.RowKey = pk;
        if (user != null) this.Content.CreatedBy = user;
        this.Content.Created = DateTime.Now;
    }        

With this you can call your constructor setting or missing any of your params, providing them from left to right or using named params as suggested by @Robert Harvey.
Examples:

var c = new ContentViewModel();
var c = new ContentViewModel(pk: 'ppp', user: 'marco');
var c = new ContentViewModel(null, 'pk', null, 'marco');

Upvotes: 5

ghord
ghord

Reputation: 13797

In your case, you can have just once constructor:

public ContentViewModel(string pk, string rk = null, string user = null)
{
    this.Content = new Content();
    this.Content.PartitionKey = pk;
    this.Content.RowKey = rk;
    this.Content.Created = DateTime.Now;
    this.Content.CreatedBy = user;
}

You can even call it like:

var content = new ContentViewModel("pk");

or

var content = new ContentViewModel("pk", "rk");

or

var content = new ContentViewModel("pk", "rk", "user");

or even

var content = new ContentViewModel("pk", user: "user"); //without rk field

Upvotes: 0

Pete Baughman
Pete Baughman

Reputation: 3034

Yes, for example your constructor

public ContentViewModel(string pk)
{
 this.Content = new Content();
 this.Content.PartitionKey = pk;
 this.Content.Created = DateTime.Now;
}

Could call

public ContentViewModel(string pk, string rk)

With a default value for rk using the following syntax:

public ContentViewModel(string pk) : this(pk, "defaultRkValue"){}

You would substitute some meaningful data for "defaultRkValue"

If you wanted to go really crazy, then your two argument constructor could call your three argument constructor and so on and so forth

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180777

Write a single constructor with all arguments:

    public ContentViewModel(string pk, string rk, string user)
    {
        this.Content = new Content();
        this.Content.PartitionKey = pk;
        this.Content.RowKey = rk;
        this.Content.Created = DateTime.Now;
        this.Content.CreatedBy = user;
    }

Then call that constructor from the other constructors, passing default values for the missing arguments:

    public ContentViewModel(string pk) : this(pk, "","") { }

Alternatively, you can use Optional Arguments in C# 4.0.

Upvotes: 2

Related Questions