2010-05-12 89 views
2

難道這不是更好嗎? SQL Server 2005的.NET 2.0的兼容性:代碼審查:CLR RegexSubstring

public static SqlString RegexSubstring(SqlString regexpattern, 
             SqlString sourcetext, 
             SqlInt32 start_position) 
{ 
    SqlString result = null; 

    if (!regexpattern.IsNull && !sourcetext.IsNull && !start_position.IsNull) 
    { 
     int start_location = (int)start_position >= 0 ? (int)start_position : 0; 

     Regex RegexInstance = new Regex(regexpattern.ToString()); 
     result = new SqlString(RegexInstance.Match(sourcetext.ToString(), 
               start_location).Value); 
    } 

    return result; 
} 

這是我在寫CLR函數/等爲SQL Server的第一次嘗試 - 是絕對有必要使用的SqlString /等數據類型的參數?

回答

1

只要運行它通過重構/ Pro的

給這個:

public static SqlString RegexSubstring(SqlString regexpattern, 
           SqlString sourcetext, 
           SqlInt32 start_position) { 
    if (regexpattern.IsNull || sourcetext.IsNull || start_position.IsNull) 
     return null; 

    Regex RegexInstance = new Regex(regexpattern.ToString()); 

    return new SqlString(RegexInstance.Match(sourcetext.ToString(), 
               (int)start_position).Value); 
} 

注意START_LOCATION是未使用的,所以可能你忽略警告?

另一件事只是一個風格問題,但功能被寫入沒有對SqtTypes的依賴?然後代碼變爲:

private static string RegexSubstring(string regexpattern, string sourcetext, int start_position) { 

     if (regexpattern == null || sourcetext == null || start_position == null) 
      return null; 

     Regex RegexInstance = new Regex(regexpattern); 
     return RegexInstance.Match(sourcetext, start_position).Value; 
    } 

,並稱之爲:

new SqlString(RegexSubstring(regexpattern.ToString(), sourcetext.ToString(), start_position)) 
+0

欣賞更新 - 不知道是什麼我站從抽象所以我不調用toString()方法中獲得。 – 2010-05-12 21:30:40

+0

啊,就像我說過它只是一種風格的東西。有時對於單元測試隔離,人們喜歡儘可能少地依賴。 – 2010-05-12 22:05:38

+0

不用擔心,以爲你指的是多重返回與單數返回語句模式/反模式。 – 2010-05-12 22:15:12