1

我得到了一些关于构造函数的建议,但现在我有很多。我可以用函数或其他方式简化这些吗?这是我的课吗?

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; }

    }

我正在使用 c# 4。有人说过命名参数和可选参数。这会帮助我吗?

4

4 回答 4

5

试试这个:

    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;
    }

或者你可以尝试:

    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;
    }        

有了这个,您可以调用您的构造函数设置或丢失任何参数,从左到右提供它们或使用@Robert Harvey 建议的命名参数。
例子:

var c = new ContentViewModel();
var c = new ContentViewModel(pk: 'ppp', user: 'marco');
var c = new ContentViewModel(null, 'pk', null, 'marco');
于 2012-07-08T05:58:15.297 回答
2

编写一个包含所有参数的构造函数:

    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 ContentViewModel(string pk) : this(pk, "","") { }

或者,您可以使用C# 4.0 中的可选参数

于 2012-07-08T05:58:06.853 回答
1

是的,例如你的构造函数

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

可以打电话

public ContentViewModel(string pk, string rk)

使用以下语法为 rk 设置默认值:

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

您可以用一些有意义的数据代替“defaultRkValue”

如果您真的想发疯,那么您的两个参数构造函数可以调用您的三个参数构造函数,依此类推

于 2012-07-08T05:58:46.140 回答
0

在您的情况下,您只能拥有一次构造函数:

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;
}

你甚至可以这样称呼它:

var content = new ContentViewModel("pk");

或者

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

或者

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

甚至

var content = new ContentViewModel("pk", user: "user"); //without rk field
于 2012-07-08T06:03:54.890 回答