简体   繁体   English

C#识别SQL的uniqueidentifier

[英]c# recognize sql uniqueidentifier

I have a method that I want to use differently based on the type of input. 我有一种方法要根据输入的类型使用不同的方法。 This is what I have: 这就是我所拥有的:

public static DataTable GetS(string source = null, string customer = null)
    {
        if (source != null && customer == null) 
        {
            return GetDataTableFromQuery("db.GetS", new object[] { "@source", source });
        }
        else if (source == null && customer != null)
        {
            return GetDataTableFromQuery("db.GetS", new object[] { "@customer", customer });
        }
        else
        {
            throw new Exception("Bad input. Call GetS with GetS(source) or GetS(null,customer).");
        }
    }

The sp looks like this: sp看起来像这样:

CREATE PROCEDURE [db].[GetS]

    @source as nvarchar(128) = NULL,
    @customer as nvarchar(128) = NULL

AS
BEGIN
IF @customer IS NULL
BEGIN
    SELECT 
        *
    FROM 
        db.S
    WHERE
        [Source] = @source
END
IF @source IS NULL
BEGIN
    SELECT 
        * 
    FROM 
        db.S
    WHERE 
        customer = @customer
END

END

This works fine for GetS(source) and GetS(null,customer) but I have 2 issues. 这对于GetS(source)GetS(null,customer)但是我有2个问题。

  1. it goes bad if someone were to call with GetS(customer) 如果有人用GetS(customer)打电话会很糟糕
  2. it doesn't look very pretty.. 它看起来不是很漂亮。

is there some way of doing something like this(pseudo-code): 有什么办法可以做这样的事情(伪代码):

public static DataTable GetS(string input)
{
    if(input is sql-uniqueidentifier)
    {
        return GetDataTableFromQuery("db.GetS", new object[] { "@source", input});
    }
    else
    {
        return GetDataTableFromQuery("db.GetS", new object[] { "@customer", input]);
    }
}

or is there a better way? 或者,还有更好的方法? (sure, I could make 2 separate methods, but I'd like to make it work with just one Get -method. Feels weird if I make a GetSBySource or something). (当然,我可以制作2个单独的方法,但是我只想使用一种Get方法就可以使用它。如果我制作GetSBySource东西,感觉很奇怪)。

In your case, why not write two methode, this is not weird!! 在您的情况下,为什么不写两个方法,这并不奇怪! ? I thing using two methods is the best way. 我用两种方法是最好的方法。

  1. public static DataTable GetSBySource(Guid source)
  2. public static DataTable GetSByCustomer(string customer)

This would make your API ways more usable and clear. 这将使您的API方式更加实用和清晰。

If you know, that one time you need it to pass an Uniqueidentifier , you could also make it generic: 如果您知道,那一次您需要它传递一个Uniqueidentifier ,您也可以使其通用:

public static DataTable GetS<T>(string input)
{
    if(T is Guid)
    {
        return GetDataTableFromQuery("db.GetS", new object[] { "@source", input});
    }
    else
    {
        return GetDataTableFromQuery("db.GetS", new object[] { "@customer", input]);
    }
}

But then you should also make your input generic, because passing Guid s as string s around, is not very nice... 但是然后您还应该使输入通用,因为将Guid s作为string s传递不是很好...

Using bad method definitions, will cause a lot of problems, when changing the code. 在更改代码时,使用错误的方法定义会导致很多问题。 For example you need to pass a Guid to the method, which only accepts string, than refactoring or changing the code will be very hard. 例如,您需要将Guid传递给只接受字符串的方法,否则重构或更改代码将非常困难。 Futhermore the definition of a method, should describe it's usage... 此外,方法的定义应描述其​​用法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM