【发布时间】:2012-09-06 00:53:26
【问题描述】:
出于各种原因,我想开始在设计中使用更多不可变类型。目前,我正在处理一个具有这样的现有类的项目:
public class IssueRecord
{
// The real class has more readable names :)
public string Foo { get; set; }
public string Bar { get; set; }
public int Baz { get; set; }
public string Prop { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
public string Prop4 { get; set; }
public string Prop5 { get; set; }
public string Prop6 { get; set; }
public string Prop7 { get; set; }
public string Prop8 { get; set; }
public string Prop9 { get; set; }
public string PropA { get; set; }
}
这个类代表了一些确实具有这么多属性的磁盘格式,因此在这一点上将其重构为更小的位几乎是不可能的。
这是否意味着这个类的构造函数在不可变设计中真的需要有 13 个参数?如果不是,如果我要使这个设计不可变,我可以采取哪些步骤来减少构造函数中接受的参数数量?
【问题讨论】:
-
类外部的东西是否需要设置值,还是类本身可以设置值?如果类可以做到,那么你可以有一个无参数的构造函数。
-
你现在如何初始化这些属性?
标签: c# immutability