简体   繁体   English

关于方法重载的建议

[英]Advice on Method overloads

Please see following methods. 请参阅以下方法。

    public static ProductsCollection GetDummyData(int? customerId, int? supplierId)
    {
        try
        {
            if (customerId != null && customerId > 0)
            {
                Filter.Add(Customres.CustomerId == customerId);
            }

            if (supplierId != null && supplierId > 0)
            {
                Filter.Add(Suppliers.SupplierId == supplierId);
            }

            ProductsCollection products = new ProductsCollection();

            products.FetchData(Filter);

            return products;
        }
        catch
        {
            throw;
        }
    }

    public static ProductsCollection GetDummyData(int? customerId)
    {
        return ProductsCollection GetDummyData(customerId, (int?)null);
    }

    public static ProductsCollection GetDummyData()
    {
        return ProductsCollection GetDummyData((int?)null);
    }

1- Please advice how can I make overloads for both CustomerId and SupplierId because only one overload can be created with GetDummyData(int? ). 1-请告知我如何对CustomerId和SupplierId都进行重载,因为只能使用GetDummyData(int?)创建一个重载。 Should I add another argument to mention that first argument is CustomerId or SupplierId for example GetDummyData(int?, string). 我是否应该添加另一个参数来提及第一个参数是CustomerId或SupplierId,例如GetDummyData(int ?, string)。 OR should I use enum as 2nd argument and mention that first argument is CustoemId or SupplierId. 或者我应该使用enum作为第二个参数,并提到第一个参数是CustoemId或SupplierId。

2- Is this condition is correct or just checking > 0 is sufficient -> if (customerId != null && customerId > 0) 2-这个条件是正确的还是只是检查> 0是否足够->如果(customerId!= null && customerId> 0)

3- Using Try/catch like this is correct? 3-像这样使用Try / catch是正确的吗?

4- Passing (int?)null is correct or any other better approach. 4-传递(int?)null是正确的或任何其他更好的方法。

Edit: 编辑:

I have found some other posts like this and because I have no knowledge of Generics that is why I am facing this problem. 我发现了其他类似的帖子,因为我不了解泛型,所以才遇到这个问题。 Am I right? 我对吗? Following is the post. 以下是帖子。

Overloaded method calling overloaded method 重载方法调用重载方法

  1. Why not just create separate GetCustomerData(int) and GetSupplierData(int) methods? 为什么不创建单独的GetCustomerData(int)GetSupplierData(int)方法? (Along with GetData() and GetData(int,int) if you need them.) (如果需要,还可以使用GetData()GetData(int,int) 。)

  2. If you change your method arguments to int rather than int? 如果将方法参数更改为int而不是int? then you only need to check (at your discretion) that they're greater than 0. 那么您只需要(自行决定)检查它们是否大于0。

  3. There's no need for the try...catch in this situation. 在这种情况下,不需要try...catch If all you're doing is re-throwing the exception then don't bother catching it in the first place. 如果您正在做的只是重新抛出异常,那么就不要一开始就抓住它。

  4. See 1 and 2 above. 请参阅上面的1和2。

EDIT: Maybe something like this... 编辑:也许像这样...

public static ProductsCollection GetData()
{
    return GetDataImpl(-1, -1);
}

public static ProductsCollection GetData(int customerId, int supplierId)
{
    if (customerId <= 0)
        throw new ArgumentOutOfRangeException("customerId");

    if (supplierId <= 0)
        throw new ArgumentOutOfRangeException("supplierId");

    return GetDataImpl(customerId, supplierId);
}

public static ProductsCollection GetCustomerData(int customerId)
{
    if (customerId <= 0)
        throw new ArgumentOutOfRangeException("customerId");

    return GetDataImpl(customerId, -1);
}

public static ProductsCollection GetSupplierData(int supplierId)
{
    if (supplierId <= 0)
        throw new ArgumentOutOfRangeException("supplierId");

    return GetDataImpl(-1, supplierId);
}

private static ProductsCollection GetDataImpl(int customerId, int supplierId)
{
    if (customerId > 0)
        Filter.Add(Customers.CustomerId == customerId);

    if (supplierId > 0)
        Filter.Add(Suppliers.SupplierId == supplierId);

    ProductsCollection products = new ProductsCollection();
    products.FetchData(Filter);

    return products;
}

You aren't doing anything with the try catch, so why include it. 您对try catch没有做任何事情,所以为什么要包含它。 You exception is going to bubble up to the calling method anyway, so just remove the extraneous try catch code. 无论如何,您的例外都会冒泡到调用方法,因此只需删除多余的try catch代码。

I would ditch the second and the third methods as they aren't really doing anything. 我将放弃第二种和第三种方法,因为它们实际上没有做任何事情。 Unless you have some reason to keep them around, like they are legacy code, I would remove them. 除非您有任何保留它们的理由,例如它们是旧代码,否则我将其删除。 It will just make the code more complicated for future support. 这只会使代码变得更加复杂,以供将来支持。

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

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