/// <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)); }
/// <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)); }
/// <inheritdoc /> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Undefined) { // undefined 表示空的 Maybe<T> 实例。直接使用 Activator 实例化即可。 existingValue = Activator.CreateInstance(objectType); return(existingValue); } var valueType = MaybeUtils.GetInnerType(objectType); var value = serializer.Deserialize(reader, valueType); if (existingValue != null && MaybeUtils.IsMaybe(existingValue)) { MaybeUtils.SetInnerValue(existingValue, value); } else { existingValue = MaybeUtils.Create(valueType, value); } return(existingValue); }
public void TestIsMaybeGenericFalse() { Assert.IsFalse(MaybeUtils.IsMaybe <int>(10)); Assert.IsFalse(MaybeUtils.IsMaybe <int>(new Maybe <short>(10))); }
public void TestIsMaybeGenericTrue() { Assert.IsTrue(MaybeUtils.IsMaybe <int>(new Maybe <int>())); Assert.IsTrue(MaybeUtils.IsMaybe <int>(new Maybe <int>(10))); }
public void TestIsMaybeGenericNull() { Assert.Throws <ArgumentNullException>(() => MaybeUtils.IsMaybe <object>(null)); }
public void TestIsMaybeFalse() { Assert.IsFalse(MaybeUtils.IsMaybe(10)); }
public void TestIsMaybeTrue() { Assert.IsTrue(MaybeUtils.IsMaybe(new Maybe <int>())); Assert.IsTrue(MaybeUtils.IsMaybe(new Maybe <object>(new object()))); }
public void TestIsMaybeNull() { Assert.Throws <ArgumentNullException>(() => MaybeUtils.IsMaybe(null)); }