StructureMap - Can the state of cached entity be inspected in a custom ILifecycle?

核能气质少年 提交于 2020-01-05 03:56:28

问题


I'm spiking a custom StructureMap ILifecycle with semantics similar to the SingletonLifecycle but with a predicate that must be met for the existing instance to be returned.

The idea is that the custom lifecycle class will take a predicate as a constructor parameter, and that if the predicate is not met then a new empty cache will be returned. So far I've based the code on the existing SingletonLifecycle but can see no way of inspecting the state of the cached entity.

  • From within an ILifecycle is it possible to retrieve and inspect the cached entity?
  • StructureMap folks - is this even a valid approach?

Code so far is as follows:

public class PredicatedLifecycle<T> : ILifecycle
{
    public PredicatedLifecycle(Predicate<T> predicate)
    {
        Predicate = predicate;
    }

    private Predicate<T> Predicate { get; set; }
    private readonly MainObjectCache _cache = new MainObjectCache();

    public void EjectAll()
    {
        _cache.DisposeAndClear();
    }

    public IObjectCache FindCache()
    {
        // TODO : Inspect the cache for an instance of T
        // if it exists retrieve from the cache and invoke 
        // the predicate against it
        // replace the cache if predicate returns false
        return _cache;
    }

    public string Scope
    {
        get { return GetType().Name; }
    }
}

来源:https://stackoverflow.com/questions/4956821/structuremap-can-the-state-of-cached-entity-be-inspected-in-a-custom-ilifecycl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!