コード例 #1
0
        private void DumpProperties(StringBuilder sb, object obj, NestedModel nested, IList properties)
        {
            foreach (PropertyModel m in properties)
            {
                if (nested != null)
                {
                    sb.AppendFormat("{0}.", nested.Property.Name);
                }

                if ((m.PropertyAtt.ColumnType ?? "").EndsWith("Blob"))
                {
                    sb.AppendFormat("{0} = (blob); ", m.Property.Name);
                }
                else
                {
                    object value = m.Property.GetValue(obj, null);
                    if (NullableHelper.IsNull(value))
                    {
                        sb.AppendFormat("{0} = (null); ", m.Property.Name);
                    }
                    else
                    {
                        sb.AppendFormat("{0} = '{1}'; ", m.Property.Name, value);
                    }
                }
            }
        }
コード例 #2
0
        public void Benchmark_Nested_Model()
        {
            var oldModel = new NestedModel
            {
                Id    = 1,
                Child = new ChildModel
                {
                    Id         = 2,
                    Name       = "Child",
                    GrandChild = new GrandChildModel
                    {
                        Id    = 3,
                        Name  = "GrandChild",
                        Value = 100,
                    }
                }
            };
            var newModel = new NestedModel
            {
                Id    = 1,
                Child = new ChildModel
                {
                    Id         = 2,
                    Name       = "Child 2",
                    GrandChild = new GrandChildModel
                    {
                        Id    = 4,
                        Name  = "GrandChild 2",
                        Value = 200,
                    }
                }
            };

            var sw = new Stopwatch();

            sw.Start();
            var comparer = SutEngine.Get <NestedModel>();

            sw.Stop();
            var compilation = sw.ElapsedMilliseconds;

            sw.Reset();

            sw.Start();
            var numIterations = 100000;

            for (var i = 0; i < numIterations; i++)
            {
                var updates = comparer(oldModel, newModel);
            }
            sw.Stop();

            var benchmark = sw.ElapsedMilliseconds;

            Console.WriteLine($"Compilation took {compilation} ms.");
            Console.WriteLine($"{numIterations} iterations took {benchmark} ms.");
            Assert.IsTrue(benchmark < 500);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: RestCode/samples
        static void Main(string[] args)
        {
            var baseAddr = "http://localhost:8888";

            Console.WriteLine("hello------from origin http client");

            var client = new HttpClient {
                BaseAddress = new Uri(baseAddr)
            };

            var res = client.GetAsync("api/test/GetFromSimpleArg?id=world").Result.Content.ReadAsStringAsync().Result;

            Console.WriteLine(res);
            Console.WriteLine("\n\n");


            var proxy = new ProxyClient();

            Console.WriteLine("hello------from web api proxy");

            var v1 = proxy.GetFromSimpleArg("just say this is my name");

            Console.WriteLine("1---" + JsonConvert.SerializeObject(v1));

            v1 = proxy.GetFromSimpleArgAsync("just say this is my name").Result;
            Console.WriteLine("1-async---" + JsonConvert.SerializeObject(v1));

            var complexModel = new ComplexModel()
            {
                Age = 18, Name = "super star"
            };
            var v2 = proxy.GetFromComplexArg(complexModel);

            Console.WriteLine("2---" + JsonConvert.SerializeObject(v2));

            var v3 = proxy.GetFromMixedArg(2016, complexModel);

            Console.WriteLine("3---" + JsonConvert.SerializeObject(v3));


            var nest = new NestedModel()
            {
                Id           = 999999,
                ComplexModel = complexModel
            };
            var v4 = proxy.PostFromMixedArg("this is my str", nest, complexModel);

            Console.WriteLine("4---" + JsonConvert.SerializeObject(v4));

            var v5 = proxy.PostFromMixedArg2("this is my str", complexModel, nest);

            Console.WriteLine("5---" + JsonConvert.SerializeObject(v5));


            Console.WriteLine("---------------done!");
            Console.ReadLine();
        }
コード例 #4
0
        public void Benchmark_Nested_Model()
        {
            var oldModel = new NestedModel
            {
                Id = 1,
                Child = new ChildModel
                {
                    Id = 2,
                    Name = "Child",
                    GrandChild = new GrandChildModel
                    {
                        Id = 3,
                        Name = "GrandChild",
                        Value = 100,
                    }
                }
            };
            var newModel = new NestedModel
            {
                Id = 1,
                Child = new ChildModel
                {
                    Id = 2,
                    Name = "Child 2",
                    GrandChild = new GrandChildModel
                    {
                        Id = 4,
                        Name = "GrandChild 2",
                        Value = 200,
                    }
                }
            };

            var sw = new Stopwatch();

            sw.Start();
            var comparer = SutEngine.Get<NestedModel>();
            sw.Stop();
            var compilation = sw.ElapsedMilliseconds;
            sw.Reset();

            sw.Start();
            var numIterations = 100000;
            for (var i = 0; i < numIterations; i++)
            {
                var updates = comparer(oldModel, newModel);
            }
            sw.Stop();

            var benchmark = sw.ElapsedMilliseconds;
            Console.WriteLine($"Compilation took {compilation} ms.");
            Console.WriteLine($"{numIterations} iterations took {benchmark} ms.");
            Assert.IsTrue(benchmark < 500);
        }
コード例 #5
0
        public void AreDeeplyEqualShouldWorkCorrectlyWithSameReferences()
        {
            var firstObject = new NestedModel {
                Integer = 1, String = "Text"
            };
            var secondObject = new NestedModel {
                Integer = 1, String = "Text", Nested = firstObject
            };

            firstObject.Nested = secondObject;

            Assert.True(DeepEquality.AreDeeplyEqual(firstObject, secondObject));
        }
コード例 #6
0
        public void AreDeeplyEqualShouldReportCorrectly()
        {
            var firstObject = new NestedModel()
            {
                Integer = 1, String = "Text"
            };
            var secondObject = new NestedModel()
            {
                Integer = 2, String = "Text"
            };

            Assert.False(DeepEquality.AreDeeplyEqual(firstObject, secondObject, out var result));
            Assert.Equal("Difference occurs at 'NestedModel.Integer'. Expected a value of '1', but in fact it was '2'", result.ToString());
        }
コード例 #7
0
        public void Check_nested_properties()
        {
            var model = new NestedModel
            {
                Prop = new RequiredModel()
            };

            builder.BuildFromModel(model);

            builder.Invalid.Should().BeTrue();
            builder.Result.Errors.Should().ContainEquivalentOf(new
            {
                Error = "required",
                Path  = new[] { "prop", "summary" }
            });
        }
コード例 #8
0
        public void Can_deserialize_nested_json_with_whitespace()
        {
            var model = new NestedModel
            {
                Id    = "Nested with space",
                Model = new ModelWithIdAndName
                {
                    Id   = 1,
                    Name = @"Simple string"
                }
            };

            const string json = "\t { \"Id\" : \"Nested with space\" \n , \r \t \"Model\" \t : \n { \t \"Id\" \t : 1 , \t \"Name\" \t  : \t \"Simple string\" \t } \t } \n ";

            var fromJson = JsonSerializer.DeserializeFromString <NestedModel>(json);

            Assert.That(fromJson.Id, Is.EqualTo(model.Id));
            Assert.That(fromJson.Model, Is.EqualTo(model.Model));
        }
コード例 #9
0
        public void Can_deserialize_nested_json_with_whitespace()
        {
            var model = new NestedModel
            {
                Id = "Nested with space",
                Model = new ModelWithIdAndName
                {
                    Id = 1,
                    Name = @"Simple string"
                }
            };

            const string json = "\t { \"Id\" : \"Nested with space\" \n , \r \t \"Model\" \t : \n { \t \"Id\" \t : 1 , \t \"Name\" \t  : \t \"Simple string\" \t } \t } \n ";

            var fromJson = JsonSerializer.DeserializeFromString<NestedModel>(json);

            Assert.That(fromJson.Id, Is.EqualTo(model.Id));
            Assert.That(fromJson.Model, Is.EqualTo(model.Model));
        }
コード例 #10
0
        public void CanValidateNestedPropertiesFalse()
        {
            var response = new NestedModel
            {
                Phone      = "11111111111",
                Email      = "*****@*****.**",
                MyProperty = "AFDSFSD",
                Model      = new Model
                {
                    Phone      = "c",
                    Email      = "aaaaaa",
                    MyProperty = null
                }
            }
            .ValidateNested();

            Assert.IsFalse(response.IsSuccess);
            Assert.AreEqual(3, response.Results.Count);
        }
コード例 #11
0
        public void Can_deserialize_nested_json_object_with_slash_at_end_of_value()
        {
            var model = new NestedModel
            {
                Id    = "Nested with slash at the end of name",
                Model = new ModelWithIdAndName
                {
                    Id   = 1,
                    Name = "String with slashes\\",
                }
            };

            const string json = "{ \"Id\" : \"Nested with slash at the end of name\", \"Model\" : { \"Id\" : 1, \"Name\" : \"String with slashes\\\\\" } }";

            var fromJson = JsonSerializer.DeserializeFromString <NestedModel>(json);

            Assert.That(fromJson.Id, Is.EqualTo(model.Id));
            Assert.That(fromJson.Model, Is.EqualTo(model.Model));
        }
コード例 #12
0
        public void GetValidateNestedPropertiesResultMessages()
        {
            var response = new NestedModel
            {
                Phone      = "11111111111",
                Email      = "*****@*****.**",
                MyProperty = "AFDSFSD",
                Model      = new Model
                {
                    Phone      = "c",
                    Email      = "aaaaaa",
                    MyProperty = null
                }
            }
            .ValidateNested();

            Assert.AreEqual(response.Results[0].Message, "The  field is not a valid phone number.");
            Assert.AreEqual(response.Results[1].Message, "The  field is not a valid e-mail address.");
            Assert.AreEqual(response.Results[2].Message, "The  field is required.");
        }
コード例 #13
0
        public void GetValidateNestedPropertiesResultKeys()
        {
            var response = new NestedModel
            {
                Phone      = "11111111111",
                Email      = "*****@*****.**",
                MyProperty = "AFDSFSD",
                Model      = new Model
                {
                    Phone      = "c",
                    Email      = "aaaaaa",
                    MyProperty = null
                }
            }
            .ValidateNested();

            Assert.AreEqual(response.Results[0].Key, "Phone");
            Assert.AreEqual(response.Results[1].Key, "Email");
            Assert.AreEqual(response.Results[2].Key, "MyProperty");
        }
コード例 #14
0
        public void ShouldMapFromNestedModelToFluentObject()
        {
            var nestedModel = new NestedModel(false);

            var fluentObject = nestedModel.MapToFluentObject();

            Assert.IsNotNull(fluentObject);
            Assert.AreEqual(fluentObject.Name, NestedModelFields.MODEL_NAME);

            Assert.IsTrue(fluentObject.ContainsKey(NestedModelFields.FIELD_NAME));
            Assert.IsTrue(fluentObject.ContainsKey(NestedModelFields.CONDITIONAL_FIELD_NAME));
            Assert.IsFalse(fluentObject.ContainsKey(NestedModelFields.CONDITIONAL_REVERSED_FIELD_NAME));
            Assert.IsFalse(fluentObject.ContainsKey(NestedModelFields.READONLY_FIELD_NAME));
            Assert.IsTrue(fluentObject.ContainsKey(NestedModelFields.DEPENDENT_FIELD_NAME));
            Assert.IsFalse(fluentObject.ContainsKey(NestedModelFields.DEPENDENT_REVERSED_FIELD_NAME));

            Assert.AreEqual(fluentObject[NestedModelFields.FIELD_NAME], NestedModelFields.FIELD_VALUE);
            Assert.AreEqual(fluentObject[NestedModelFields.CONDITIONAL_FIELD_NAME], NestedModelFields.CONDITIONAL_FIELD_VALUE.ToString());
            Assert.AreEqual(fluentObject[NestedModelFields.DEPENDENT_FIELD_NAME], NestedModelFields.DEPENDENT_FIELD_VALUE.ToString());
        }
コード例 #15
0
        public void AreDeeplyEqualShouldReportCorrectlyWithNestedObjects()
        {
            var firstObject = new NestedModel {
                Integer = 1, String = "Text"
            };
            var secondObject = new NestedModel {
                Integer = 2, String = "Text"
            };

            Assert.False(DeepEquality.AreDeeplyEqual(firstObject, secondObject, out var result));
            Assert.Equal("Difference occurs at 'NestedModel.Integer'. Expected a value of '1', but in fact it was '2'", result.ToString());

            Assert.False(DeepEquality.AreDeeplyEqual(
                             new NestedModel
            {
                Integer = 1,
                String  = "test",
                Enum    = CustomEnum.ConstantWithCustomValue,
                Nested  = new NestedModel {
                    Integer = 2, String = "test2", Nested = new NestedModel {
                        Integer = 3, String = "test3"
                    }
                }
            },
                             new NestedModel
            {
                Integer = 1,
                String  = "test",
                Enum    = CustomEnum.ConstantWithCustomValue,
                Nested  = new NestedModel {
                    Integer = 2, String = "test1", Nested = new NestedModel {
                        Integer = 3, String = "test3"
                    }
                }
            }, out result));

            Assert.Equal("Difference occurs at 'NestedModel.Nested.String'. Expected a value of 'test2', but in fact it was 'test1'", result.ToString());
        }
コード例 #16
0
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
        {
            return(null);
        }
        // Load JObject from stream
        JObject jObject = JObject.Load(reader);
        // Create target object based on JObject
        NestedModel target = new NestedModel();
        // Populate the object properties
        StringWriter writer = new StringWriter();

        serializer.Serialize(writer, jObject);
        using (JsonTextReader newReader = new JsonTextReader(new StringReader(writer.ToString())))
        {
            newReader.Culture              = reader.Culture;
            newReader.DateParseHandling    = reader.DateParseHandling;
            newReader.DateTimeZoneHandling = reader.DateTimeZoneHandling;
            newReader.FloatParseHandling   = reader.FloatParseHandling;
            serializer.Populate(newReader, target);
        }
        return(target);
    }
コード例 #17
0
        public void Extract_Structure_ShouldMapToNestedObject()
        {
            // Arrange
            var          structHandle = (IntPtr)443534;
            var          intValue     = 123;
            RfcErrorInfo errorInfo;

            _interopMock.Setup(x => x.GetStructure(It.IsAny <IntPtr>(), It.IsAny <string>(), out structHandle, out errorInfo));
            _interopMock.Setup(x => x.GetInt(It.IsAny <IntPtr>(), It.IsAny <string>(), out intValue, out errorInfo));

            // Act
            NestedModel result = OutputMapper.Extract <NestedModel>(_interopMock.Object, DataHandle);

            // Assert
            _interopMock.Verify(
                x => x.GetStructure(DataHandle, "INNERMODEL", out structHandle, out errorInfo),
                Times.Once);
            _interopMock.Verify(
                x => x.GetInt(structHandle, "VALUE", out intValue, out errorInfo),
                Times.Once);
            result.Should().NotBeNull();
            result.InnerModel.Should().NotBeNull();
            result.InnerModel.Value.Should().Be(123);
        }
コード例 #18
0
ファイル: DemoOne.cs プロジェクト: RestCode/samples
 /// <summary>
 ///
 /// </summary>
 /// <param name="simpleStr"></param>
 /// <param name="uriComplexArg"></param>
 /// <param name="bodyNestedArg"></param>
 public virtual TotalResult PostFromMixedArg2(String simpleStr, ComplexModel uriComplexArg, NestedModel bodyNestedArg)
 {
     return(PostFromMixedArg2Async(simpleStr, uriComplexArg, bodyNestedArg).Result);
 }
コード例 #19
0
ファイル: DemoOne.cs プロジェクト: RestCode/samples
 protected virtual List <KeyValuePair <string, object> > GeneratePostFromMixedArg2KeyValueList(String simpleStr, ComplexModel uriComplexArg, NestedModel bodyNestedArg)
 {
     // Name={Name}&Age={Age}
     throw new NotImplementedException();
 }
コード例 #20
0
ファイル: DemoOne.cs プロジェクト: RestCode/samples
        protected virtual string GeneratePostFromMixedArg2QueryString(String simpleStr, ComplexModel uriComplexArg, NestedModel bodyNestedArg)
        {
            var kvList = GeneratePostFromMixedArg2KeyValueList(simpleStr, uriComplexArg, bodyNestedArg);
            var urlTpl = GenerateQueryStrFromKvList(kvList);

            return(urlTpl);
        }
コード例 #21
0
ファイル: DemoOne.cs プロジェクト: RestCode/samples
        /// <summary>
        ///
        /// </summary>
        /// <param name="simpleStr"></param>
        /// <param name="uriComplexArg"></param>
        /// <param name="bodyNestedArg"></param>
        /// <returns></returns>
        public virtual async Task <TotalResult> PostFromMixedArg2Async(String simpleStr, ComplexModel uriComplexArg, NestedModel bodyNestedArg)
        {
            var requestUrl = "api/test/PostFromMixedArg2";


            var queryHasParamUrl = "simpleStr=" + Uri.EscapeDataString(Convert.ToString(simpleStr)) + "";



            var queryNoParamUrl = GeneratePostFromMixedArg2QueryString(simpleStr, uriComplexArg, bodyNestedArg);


            if (string.IsNullOrEmpty(queryHasParamUrl))
            {
                requestUrl = requestUrl + "?" + queryNoParamUrl;
            }
            else
            {
                requestUrl = requestUrl + "?" + queryHasParamUrl + "&" + queryNoParamUrl;
            }


            var result = await HttpClient.PostAsJsonAsync <NestedModel>(requestUrl, bodyNestedArg);


            EnsureSuccess(result);

            return(await result.Content.ReadAsAsync <TotalResult>());
        }
コード例 #22
0
        public void AreDeeplyEqualShouldWorkCorrectlyWithSameReferences()
        {
            var firstObject = new NestedModel { Integer = 1, String = "Text" };
            var secondObject = new NestedModel { Integer = 1, String = "Text", Nested = firstObject };
            firstObject.Nested = secondObject;

            Assert.True(Reflection.AreDeeplyEqual(firstObject, secondObject));
        }
コード例 #23
0
ファイル: Issue366Tests.cs プロジェクト: wtf3505/home
 public Model()
 {
     NestedModel = new NestedModel();
 }
コード例 #24
0
        public void Can_deserialize_nested_json_object_with_slash_at_end_of_value()
        {
            var model = new NestedModel
            {
                Id = "Nested with slash at the end of name",
                Model = new ModelWithIdAndName
                {
                    Id = 1,
                    Name = "String with slashes\\",
                }
            };

            const string json = "{ \"Id\" : \"Nested with slash at the end of name\", \"Model\" : { \"Id\" : 1, \"Name\" : \"String with slashes\\\\\" } }";

            var fromJson = JsonSerializer.DeserializeFromString<NestedModel>(json);

            Assert.That(fromJson.Id, Is.EqualTo(model.Id));
            Assert.That(fromJson.Model, Is.EqualTo(model.Model));
        }
コード例 #25
0
ファイル: Program.cs プロジェクト: RestCode/samples
 protected override List <KeyValuePair <string, object> > GeneratePostFromMixedArg2KeyValueList(string simpleStr, ComplexModel uriComplexArg, NestedModel bodyNestedArg)
 {
     return(new List <KeyValuePair <string, object> >()
     {
         new KeyValuePair <string, object>("Name", uriComplexArg.Name),
         new KeyValuePair <string, object>("Age", uriComplexArg.Age),
     });
 }