2010-05-12 90 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