问题
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