0

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:

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.

  1. it goes bad if someone were to call with 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).

5
  • What is the exact problem? Developer errors? Why do you want to put this in a single method? Will there ever be a case that you want to pass in both values? What is the use of the sql-identifier? Commented Jan 14, 2016 at 12:33
  • I think it's more about architecture Commented Jan 14, 2016 at 12:33
  • 2
    If source is suppose to be a unque identifier then why is it a string and not a Guid? Commented Jan 14, 2016 at 12:35
  • 2
    Also personally I think you should separate that stored procedure up as well. Commented Jan 14, 2016 at 12:36
  • 1
    To see if a string contains a valid GUID you can use Guid.TryParse for the return value. Commented Jan 14, 2016 at 13:41

1 Answer 1

3

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.

If you know, that one time you need it to pass an Uniqueidentifier, you could also make it generic:

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 Guids as strings around, is not very nice...

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. Futhermore the definition of a method, should describe it's usage...

Sign up to request clarification or add additional context in comments.

1 Comment

ok. Thanks for reply, I actually went with the 2 separate methods in the end =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.