我经常(就像现在一样)来写这样的c#(或vb.net)代码:
someObject.field_1 = doSomething(
anotherObject_1.propertyA,
anotherObject_1.propertyB);
someObject.field_2 = doSomething(
anotherObject_2.propertyA,
anotherObject_2.propertyB);
// some more field in same schema.
someObject.field_X = doSomething(
anotherObject_X.propertyA,
anotherObject_X.propertyB);
编辑: anotherObject_1 .. anotherObject_X 具有相同的类型;someObject 和 anotherObject 通常具有不同的类型。
问题是可扩展性和可维护性。新领域使我可以编写几乎相同的代码,而对象命名几乎没有区别。
通过将逻辑封装在 doSomething(..) 中,我避免了逻辑冗余,但调用冗余仍然很烦人。
有没有办法(例如模式或.Net(4.0)语言结构)来避免这种情况?