Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6344977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:44:17+00:00 2026-05-24T20:44:17+00:00

This is probably easiest to explain with code (this is of course not the

  • 0

This is probably easiest to explain with code (this is of course not the actual code but it has the same properties):

I have an interface that looks something like this:

    public interface ISomeProvider
    {
        object GetFoo1(); //<-- This needs caching

        //These others don't
        object GetFoo2();
        object GetFoo3();

        //And let's say 20 more
    }

And this has an implementation like this:

    //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, let’s say GetFoo1 is really heavy so I want to provider a new version of the interface where calls to it are cached using an instance of the old one.

I’m doing it like this at the moment:

    public class CachedSuperCleverProvider : ISomeProvider
    {
        private readonly SuperCleverProvider _provider;

        public CachedSuperCleverProvider(SuperCleverProvider provider)
        {
            _provider = provider;
        }

        private object UsingCache<T>(string cacheKey, Func<T> eval)
        {
            //Pretend this does caching. This is not related to the question
            throw new NotImplementedException();
        }

        public object GetFoo1()
        {
            return UsingCache("foo1", _provider.GetFoo1);
        }

        //The code below this point is what I want to get rid of
        public object GetFoo2()
        {
            return _provider.GetFoo2();
        }

        public object GetFoo3()
        {
            return _provider.GetFoo3();
        }

        //And so on for all the rest
    }

This has two problems (at least):

  • Every time someone adds a method to the interface I have to go change this even though I dont want this new method to be cached
  • I get this huge list of useless code that just call through to the underlying implementation.

Can anyone think of a way of doing this that doesn’t have these problems?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:44:18+00:00Added an answer on May 24, 2026 at 8:44 pm

    Three options:

    • Autogenerate the class
    • Use PostSharp or something similar to do it in a more interceptor-based way
    • Live with it

    Personally I’d probably go with the third option, unless you really find yourself doing this a lot. Weigh up the cost of each option – how much time are you actually going to spend adding this delegation?

    Personally I’d like to see this sort of thing as a language feature – “delegate to this interface via this field unless I override it” but obviously that’s not present at the moment…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This probably has a simple answer, but I must not have had enough coffee
I know this probably is not the easiest thing to do, but I am
OK, this probably is supposed to be the easiest thing in the world, but
This probably sounds really stupid but I have noo idea how to implement jquery's
I know this probably really simple but Im not sure what im doing wrong...
Ok, this probably has a really simple answer, but I've never tried to do
I probably have one of the easiest questions of the day, but I'm having
This is probably one of those easiest things to deal with but for some
This is probably something very simple but I'm not getting the results I'm expecting.
Which IoC container is the easiest to get started with. This probably equates to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.