internal CompareExchangeValue <T> Create <T>(T item) { AssertState(); if (_value != null) { throw new InvalidOperationException($"The compare exchange value with key '{_key}' is already tracked."); } _index = 0; var value = new CompareExchangeValue <T>(_key, _index, item); _value = value; _state = CompareExchangeValueState.Created; return(value); }
internal CompareExchangeValue <T> GetValue <T>(DocumentConventions conventions) { switch (_state) { case CompareExchangeValueState.None: case CompareExchangeValueState.Created: { if (_value is CompareExchangeValue <T> v) { return(v); } if (_value != null) { throw new InvalidOperationException("Value cannot be null."); } T entity = default; if (_originalValue != null && _originalValue.Value != null) { var type = typeof(T); if (type.IsPrimitive || type == typeof(string)) { _originalValue.Value.TryGet(Constants.CompareExchange.ObjectFieldName, out entity); } else { entity = conventions.Serialization.DefaultConverter.FromBlittable <T>(_originalValue.Value, _key); } } var value = new CompareExchangeValue <T>(_key, _index, entity); _value = value; return(value); } case CompareExchangeValueState.Missing: case CompareExchangeValueState.Deleted: return(null); default: throw new NotSupportedException($"Not supported state: '{_state}'"); } }