tags:

views:

63

answers:

3

I am wrapping a number of functions from a vender API in C#. Each of the wrapping functions will fit the pattern:

public IEnumerator<IValues> GetAggregateValues(string pointID, DateTime startDate, DateTime endDate, TimeSpan period) {
   // Validate Data
   // Break up Requesting Time-Span
   // Make Requests
   // Read Results (through another method call
}

5 of the 6 requests are aggregate data pulls and have the same signature, so it makes sense to put them in one method and pass the aggregate type to avoid duplication of code. The 6th method however follows the exact same pattern with the same result-set, but is not an aggregate, so no time period is passed to the function (changing the signature).

Is there an elegant way to handle this kind of situation without coding a one-off function to handle the non-aggregate request?

A: 

You could just pass in DateTime.MinValue and DateTime.MaxValue as the startDate and endDate.

smoore
+1  A: 

There are definitely ways to handle this:

  1. Make the time-period parameters nullable (DateTime? or Nullable<DateTime>)
  2. Create a struct or class to hold the "parameters" to the function, including more or less information as needed.
  3. Pull the code inside your existing function into several smaller functions, then put them back together into the two separate functions you need, avoiding most of the duplication while keeping completely separate functions/signatures.

Note that for 1 and 2, you'll need to check for these conditions and use/skip some code inside the function based on the values.

John Fisher
A: 

I think the best way is to create AggregateArgs class for saving parameters of the request. And you can have a signature like this:

public IEnumerator<IValues> GetAggregateValues(AggregateArgs args);

Also you can just send TimeSpan.Zero to your method if you don't need it.

Hun1Ahpu