我有一些看起来像这样的摘要对象......
public class ObjectSummary : ISummary
{
public int Id { get; set;}
public string Name { get; set;}
...
// other fields
}
使用案例如下:
public ISummary Summarize()
{
return new ObjectSummary
{
Id = _id,
Name = _name,
....
// etc with other fields
};
}
我试图重构所有这样的实例(让它们变成不可变的):
public class ObjectSummary : ISummary
{
public ObjectSummary(int id, string name,..., /* other params*/)
{
Id = id;
Name = name;
}
public int Id { get; private set;}
public string Name { get; private set;}
// other fields
}
public ISummary Summarize()
{
return new ObjectSummary(_id, _name,..., /*other params*/);
}
如何重构一个使用对象初始化器的对象使其不可变(也将对象初始化器中的set逻辑移动到构造函数中)?