Exemple #1
0
        /// <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));
        }
Exemple #2
0
        /// <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));
        }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
 public void TestIsMaybeGenericFalse()
 {
     Assert.IsFalse(MaybeUtils.IsMaybe <int>(10));
     Assert.IsFalse(MaybeUtils.IsMaybe <int>(new Maybe <short>(10)));
 }
Exemple #5
0
 public void TestIsMaybeGenericTrue()
 {
     Assert.IsTrue(MaybeUtils.IsMaybe <int>(new Maybe <int>()));
     Assert.IsTrue(MaybeUtils.IsMaybe <int>(new Maybe <int>(10)));
 }
Exemple #6
0
 public void TestIsMaybeGenericNull()
 {
     Assert.Throws <ArgumentNullException>(() => MaybeUtils.IsMaybe <object>(null));
 }
Exemple #7
0
 public void TestIsMaybeFalse()
 {
     Assert.IsFalse(MaybeUtils.IsMaybe(10));
 }
Exemple #8
0
 public void TestIsMaybeTrue()
 {
     Assert.IsTrue(MaybeUtils.IsMaybe(new Maybe <int>()));
     Assert.IsTrue(MaybeUtils.IsMaybe(new Maybe <object>(new object())));
 }
Exemple #9
0
 public void TestIsMaybeNull()
 {
     Assert.Throws <ArgumentNullException>(() => MaybeUtils.IsMaybe(null));
 }