/// <inheritdoc/> public async Task UpdateAsync(IImmutable <TEntity> newValue, CancellationToken ct = default) { if (newValue == null) { if (_store.ContainsKey(_id)) { newValue = _store[_id]; _store.Remove(_id); await _notifier.StateItemRemovedAsync(_id, newValue, ct); } } else { if (_store.ContainsKey(_id)) { _store.AddOrUpdate(_id, newValue); await _notifier.StateItemUpdatedAsync(_id, newValue, ct); } else { _store.TryAdd(_id, newValue); await _notifier.StateItemAddedAsync(_id, newValue, ct); } } await _notifier.StateChangedAsync(_id, newValue, ct); }
public static void Main(String[] args) { ClassWithImmutable test = new ClassWithImmutable(); IImmutable o = test.Object; o.Value = 1; // fails }
public void ShouldBeEqual_WhenImmutablesAndClassAreAsExpected(IImmutable <SimpleTestClass> a, SimpleTestClass b, bool expected) { // Arrange / Act var result = a.Equals(b); // Assert result.Should().Be(expected); }
public void ShouldBeEqual_WhenImmutablesAreAsExpected(IImmutable a, IImmutable b, bool expected) { // Arrange / Act var result = a.Equals(b); // Assert result.Should().Be(expected); }
/// <summary> /// Constructs a <see cref="Computed{T}"/> /// </summary> /// <param name="execute">The action to execute</param> /// <param name="canExecute">The observable that indicates if the action is currently allowed</param> public Command(Action execute, IImmutable <bool> canExecute = null) { _execute = execute; if (canExecute != null) { _canExecute = canExecute; _canExecute.PropertyChanged += CanExecutePropertyChanged; } }
public TMutable GetOrCreateMutable <TMutable>(IImmutable immutable, Func <TMutable> createMutable) where TMutable : IMutable { IMutable result; if (!_immutableToMutable.TryGetValue(immutable, out result)) { _immutableToMutable.Add(immutable, result = createMutable()); } return((TMutable)result); }
public static int ImmutableHash(this IImmutable o, ref int? hashCache) { if (hashCache is null) { hashCache = 0; foreach (var tProp in GetImmutableFields(o)) { hashCache = HashCode.Combine(hashCache.Value, tProp.GetValue(o).GetHashCode()); } } return hashCache.Value; }
public static bool ImmutableEquals(this IImmutable o1, object o2) { if (ReferenceEquals(o1, o2)) return true; if (o2 is null || o1.GetType() != o2.GetType() || o1.GetHashCode() != o2.GetHashCode()) return false; foreach (var tProp in GetImmutableFields(o1)) { var test = tProp.GetValue(o1)?.Equals(tProp.GetValue(o2)); if (test is null) continue; if (!test.Value) return false; } return true; }
/// <summary> /// Returns a flatten tree of all the immutable objects under the source object, including itself /// </summary> public static IEnumerable <IImmutable> GetAllSubObjects(this IImmutable source) { if (source == null) { return(Enumerable.Empty <IImmutable>()); } var type = source.GetType(); var props = type.GetAllProperties(); var allSubObjects = props.SelectMany(prop => _getAllSubObjectsInProperty(source, prop)); return(source.Yield() .Concat(allSubObjects)); }
private static IEnumerable <IImmutable> _getAllSubObjectsInProperty(IImmutable obj, PropertyInfo prop) { if (prop.PropertyType.IsImmutableType()) { var subOjb = prop.GetValue(obj) as IImmutable; return(subOjb.GetAllSubObjects()); } if (prop.PropertyType.IsEnumreableOfImmutables()) { var list = prop.GetValue(obj) as IEnumerable; if (list != null) { return(list.Cast <IImmutable>().SelectMany(imm => imm.GetAllSubObjects())); } } return(Enumerable.Empty <IImmutable>()); }
public static IImmutable ModifyRecusively(this IImmutable source, Func <IImmutable, IImmutable> modifier, Type type) { if (source == null) { return(null); } // first check if the source is of the modifiable type and if so apply the modifier on it var sourceType = source.GetType(); if (sourceType.IsInheritedFrom(type)) { source = modifier(source); } var properties = sourceType.GetAllProperties(); // now search for all properties that are immutable and apply recursively on them as well foreach (var prop in properties) { if (prop.PropertyType.IsImmutableType()) { var subObj = prop.GetValue(source) as IImmutable; subObj = ModifyRecusively(subObj, modifier, type); source = source.With(prop, subObj); } else if (prop.PropertyType.IsEnumreableOfImmutables()) { var list = prop.GetValue(source) as IEnumerable; var modified = list .Cast <IImmutable>() .Select(imm => ModifyRecusively(imm, modifier, type)); var newList = _toImmutableList(modified, list.GetType()); source = source.With(prop, newList); } } return(source); }
public static IImmutable With(this IImmutable source, PropertyInfo property, object newValue) { return(_with(source, property, newValue) as IImmutable); }
/// <inheritdoc/> public bool TryGetValue(TId key, out IImmutable <TEntity> value) => _store.TryGetValue(key, out value);
/// <inheritdoc/> public bool TryAdd(TId id, IImmutable <TEntity> value) => _store.TryAdd(id, value);
/// <inheritdoc/> public void AddOrUpdate(TId id, IImmutable <TEntity> value) => _store.AddOrUpdate(id, value, (a, b) => value);
public static T RandomTakeOne <T>(this IImmutable <T> collection) { return(collection.ToImmutable().RandomTakeOne()); }