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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:51:24+00:00 2026-06-14T15:51:24+00:00

I’m switching over from structure map to Autofac. I’ve use a caching pattern from

  • 0

I’m switching over from structure map to Autofac. I’ve use a caching pattern from Scott Millett’s book ASP.net Design Patterns which implements an interface for both Cache and the Repository and switches in the appropriate object depending on the constructor parameter name

The interface looks like this

public interface ISchemeRepository
{
        List<Scheme> GetSchemes();
}

The cache object looks like this

public class SchemeRepository : BaseRepository, ISchemeRepository
{
/***************************************************************
 *  Properties
 ***************************************************************/

private readonly ISchemeRepository schemeRepository;

/***************************************************************
 *  Constructors
 ***************************************************************/
public SchemeRepository()
    : this(ObjectFactory.GetInstance<ISchemeRepository>(), ObjectFactory.GetInstance<IConfigurationSetting>())
{
}

public SchemeRepository(ISchemeRepository realSchemeRepository, IConfigurationSetting configurationSetting)
{
    schemeRepository = realSchemeRepository;
    this.configurationSetting = configurationSetting;
}

/**************************************************************
 *  Methods
 ***************************************************************/
public List<Scheme> GetSchemes()
{
    string key = Prefix + "Schemes";

    if (!MemoryCache.Default.Contains(key))
    {
        MemoryCache.Default.Add(key, schemeRepository.GetSchemes(), new CacheItemPolicy());
    }

    return (List<Scheme>)MemoryCache.Default.Get(key);
}

}

The repository looks like this

 public class SchemeRepository : BaseLocalRepository, ISchemeRepository
    {
        /***************************************************************
         *  Properties
         ***************************************************************/
        private readonly IConnectionSetting connectionSetting;

        /***************************************************************
         *  Constructors
         ***************************************************************/
        public SchemeRepository()
            : this(ObjectFactory.GetInstance<IConnectionSetting>())
        {
        }

        public SchemeRepository(IConnectionSetting connectionSetting)
        {
            this.connectionSetting = connectionSetting;
        }

        /**************************************************************
         *  Methods
         ***************************************************************/
        public List<Scheme> GetSchemes()
        {
            var response = new List<Scheme>();

            var conn = new SqlConnection(connectionSetting.CQBConnectionString);
            var command = new SqlCommand("proc_GetSchemes", conn) { CommandType = CommandType.StoredProcedure };

            conn.Open();
            var reader = command.ExecuteReader();
            while (reader.Read())
            {
                response.Add(
                        new Scheme
                        {
                            SchemeId = reader["Scheme_Id"].ToString().Trim(),
                            GuaranteeText = reader["Guarantee_Text"].ToString().Trim()
                        }
                    );
            }
            conn.Close();

            return response;         
        }


    }

The structure map call is below

 InstanceOf<Repository.Local.Contract.IProviderRepository>().Is.OfConcreteType<Repository.Local.Core.ProviderRepository>().WithName("RealProviderRepository");

ForRequestedType<Repository.Local.Contract.IProviderRepository>().TheDefault.Is.OfConcreteType<Repository.Local.Cache.ProviderRepository>().CtorDependency<Repository.Local.Contract.IProviderRepository>().Is(x => x.TheInstanceNamed("RealProviderRepository"));

Structure map looks at the constructor and if it contains a parameter called “realSchemeRepository” then it implements the object that connect to the database, if not it implements the cache object that checks the cache and calls the database if nothing is in the cache and populates the cache.

How do I do this in Autofac? Is there a better way of doing this in Autofac?

  • 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-06-14T15:51:26+00:00Added an answer on June 14, 2026 at 3:51 pm

    I believe what you’re asking how to do is set up a decorator between your two repository classes. I’m gonna pretend the two class names are CacheSchemeRepository and RealSchemeRepository because naming them exactly the same is confusing and terrible. Anyways…

    builder.Register(c => new RealSchemeRepository(c.Resolve<IConnectionSetting>())
           .Named<ISchemeRepository>("real");
    
    builder.RegisterDecorator<ISchemeRepository>(
        (c, inner) => new CacheSchemeRepository(inner, c.Resolve<IConfigurationSetting>()),
        "real");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.