mardi 13 décembre 2016

Design or Code Pattern to return multiple optional values from a function

I keep writing several utility, reusable functions. Many functions return multiple values for which I use out or ref parameters. Many functions also have additional information, which may be of use to some callers (but not all callers).

Example, a function to read a CSV file may have additional information like no. of blank lines, no. of lines with duplicate values, and some other statistics.

Not every caller will be interested in this information, so I don't want to include all these as out, ref or Tuples, which will make it mandatory for the caller to declare all such expected variables.

Is there any other mechanism using which the caller can choose or retrieve some of the optional additional information it is interested in?

For example, Func 1 can call Func B. After calling it, it gets all the standard return values. Additionally, can it call something like FuncB.GetAdditionalInfo(infoType) without Func B getting executed again?

It may be possible to design this using a class which serves as an intermediary to store all the optional values as well, and then return them to the caller on request; but I want it to be generic enough to be used across all my utility functions.

1 possibility is Func B storing all these in some kind of global variables, which the caller can access if required. But if a utility class has several such resuable functions, I will need to have so many public variables, for the additional info of each function!

I am on .NET 4.5 as of now. Is there a design pattern for this?

Thanks.

Aucun commentaire:

Enregistrer un commentaire