Paul Plato
Paul Plato

Reputation: 1501

Autofac Single Instance

I have the following problem understanding how the singleinstance binding works

I have the following class listing

public interface ICacheManager
{
    object Get(string key);

    void Set(string key, object data, int cacheTime);

    bool IsSet(string key);

    void Invalidate(string key);
}

Implemented as follows

public class MemoryCacheManager : ICacheManager
{
    private ObjectCache Cache
    {
        get { return MemoryCache.Default; }
    }

    public object Get(string key)
    {
        return Cache[key];
    }

    public void Set(string key, object data, int cacheTime)
    {

        var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime) };
        Cache.Add(new CacheItem(key, data), policy);
    }

    public bool IsSet(string key)
    {
        return Cache[key] != null;
    }

    public void Invalidate(string key)
    {
        Cache.Remove(key);
    }

}

Registered in autofac as follows

 builder.RegisterType<MemoryCacheManager>().As<ICacheManager>().SingleInstance();

Now my question is this

say i have the following class listing that uses the ICacheManager dependency

public class ClassA
{
    private readonly ICacheManager _cacheManager;
    public ClassA(ICacheManager cacheManager)
    {
      _cacheManager = cacheManager;
    }

   private void BindItem(object o)
   {
     if(!_cacheManager.IsSet("SOME_KEY"))
         _cacheManager.Set("SOME_KEY", O, 60);
   }
}

After the Object O has been added to the Cache, if the ICacheManager instance is resolved at a latter time, will it contain the newly added item or not.

Upvotes: 1

Views: 10199

Answers (1)

Kaleb Pederson
Kaleb Pederson

Reputation: 46479

When an object is registered SingleInstance the exact same instance that was returned at the first request for that object will be returned for every other request for an instance of that object. So, to directly answer your question, yes. If you resolve ICacheManager from the container, add an instance to the cache, and then later request an ICacheManager instance, the second request will receive the exact same instance of the cache manager as the first request and barring your cached item having been ejected from the cache, your item would be present in the cache.

You can verify this behavior using unit tests:

// NUnit test fixture - untested, please forgive any typos
[TestFixture]
public class AutofacComprehensionTest
{
    internal interface ICacheManager {}
    internal class ConcreteCacheManager : ICacheManager {}

    [Test]
    public void SingleInstance_causes_same_instance_to_be_returned_for_each_request()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<ConcreteCacheManager>().As<ICacheManager>().SingleInstance();
        var container = builder.Build();
        var first = container.Resolve<ICacheManager>();
        var second = container.Resolve<ICacheManager>();
        Assert.That(first, Is.SameAs(second));
    }
}

Upvotes: 12

Related Questions