mardi 11 septembre 2018

Autofac Single Instance with event handlers

I have a cache class that is registered as a single instance with Autofac. Whenever i clear the cache i call the method ExecuteCacheCleared();

The cache class looks like this

public IEnumerable<ICacheCleared> _cacheCleared { get; private set; }

public CacheService(IEnumerable<ICacheCleared> cacheCleared) : ICacheService
{
    _cacheCleared = cacheCleared;
}

private void ExecuteCacheCleared()
{
    if (_cacheCleared != null)
    {
        foreach (var cacheCleared in _cacheCleared)
        {
            cacheCleared.EntityCacheChanged();
        }
    }
}

I then have several concrete implementations of ICacheCleared that are called when ExecuteCacheCleared is called.

So currently i am registering bits in autofac as follows

builder.RegisterType<CacheService>().As<ICacheService>().SingleInstance();
builder.RegisterType<CacheCleared>().As<ICacheCleared>().InstancePerRequest();

With the above i get an error (which i get) because the SingleInstance won't work with the InstancePerRequest but in my CacheCleared concrete class i also inject other dependencies that need to be InstancePerRequest.

Hopefully you can see what i am trying to achieve (basicly trying to notify subscribing classes of changes) but i'm stuck on how to achieve this.

Aucun commentaire:

Enregistrer un commentaire