0

I have a function:

private static PrivateMessage GetPM()
{
    using (var db = new DBContext())
    {
        var q = CompiledQueries.GetPMByID(db, messageID);
        if (q == null) return null;
        return new PrivateMessage(q);
    }
}

I wish to pass this function as a parameter:

var pm = cacheObj.SetIfNotExists(GetPM);

Where SetIfNotExists is defined as:

public T SetIfNotExists<T>(Func<T> getCachableObjectFunction)

What do I need to modify so that I can pass messageID as a parameter in GetPM()? EG:

private static PrivateMessage GetPM(int messageID)
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456

1 Answers1

2

Wrap and typecast your output is the easiest way.

var pm = cacheObj.SetIfNotExists(() => GetPM(1));

And change GetPM header to:

private static PrivateMessage GetPM(int messageID)
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
J.R.
  • 167
  • 5
  • Perfect thank you! Answers my next question of how to do it for functions with different parameter inputs as well! Just checking, passing it in this way won't invoke it will it? – Tom Gullen Oct 28 '16 at 12:09
  • You're right, it doesn't but the answer lets you pass any number of parameters in the `GetPM()` method. – Tom Gullen Oct 28 '16 at 12:19
  • 1
    @TomGullen Probably should have just given your own answer instead, but this is correct now. – juharr Oct 28 '16 at 12:22