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 6023885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:04:21+00:00 2026-05-23T04:04:21+00:00

I have an IRepository< T > interface with many T’s and several implementations (on-demand

  • 0

I have an IRepository< T > interface with many T’s and several implementations (on-demand DB, web service, etc.). I use AutoFac to register IRepository’s for many T’s depending on the kind of repository I want for each T.

I also have a .NET-caching-based implementation that looks for T’s in cache and then calls a ‘real’ IRepository.Find to resolve a cache miss. It is constructed something like this:

new CachingRepository(realRepository, cacheImplementation);

I would like to use a configuration flag to decide if AutoFac serves up caching-based IRepository’s or the ‘real things.’ It seems like ‘realRepository’ comes from asking AutoFac to resolve IRepository < T > but then what do clients get when they ask to resolve the same interface? I want them to get the CachingRepository if the flag is set.

I can’t get my head around how to implement this flag-based resolution. Any ideas?

  • 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-23T04:04:21+00:00Added an answer on May 23, 2026 at 4:04 am

    Simplest Option: Conditional Registration Delegate

    There are a number of ways to do this. Using your cache setting in a registration delegate is probably the simplest (and illustrates the power of delegate registrations):

    var builder = new ContainerBuilder();
    
    bool cache = GetCacheConfigSetting(); //Up to you where this setting is.    
    
    builder.Register(c => cache ? (IRepository<string>)new CachingRepository<string>(new RealRepos<string>(), new CacheImpl()) : new RealRepos<string>());
    

    The code above will only read the cache config once. You could also include the GetCacheConfigSetting() in the registration delegate. This would result in the setting being checked on every Resolve (assuming InstancePerDependency).

    Other Options: Autofac Decorators and Modules

    There are some more advanced features of Autofac that you may also find useful. The cache class in your question is an example of the Decorator Pattern. Autofac has explicit support for decorators. It also has a nice model for structuring your registrations and managing configuration information, called Modules.

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

Sidebar

Related Questions

I have an interface IRepository and an implementation EFRepository. I use structuremap injection in
I have an IRepository interface that inherits from IRepository<TObject> . I also have a
I have a logger interface that I inject into many different classes, can I
For example, you have an IRepository interface. You also have a SqlRepository class, which
Say I have this interface: public interface IRepository<T> where T : Entity { T
I have an interface ( IRepository<T> ) that is currently being extended for each
I am looking for a simple IRepository interface for my mvc web application, I
I am using Autofac as my IoC container. I have: IRepository<> , my repository
I have a repository like so: public interface IRepository { void Save<T>(T entity); void
I have implemented the repository pattern using the following generic interface. public interface IRepository<T>

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.