C# 代码审查:CLR RegexSubstring

C# 代码审查:CLR RegexSubstring,c#,.net,sql-server,regex,clr,C#,.net,Sql Server,Regex,Clr,这能更好吗。SQL Server 2005的NET 2.0兼容性: public static SqlString RegexSubstring(SqlString regexpattern, SqlString sourcetext, SqlInt32 start_position) { SqlString result = nu

这能更好吗。SQL Server 2005的NET 2.0兼容性:

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;
}

这是我第一次尝试为SQL Server编写CLR函数/etc-是否绝对有必要为参数使用SqlString/etc数据类型?

只是通过重构/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);
}
请注意,开始位置未使用,因此您可能忽略了警告?

另一件事只是风格问题,但是函数是否可以编写为不依赖于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))

感谢更新-不知道我将从抽象中获得什么,所以我不会在方法中调用ToString()。啊,就像我说的,这只是一种风格。有时,对于单元测试隔离,人们希望看到尽可能少的依赖项。不用担心,您指的是多返回与单返回语句模式/反模式。