T IContextCache.AddOrUpdateContext <T>(ContextFactory <T> addFactory, UpdateContextFactory <T> updateFactory) { try { T previousValue = null; IContextValue <T> context = null; IContextCollection currentCollection; do { T existingValue; if (_collection.TryGetContext(out existingValue)) { if (context == null || previousValue != existingValue) { context = new ContextValue <T>(updateFactory(existingValue)); } previousValue = existingValue; currentCollection = Volatile.Read(ref _collection); Interlocked.CompareExchange(ref _collection, currentCollection.Add(context), currentCollection); } else { if (context == null) { context = new ContextValue <T>(addFactory()); } currentCollection = Volatile.Read(ref _collection); Interlocked.CompareExchange(ref _collection, currentCollection.Add(context), currentCollection); } } while (currentCollection == Volatile.Read(ref _collection)); return(context.Value); } catch (Exception exception) { throw new ContextFactoryException($"The payload factory faulted: {TypeCache<T>.ShortName}", exception); } }
T IContext.AddOrUpdateContext <T>(ContextFactory <T> addFactory, UpdateContextFactory <T> updateFactory) { return(_context.AddOrUpdateContext(addFactory, updateFactory)); }
public T AddOrUpdateContext <T>(ContextFactory <T> addFactory, UpdateContextFactory <T> updateFactory) where T : class { return(ContextCache.AddOrUpdateContext(addFactory, updateFactory)); }
public T AddOrUpdateContext <T>(ContextFactory <T> addFactory, UpdateContextFactory <T> updateFactory) where T : class { throw new ContextNotFoundException(); }