【问题标题】:Advice on Method overloads关于方法重载的建议
【发布时间】:2010-03-25 21:10:02
【问题描述】:

请看以下方法。

    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- 请建议我如何为 CustomerId 和 SupplierId 进行重载,因为使用 GetDummyData(int?) 只能创建一个重载。我是否应该添加另一个参数来提及第一个参数是 CustomerId 或 SupplierId,例如 GetDummyData(int?, string)。或者我应该使用枚举作为第二个参数并提到第一个参数是 CustoemId 或 SupplierId。

2- 这个条件是正确的还是仅仅检查 > 0 就足够了 -> if (customerId != null && customerId > 0)

3- 像这样使用 Try/catch 是否正确?

4- 传递 (int?)null 是正确的或任何其他更好的方法。

编辑:

我发现了一些其他类似的帖子,因为我对泛型一无所知,这就是我面临这个问题的原因。我对吗?以下是帖子。

Overloaded method calling overloaded method

【问题讨论】:

    标签: c#


    【解决方案1】:
    1. 为什么不创建单独的 GetCustomerData(int)GetSupplierData(int) 方法? (如果需要,还可以加上 GetData()GetData(int,int)。)

    2. 如果您将方法参数更改为 int 而不是 int?,那么您只需检查(自行决定)它们是否大于 0。

    3. 在这种情况下不需要try...catch。如果您所做的只是重新抛出异常,那么一开始就不要费心去捕捉它。

    4. 参见上面的 1 和 2。

    编辑:也许是这样的......

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

    【讨论】:

    • 感谢您的建议,您能对我的编辑发表评论吗?
    • 我认为泛型或您编辑中提到的其他问题与这种情况无关。
    • 感谢您的时间和 cmets。如果这不打扰你,你能告诉我为什么我必须创建单独的方法。为什么我不应该重载这个方法。如果我必须创建单独的方法来编写或多或少相同的代码,那么方法重载的目的是什么。
    • 您不能使用相同类型的参数创建多个重载,因为它们具有相同的签名,这就是创建不同命名方法的原因。不同命名的方法将需要与重载完全相同的代码。重载方法可能有多种原因,主要是您这样做是为了让可选参数在重载方法中采用默认值。您也可以使用多个方法来获取和返回不同类型,但只要您使用的是 c# 2.0 及更高版本,使用泛型可以更好地解决此问题。
    • 更明确地删除 try/catch。您所做的所有代码都使调试变得更加困难,因为这样就更难看到异常发生的位置,因为它总是发生在 throw 语句中。
    【解决方案2】:

    你没有对 try catch 做任何事情,所以为什么要包含它。无论如何,您的异常都会冒泡到调用方法,因此只需删除无关的 try catch 代码。

    我会放弃第二种和第三种方法,因为它们实际上并没有做任何事情。除非你有一些理由保留它们,就像它们是遗留代码一样,我会删除它们。它只会使代码更加复杂,以供将来支持。

    【讨论】:

    • 我不需要参数,获取没有过滤器的数据,或者我想获取 CustomerId 或 SupplierId 或两者上的数据,这就是我创建重载的原因。在我的场景中我不需要这些吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多