Should I use different types of return data for overloaded methods?

Are there any recommendations for returning different types of returned data with overloaded methods? For example, if I have a Load method in my DAL, I either want to load a single element or a bunch of elements. I know that I can use several approaches:

Loading a single object

MyBusinessObject LoadOne(int id)
{
}

Loading multiple objects

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

Now I know what I can do is to overload one method and have different types of returned data. For example:

MyBusinessObject Load(int id)
{
}

and

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

While it seems like nothing prevents me from doing this, and it keeps things clean from an API point of view, does this seem like a good idea? I stumbled upon it last night, and part of me thinks I should not do this because of the desire for matching return types for the overloaded method.

Load (int id), , . , , , , , , .

, , :

  • .
  • , , . API, , , .
  • , .. , , , , , , , .

, , .

- , ? .

+5
5

, API :

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

, params - .

+8

API. , LINQ, "", , "Single", . API , , , .

+3

, , , , , .

:

var a = MyFunc("Some text");

var a = MyFunc(1);

, var . , , , .

+3

", " , , " ".

but then you can overload "LoadMany" with several scripts;

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
}

... any combinations you need can be added, but in the end, LoadMany returns a list and Load returns a single entity.

+2
source

My personal idea is that the latter method seems more understandable from the point of view of API users.

0
source

All Articles