传递给方法的字符串参数的 trim/null 值的优雅构造
Elegant construct to trim/null values of the string parameters passed to the method
假设我们有以下方法:
void Write(int? id, string s1, string s2, ... , string s10)
{
// prepare parameters:
// null/trim all (or some of) the string parameters
s1 = string.IsNullOrWhiteSpace(s1) ? null : s1.Trim();
s2 = string.IsNullOrWhiteSpace(s2) ? null : s2.Trim();
...
s10 = string.IsNullOrWhiteSpace(s10) ? null : s10.Trim();
// do write:
WriteRaw(id, s1, s2, ... , s10);
}
将记录写入数据库table。但是,在写入数据之前,需要"normalize"个参数,例如trim/null其中字符串类型的
是否可以更优雅地重写参数准备部分?类似于:
void Write(int? id, string s1, string s2, ... , string s10)
{
//pseudo code:
{ s1, s2, ... , s10 }.ForEach((ref s) => {
s = string.IsNullOrWhiteSpace ? null : s.Trim();
});
WriteRaw(id, s1, s2, ... , s10);
}
UPD:我无法更改 WriteRaw
的签名。另外除了字符串类型的参数外,还可以有其他类型的参数,例如:
void SetContactInfo(int? id, string firstName, string middleName, string lastName, bool isActive, string xmlContacts)
{
...
SetContactInfoRaw(id, firstName, middleName, lastName, isActive, xmlContacts);
}
将 s* 参数作为数组或映射对象传递,然后您将能够比现在更好地操作属性!
这会有帮助吗?
void Write(int? id, params string[] values)
{
var normalizedValues = values
.Select(v => Normalize(v))
.ToArray();
// do the rest
}
string Normalize(string v)
{
return string.IsNullOrWhiteSpace(v) ? null : v.Trim();
}
请注意,由于 params
仅支持数组,因此最好有
也用 IEnumerable<string>
重载:
void Write(int? id, IEnumerable<string> values)
{
// ...
}
尽管您可以使用与您的方法非常相似的方法来完成,
var s = new [] {s1, s2, ... , s10}
.Select( v => string.IsNullOrWhiteSpace(v) ? null : v.Trim()).ToList();
WriteRaw(id, s[0], s[1], ... , s[9]);
更好的方法是将检查包装到扩展方法中,并将其应用到位:
WriteRaw(id, s1.NullTrim(), s2.NullTrim(), ... , s10.NullTrim());
// This goes to a separate static "helper" class
internal static string NullTrim(this string s) {
return string.IsNullOrWhiteSpace(s) ? null : s.Trim();
}
第二种方法更经济,因为它不会创建包含要 "normalized" 的字符串的新列表或数组。
假设我们有以下方法:
void Write(int? id, string s1, string s2, ... , string s10)
{
// prepare parameters:
// null/trim all (or some of) the string parameters
s1 = string.IsNullOrWhiteSpace(s1) ? null : s1.Trim();
s2 = string.IsNullOrWhiteSpace(s2) ? null : s2.Trim();
...
s10 = string.IsNullOrWhiteSpace(s10) ? null : s10.Trim();
// do write:
WriteRaw(id, s1, s2, ... , s10);
}
将记录写入数据库table。但是,在写入数据之前,需要"normalize"个参数,例如trim/null其中字符串类型的
是否可以更优雅地重写参数准备部分?类似于:
void Write(int? id, string s1, string s2, ... , string s10)
{
//pseudo code:
{ s1, s2, ... , s10 }.ForEach((ref s) => {
s = string.IsNullOrWhiteSpace ? null : s.Trim();
});
WriteRaw(id, s1, s2, ... , s10);
}
UPD:我无法更改 WriteRaw
的签名。另外除了字符串类型的参数外,还可以有其他类型的参数,例如:
void SetContactInfo(int? id, string firstName, string middleName, string lastName, bool isActive, string xmlContacts)
{
...
SetContactInfoRaw(id, firstName, middleName, lastName, isActive, xmlContacts);
}
将 s* 参数作为数组或映射对象传递,然后您将能够比现在更好地操作属性!
这会有帮助吗?
void Write(int? id, params string[] values)
{
var normalizedValues = values
.Select(v => Normalize(v))
.ToArray();
// do the rest
}
string Normalize(string v)
{
return string.IsNullOrWhiteSpace(v) ? null : v.Trim();
}
请注意,由于 params
仅支持数组,因此最好有
也用 IEnumerable<string>
重载:
void Write(int? id, IEnumerable<string> values)
{
// ...
}
尽管您可以使用与您的方法非常相似的方法来完成,
var s = new [] {s1, s2, ... , s10}
.Select( v => string.IsNullOrWhiteSpace(v) ? null : v.Trim()).ToList();
WriteRaw(id, s[0], s[1], ... , s[9]);
更好的方法是将检查包装到扩展方法中,并将其应用到位:
WriteRaw(id, s1.NullTrim(), s2.NullTrim(), ... , s10.NullTrim());
// This goes to a separate static "helper" class
internal static string NullTrim(this string s) {
return string.IsNullOrWhiteSpace(s) ? null : s.Trim();
}
第二种方法更经济,因为它不会创建包含要 "normalized" 的字符串的新列表或数组。