简体   繁体   English

我应该在重载方法上使用不同的返回类型吗?

[英]Should I use different return types on overloaded methods?

Are there any best practices on returning different return types on overloaded methods? 是否有关于在重载方法上返回不同返回类型的最佳实践? For instance if I have a Load method in my DAL, I either want to load a single item or a bunch of items. 例如,如果我的DAL中有一个Load方法,我要么加载一个项目或一堆项目。 I know that I could use a number of approaches: 我知道我可以使用多种方法:

Load one object 加载一个对象

MyBusinessObject LoadOne(int id)
{
}

Load multiple objects 加载多个对象

MyBusinessObject[] LoadMany(params int[] ids)
{
}

Now something I know I can do is to overload one method and have different return types. 现在,我知道我可以做的是重载一个方法并具有不同的返回类型。 Like so: 像这样:

MyBusinessObject Load(int id)
{
}

And

MyBusinessObject[] Load(params int[] ids)
{
}

While it seems there's nothing to stop me doing this, and it keeps things tidy from an API perspective, does this seem like a good idea? 虽然似乎没有什么可以阻止我这样做,并且从API的角度来看它保持整洁,这看起来是个好主意吗? I came across it last night and part of me thinks I shouldn't be doing this for the reason of wanting matching return types for overloaded method. 我昨晚遇到了它,部分我认为我不应该这样做是因为想要匹配重载方法的返回类型。

I could also have the Load(int id) method return a collection that only holds one item. 我也可以让Load(int id)方法返回一个只包含一个项目的集合。 It seems to me that this violates the principle of least surprise though in that if you're expecting one item returned, you should return that item, you shouldn't return a list containing a single item. 在我看来,这违反了最少惊喜的原则,但是如果你期望返回一个项目,你应该返回该项目,你不应该返回包含单个项目的列表。

So here are my conflicting thoughts surrounding these ideas: 所以这是围绕这些想法的矛盾思想:

  • Overloaded methods should all return the same type. 重载方法应该都返回相同的类型。
  • If methods do the same thing, don't give them a bunch of different names, overload the same method name. 如果方法做同样的事情,不要给它们一堆不同的名称,重载相同的方法名称。 It makes things simpler from an API user's perspective, they don't have to trawl through a bunch of different methods that all essentially do the same thing but with different parameters. 从API用户的角度来看,它使事情更简单,他们不必遍历一堆不同的方法,这些方法基本上都做同样的事情,但使用不同的参数。
  • Return the most obvious type for the method, ie if the user is likely to be expecting a collection of items, return a collection of items, if they are likely to be expecting a single item, return a single item. 返回该方法最明显的类型,即如果用户可能期望收集项目,则返回项目集合,如果他们可能期望单个项目,则返回单个项目。

So the latter two thoughts kind of outweigh the first, but at the same time, the first thought seems like a programmatic best practice of sorts. 所以后两种想法比第一种想法更重要,但与此同时,第一种想法似乎是一种程序化的最佳实践。

Are there any best practices surrounding this practice? 有没有围绕这种做法的最佳做法? I'd be interested to hear others' thoughts on the subject. 我有兴趣听听别人对这个问题的看法。

I might be tempted to make the API explicit and use plurality in the names: 我可能想要明确API并在名称中使用多个:

Customer LoadCustomer(int id) {...}
Customer[] LoadCustomers(params int[] id) {...}

Actually, the params is rarely useful here - you don't usually know the ids at compile-time. 实际上, params在这里很少有用 - 你通常不会在编译时知道id。

You can just look to an existing APIs. 您可以查看现有的API。 Let's take LINQ for example, it has "Select" method that returns many entities and also "Single" method that returns only one entity. 我们以LINQ为例,它具有返回许多实体的“Select”方法以及仅返回一个实体的“Single”方法。 Most of the existing APIs has two different methods, not an overloaded ones and I think this is logical and more readable. 大多数现有的API有两种不同的方法,而不是重载的方法,我认为这是合乎逻辑的,更具可读性。

There are probably exceptions, but unless you have a really good reason to return different types, a function and its overloads should return the same type so that you don't drive other developers crazy. 可能有异常,但除非你有一个很好的理由返回不同类型,否则函数及其重载应该返回相同的类型,这样你就不会让其他开发人员疯狂。

For example: 例如:

var a = MyFunc("Some text");

and

var a = MyFunc(1);

both look like they should resolve the var to the same type to me. 看起来他们应该将var解析为同一类型给我。 Before I deviated from having all overloads return the same type I would make sure I had a very solid reason for returning different types. 在我偏离所有重载返回相同类型之前,我会确保我有一个非常坚实的理由返回不同类型。

I tend to follow the first point in your list of "thoughts surronding this idea" that says "overloads should return the same type". 我倾向于遵循你的“思想支持这个想法”列表中的第一点,即“重载应该返回相同的类型”。

but you can then overload the "LoadMany" with some different scenarios; 但是你可以用一些不同的场景重载“LoadMany”;

public Customer Load(int id)
{
    // return just one customer
}

public List<Customer> LoadMany()
{
    // return every single customer
}

public List<Customer> LoadMany(int statusFilter)
{
    // return a filtered list of customers
}

public List<Customer> LoadMany(DateTime InitialContactFrom)
{
    // return a filtered list of customers
}

public List<Customer> LoadMany(DateTime InitialContactFrom, DateTime InitialContactBefore)
{
    // return a filtered list of customers
}

...whatever combinations you need can obviously be added but in the end, LoadMany returns a list and Load returns one entity. ...显然可以添加你需要的任何组合,但最后,LoadMany返回一个列表,Load返回一个实体。

我个人的想法是,从API用户的角度来看,后一种方法似乎更容易理解。

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

相关问题 c#泛型:我可以将重载方法合并为具有不同返回/输入数据类型的方法吗? - c# Generics : Can I combine overloaded methods into one with different return/input data types? 在重载方法中使用泛型类型 - Use of generic types in overloaded methods 使用 c# 库的 Powershell 无法识别具有不同返回类型的重载方法 - Powershell using c# library not recognizing overloaded methods with different return types 是否应将具有值类型的重载方法合并为通用方法? - Should overloaded methods with value types be consolidated into a generic method? 多个异步方法,最好使用WhenAll具有不同的返回类型 - Multiple Async Methods, best to use WhenAll with different return types 使用通用返回类型不工作的重载静态方法 - Overloaded static methods using Generic Return Types Not Working 具有不同返回类型的相同方法 - Same methods with different return types 返回不同返回类型的方法 - methods that are returning different return types 如果“==”运算符被重载以与几种类类型进行比较,我应该如何使用“== null” - How should I use “== null” if “==” operator is overloaded to be compared with several class types C#.Net 3.5使用具有不同返回类型的重载索引器 - C# .Net 3.5 Using Overloaded Indexers with different return types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM