Question

i've got simple method with no parameters:

[Cache]
private void Get()
{
   int id = 5;
   var c = GetCustomerService(id);
}

I would like to Cache this method for different "id"s. "id" isn't parameter of this method. Is there any chance to call OnInvoke() using my id argument to calculate proper CacheKey??

thx4help

Était-ce utile?

La solution

I understand that your Cache attribute is MethodInterceptionAspect. During interception your aspect's OnInvoke code is actually executed first and then it can continue with executing your original method. It seems that what you're asking about is the opposite - calling OnInvoke back from the original Get method. This is of course is not possible.

You can perform some refactoring by creating a new method with all the parameters that define the caching key. Then inside your OnInvoke method you can just use MethodInterceptionArgs.Arguments to build the caching key.

private T Get()
{
   int id = 5;
   return GetInternal(id);
}

[Cache]
private T GetInternal(int id)
{
   return GetCustomerService(id);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top