internal T GetOrAddScopedInstance <T>(Func <Scoped, T> factory, Type key) where T : notnull { SinglyLinkedListKeyNode <Type, object>?initialValue = _scopedInstances; SinglyLinkedListKeyNode <Type, object>?current = initialValue; while (current != null) { if (ReferenceEquals(current.Key, key)) { return((T)current.Value); } current = current.Next; } //There is a very slight chance that this instance is created more than once under heavy load. //In that case the duplicate will be discarded. T obj = factory(this); SinglyLinkedListKeyNode <Type, object> computedValue = initialValue.Add(key, obj); if (ReferenceEquals(Interlocked.CompareExchange(ref _scopedInstances, computedValue, initialValue), initialValue)) { return(obj); } else { return(HandleScopeThreadCollision(obj, key)); } }
public void Enumerate_EnumerableNonGenericType() { SinglyLinkedListKeyNode <int, int> list = null; list = list.Add(0, 1); list = list.Add(1, 2); list = list.Add(2, 3); int[] results = ((IEnumerable)list).OfType <int>().ToArray(); Assert.Equal(3, results.Length); Assert.Equal(3, results[0]); Assert.Equal(2, results[1]); Assert.Equal(1, results[2]); }
public void Enumerate_ConcreteType() { SinglyLinkedListKeyNode <int, int> list = null; list = list.Add(0, 1); list = list.Add(1, 2); list = list.Add(2, 3); int[] results = list.ToArray(); Assert.Equal(3, results.Length); Assert.Equal(3, results[0]); Assert.Equal(2, results[1]); Assert.Equal(1, results[2]); }
/// <summary> /// Disposes the scope, calling all dispose actions on the instances that where created in this scope. /// </summary> public void Dispose() { SinglyLinkedListNode <IDisposable>?disposables = Interlocked.Exchange(ref _disposables, null); while (disposables != null) { disposables.Value.Dispose(); disposables = disposables.Next; } SinglyLinkedListKeyNode <ServiceBinding, ActionList <object> >?finalizers = Interlocked.Exchange(ref _finalizers, null);; while (finalizers != null) { finalizers.Value.Invoke(); finalizers = finalizers.Next; } }
/// <summary> /// Disposes the scope, calling all dispose actions on the instances that where created in this scope. /// </summary> public void Dispose() { SinglyLinkedListNode <IDisposable>?disposables = _disposables; while (disposables != null) { disposables.Value.Dispose(); disposables = disposables.Next !; } SinglyLinkedListKeyNode <ServiceBinding, ActionList <object> >?finalizers = _finalizers; while (finalizers != null) { finalizers.Value.Invoke(); finalizers = finalizers.Next; } }
private T HandleScopeThreadCollision <T>(T obj, Type key) { SinglyLinkedListKeyNode <Type, object>?initialValue, computedValue; do { initialValue = _scopedInstances; SinglyLinkedListKeyNode <Type, object> current = initialValue; while (current != null) { if (ReferenceEquals(current.Key, key)) { return((T)current.Value); } current = current.Next !; } computedValue = initialValue.Add(key, obj); }while (!ReferenceEquals(Interlocked.CompareExchange(ref _scopedInstances, computedValue, initialValue), initialValue)); return(obj); }