Method Overloading. Can you overuse it?

前端 未结 16 1389
有刺的猬
有刺的猬 2020-12-02 13:38

What\'s better practice when defining several methods that return the same shape of data with different filters? Explicit method names or overloaded methods?

For exa

16条回答
  •  一整个雨季
    2020-12-02 14:05

    Yes you can overuse it, however here is another concept which could help keep the usage of it under control ...

    If you are using .Net 3.5+ and need to apply multiple filters you are probably better to use IQueryable and chaining i.e.

    GetQuery().ApplyCategoryFilter(category).ApplyProductNameFilter(productName);
    

    That way you can reuse the filtering logic over and over whereever you need it.

    public static IQueryable ApplyXYZFilter(this IQueryable query, string filter)
    {
         return query.Where(XYZ => XYZ == filter);
    } 
    

提交回复
热议问题