开发者

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

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()
    {开发者_如何学Python
        // 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; }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜