예제 #1
0
        public void PlainObjectsWithNoInheritance()
        {
            const PrefixStyle prefix     = PrefixStyle.Base128;
            const string      memberName = "Id";

            using (var ms = new MemoryStream())
            {
                var items = new List <PlainObject>();
                for (var i = 0; i < 10; i++)
                {
                    items.Add(PlainObject.WithId(i));
                }

                foreach (var item in items)
                {
                    _model.SerializeWithLengthPrefix(ms, item, prefix);
                }

                var builder = new ProjectionTypeBuilder(_model);

                var type = builder.GetTypeForProjection(typeof(PlainObject), typeof(PlainObject).GetMember(memberName));

                ms.Seek(0, SeekOrigin.Begin);
                var deserializedItems = _model.DeserializeItems(ms, type, prefix, 0, null).Cast <object>().ToArray();

                Assert.AreEqual(items.Count, deserializedItems.Length);

                var fi = (FieldInfo)type.GetMember(memberName)[0];
                for (var i = 0; i < items.Count; i++)
                {
                    Assert.AreEqual(items[i].Id, (int)fi.GetValue(deserializedItems[i]));
                }
            }
        }
예제 #2
0
            public static PlainObject WithId(int i)
            {
                var po = new PlainObject {
                    Id = i
                };

                po.Name    = po.GetHashCode().ToString(CultureInfo.InvariantCulture);
                po.Surname = po.Name;
                return(po);
            }
예제 #3
0
        public void TestGenerate_PlainObject()
        {
            var obj = new PlainObject
            {
                Name  = "Age",
                Value = "26"
            };
            var records = this.builder.Generate(obj, "Prefix").ToArray();

            CollectionAssert.AreEquivalent(new[]
            {
                new DbStringRecord("Prefix", "True"),
                new DbStringRecord("PrefixName", "Age"),
                new DbStringRecord("PrefixValue", "26")
            }, records);
        }
예제 #4
0
        public void TestSave_PlainObject()
        {
            var entity = new PlainObject
            {
                Name  = "UserName",
                Value = "Blueve"
            };

            try
            {
                this.dbContext.Save(entity);
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
            }
        }
예제 #5
0
        public void TestGenerateForList_PlainObjectList()
        {
            this.db.StringSet("Prefix", "2");
            this.db.StringSet("Prefix0", "True");
            this.db.StringSet("Prefix0Name", "Tom");
            this.db.StringSet("Prefix1", "True");
            this.db.StringSet("Prefix1Value", "18");

            var proxyList = this.generator.GenerateForList <PlainObject>("Prefix");

            Assert.AreEqual(2, proxyList.Count);
            Assert.AreEqual("Tom", proxyList[0].Name);
            Assert.AreEqual("18", proxyList[1].Value);

            proxyList[0] = new PlainObject {
                Name = "Speike"
            };
            Assert.AreEqual("Speike", this.db.StringGet("Prefix0Name").ToString());
        }
예제 #6
0
        public void TriggerInvalidateMember()
        {
            var obj = new PlainObject {
                string_property = "Hello",
            };
            var left = "";

            var b = Binding.Create(() => left == obj.string_property);

            Assert.AreEqual(obj.string_property, left);

            obj.string_property = "Goodbye";

            Binding.InvalidateMember(() => obj.string_property);

            Assert.AreEqual("Goodbye", left);

            b.Unbind();

            obj.string_property = "Hello Again";

            Assert.AreEqual("Goodbye", left);
        }
        private static IValidatorProvider CreateValidator(Type modelType, string propertyKey, ValueAttribute attribute, Func <string, object> deserializer)
        {
            Func <IResourceContext, IProxy> argumentProvider;
            var argument = attribute.Argument;

            if (argument is string expression)
            {
                var boundExpression = BoundExpression.Parse(expression);
                if (boundExpression.IsPlainString)
                {
                    var literal = new PlainObject(deserializer != null
                        ? deserializer(boundExpression.StringFormat)
                        : boundExpression.StringFormat);

                    argumentProvider = context => literal;
                }
                else
                {
                    var getString = boundExpression.StringFormat != null;
                    var action    = attribute.ArgumentUpdatedAction;
                    var notify    = action == ValidationAction.ClearErrors || action == ValidationAction.ValidateField;
                    argumentProvider = context =>
                    {
                        var value = getString
                            ? (IProxy)boundExpression.GetStringValue(context)
                            : boundExpression.GetValue(context);

                        if (notify)
                        {
                            value.ValueChanged = () =>
                            {
                                object model;
                                try
                                {
                                    model = context.GetModelInstance();
                                }
                                catch
                                {
                                    // Something went wrong so it's best to
                                    // disable the feature entirely.
                                    value.ValueChanged = null;
                                    return;
                                }

                                if (model is ExpandoObject && modelType == null)
                                {
                                    // Do nothing.
                                }
                                else if (model == null || model.GetType() != modelType)
                                {
                                    // Self dispose when form indicates model change.
                                    value.ValueChanged = null;
                                    return;
                                }

                                if (action == ValidationAction.ValidateField)
                                {
                                    ModelState.Validate(model, propertyKey);
                                }
                                else
                                {
                                    ModelState.ClearValidationErrors(model, propertyKey);
                                }
                            };
                        }

                        return(value);
                    };
                }
            }
            else
            {
                var literal = new PlainObject(argument);
                argumentProvider = context => literal;
            }

            BindingProxy ValueProvider(IResourceContext context)
            {
                var key = new BindingProxyKey(propertyKey);

                if (context.TryFindResource(key) is BindingProxy proxy)
                {
                    return(proxy);
                }

                proxy = new BindingProxy();
                context.AddResource(key, proxy);
                return(proxy);
            }

            Func <IResourceContext, IBoolProxy> isEnforcedProvider;

            switch (attribute.When)
            {
            case null:
                isEnforcedProvider = context => new PlainBool(true);
                break;

            case string expr:
                var boundExpression = BoundExpression.Parse(expr);
                if (!boundExpression.IsSingleResource)
                {
                    throw new ArgumentException(
                              "The provided value must be a bound resource or a literal bool value.", nameof(attribute));
                }

                isEnforcedProvider = context => boundExpression.GetBoolValue(context);
                break;

            case bool b:
                isEnforcedProvider = context => new PlainBool(b);
                break;

            default:
                throw new ArgumentException(
                          "The provided value must be a bound resource or a literal bool value.", nameof(attribute));
            }

            Func <IResourceContext, IErrorStringProvider> errorProvider;
            var message = attribute.Message;

            if (message == null)
            {
                switch (attribute.Condition)
                {
                case Must.BeGreaterThan:
                    message = "Value must be greater than {Argument}.";
                    break;

                case Must.BeGreaterThanOrEqualTo:
                    message = "Value must be greater than or equal to {Argument}.";
                    break;

                case Must.BeLessThan:
                    message = "Value must be less than {Argument}.";
                    break;

                case Must.BeLessThanOrEqualTo:
                    message = "Value must be less than or equal to {Argument}.";
                    break;

                case Must.BeEmpty:
                    message = "@Field must be empty.";
                    break;

                case Must.NotBeEmpty:
                    message = "@Field cannot be empty.";
                    break;

                default:
                    message = "@Invalid value.";
                    break;
                }
            }

            {
                var func            = new Func <IResourceContext, IProxy>(ValueProvider);
                var boundExpression = BoundExpression.Parse(message, new Dictionary <string, object>
                {
                    ["Value"]    = func,
                    ["Argument"] = argumentProvider
                });

                if (boundExpression.IsPlainString)
                {
                    var errorMessage = boundExpression.StringFormat;
                    errorProvider = context => new PlainErrorStringProvider(errorMessage);
                }
                else
                {
                    if (boundExpression.Resources.Any(
                            res => res is DeferredProxyResource resource && resource.ProxyProvider == func))
                    {
                        errorProvider =
                            context => new ValueErrorStringProvider(boundExpression.GetStringValue(context),
                                                                    ValueProvider(context));
                    }
 public static PlainObject WithId(int i)
 {
     var po = new PlainObject { Id = i };
     po.Name = po.GetHashCode().ToString(CultureInfo.InvariantCulture);
     po.Surname = po.Name;
     return po;
 }
예제 #9
0
        public void TriggerInvalidateMember()
        {
            var obj = new PlainObject {
                string_property = "Hello",
            };
            var left = "";

            var b = Binding.Create (() => left == obj.string_property);

            Assert.AreEqual (obj.string_property, left);

            obj.string_property = "Goodbye";

            Binding.InvalidateMember(() => obj.string_property);

            Assert.AreEqual ("Goodbye", left);

            b.Unbind ();

            obj.string_property = "Hello Again";

            Assert.AreEqual ("Goodbye", left);
        }