/// <summary> /// 获取当前数据更新模型上的给定成员上的数据更新定义。 /// </summary> /// <param name="property">当前数据更新模型上的属性成员。</param> /// <param name="parentPath">从更新模型的根到当前更新模型的父节点的路径。</param> /// <returns>当前数据更新模型上的给定成员上的数据更新定义。若给定的成员没有任何更新数据,返回 null。</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="property"/> 为 null。 /// </exception> private UpdateDefinition <TRootEntity> ResolveMemberUpdateDefinition(PropertyInfo property, ObjectPath parentPath) { Contract.NotNull(property, nameof(property)); // 如果成员的值是一个 Maybe<T> 类型,则将其展开为内部值 var memberValue = property.GetValue(this); if (memberValue != null && MaybeUtils.IsMaybe(memberValue)) { var maybe = MaybeUtils.Unbox(memberValue); if (maybe.HasValue) { memberValue = maybe.Value; } else { return(null); } } if (memberValue is UpdateInfo <TRootEntity> memberUpdateInfo) { var currentPath = parentPath.Push(property.Name); return(memberUpdateInfo.CreateUpdateDefinition(currentPath)); } return(property.GetCustomAttribute <UpdateVerbAttribute>() ?.Resolve <TRootEntity>(property, memberValue, parentPath)); }
public void TestUnbox() { var maybe = MaybeUtils.Unbox(new Maybe <int>(10)); Assert.IsTrue(maybe.HasValue); Assert.IsInstanceOf <int>(maybe.Value); Assert.AreEqual(10, maybe.Value); }
/// <inheritdoc /> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } var maybeValue = MaybeUtils.Unbox(value); if (!maybeValue.HasValue) { // 对于空的 Optional<T> 实例,向 JSON 输出写入 undefined writer.WriteUndefined(); return; } serializer.Serialize(writer, maybeValue.Value); }
/// <inheritdoc /> public override bool IsValid(object value) { if (value == null) { return(true); } if (!MaybeUtils.IsMaybe(value)) { return(true); } var maybe = MaybeUtils.Unbox(value); if (!maybe.HasValue) { return(true); } var innerAttr = ActivateInnerValidationAttribute(); return(innerAttr.IsValid(maybe.Value)); }
public void TestUnboxEmpty() { var maybe = MaybeUtils.Unbox(new Maybe <int>()); Assert.IsFalse(maybe.HasValue); }
public void TestUnboxNonMaybe() { Assert.Throws <ArgumentException>(() => MaybeUtils.Unbox(10)); }
public void TestUnboxNull() { Assert.Throws <ArgumentNullException>(() => MaybeUtils.Unbox(null)); }