This is perhaps the easiest way to explain with the help of code (this, of course, is not actual code, but it has the same properties):
I have an interface that looks something like this:
public interface ISomeProvider { object GetFoo1();
And this has an implementation as follows:
//NOTE: Sealed class otherwise we could inherit from it public sealed class SuperCleverProvider : ISomeProvider { public object GetFoo1() { return "a"; } public object GetFoo2() { return "b"; } public object GetFoo3() { return "b"; } }
Now one of these calls, say GetFoo1 is really heavy, so I want to provide a new version of the interface where calls to it are cached using an instance of the old one.
I do it like this:
public class CachedSuperCleverProvider : ISomeProvider { private readonly SuperCleverProvider _provider; public CachedSuperCleverProvider(SuperCleverProvider provider) { _provider = provider; } private object UsingCache<T>(string cacheKey, Func<T> eval) {
This has two problems (at least):
- Every time someone adds a method to the interface, I need to change this, even if I do not want this new method to be cached
- I get this huge list of useless code that just calls the base implementation.
Can anyone think of a way to do this that doesn't have these problems?
source share