[英]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: 所以这是围绕这些想法的矛盾思想:
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.