public void register_a_property_override_for_a_string_conversion()
        {
            configure(x =>
            {
                //specific override formatting for Address objects named Shipping
                x.IfPropertyMatches <Address>(prop => prop.Name == "Shipping").ConvertBy(
                    a => "{1}-{0}".ToFormat(a.Address1, a.City));

                //default formatting for Address objects
                x.IfIsType <Address>().ConvertBy(a => "{0}, {1}".ToFormat(a.Address1, a.City));
            });

            var address = new Address {
                Address1 = "2050 Ozark",
                City     = "Joplin"
            };

            const string expectedDefaultFormatting  = "2050 Ozark, Joplin";
            const string expectedOverrideFormatting = "Joplin-2050 Ozark";
            var          billingRequest             = new GetStringRequest(ReflectionHelper.GetAccessor <FakeSite>(s => s.Billing), address,
                                                                           locator);
            var shippingRequest = new GetStringRequest(ReflectionHelper.GetAccessor <FakeSite>(s => s.Shipping), address,
                                                       locator);

            stringifier.GetString(billingRequest).ShouldEqual(expectedDefaultFormatting);
            stringifier.GetString(shippingRequest).ShouldEqual(expectedOverrideFormatting);
        }
        public void register_a_property_override_for_a_string_conversion_passing_raw_value()
        {
            object passedRawValue = null;

            configure(x =>
            {
                //specific override formatting for Address objects named Shipping
                x.IfPropertyMatches <Address>(prop => prop.Name == "Shipping").ConvertBy((req, value) =>
                {
                    passedRawValue = req.RawValue;
                    return("{1}-{0}".ToFormat(value.Address1, value.City));
                });

                //default formatting for Address objects
                x.IfIsType <Address>().ConvertBy(a => "{0}, {1}".ToFormat(a.Address1, a.City));
            });

            var address = new Address();

            var shippingRequest = new GetStringRequest(ReflectionHelper.GetAccessor <FakeSite>(s => s.Shipping), address,
                                                       locator);

            stringifier.GetString(shippingRequest);

            passedRawValue.ShouldBeTheSameAs(address);
        }
        /// <summary>
        /// Formats the provided value using the accessor accessor metadata and a custom format
        /// </summary>
        /// <param name="formatter">The formatter</param>
        /// <param name="modelType">The type of the model to which the accessor belongs (i.e. Case where the accessor might be on its base class WorkflowItem)</param>
        /// <param name="accessor">The property that holds the given value</param>
        /// <param name="value">The data to format</param>
        /// <param name="format">The custom format specifier</param>
        public static string FormatValue(this IDisplayFormatter formatter, Type modelType, Accessor accessor,
            object value, string format)
        {
            var request = new GetStringRequest(accessor, value, null, format, null);

            return formatter.GetDisplay(request);
        }
Example #4
0
        public void property_type_is_determined_from_the_prop_if_no_accessor()
        {
            var property = ReflectionHelper.GetProperty <Case>(x => x.Identifier);
            var request  = new GetStringRequest(property, "something");

            request.OwnerType.ShouldEqual(typeof(Case));
            request.PropertyType.ShouldEqual(typeof(string));
        }
        public void property_type_is_determined_from_the_prop_if_no_accessor()
        {
            var property = ReflectionHelper.GetProperty<Case>(x => x.Identifier);
            var request = new GetStringRequest(property, "something");

            request.OwnerType.ShouldEqual(typeof (Case));
            request.PropertyType.ShouldEqual(typeof (string));
        }
Example #6
0
        public string Format(GetStringRequest request, DateTime?time)
        {
            if (time == null)
            {
                return(string.Empty);
            }

            return(Format(request, time.Value));
        }
Example #7
0
        public void when_creating_a_string_request_with_services_via_constructor()
        {
            var locator  = MockRepository.GenerateMock <IServiceLocator>();
            var accessor = ReflectionHelper.GetAccessor <Case>(x => x.Contact.FirstName);

            var request = new GetStringRequest(accessor, "something", locator);

            request.PropertyType.ShouldEqual(typeof(string));
            request.OwnerType.ShouldEqual(typeof(Contact));
        }
        public void when_creating_a_string_request_with_services_via_constructor()
        {
            var locator = MockRepository.GenerateMock<IServiceLocator>();
            var accessor = ReflectionHelper.GetAccessor<Case>(x => x.Contact.FirstName);

            var request = new GetStringRequest(accessor, "something", locator);

            request.PropertyType.ShouldEqual(typeof (string));
            request.OwnerType.ShouldEqual(typeof (Contact));
        }
Example #9
0
 public Func <object, string> ToFormatter(IDisplayFormatter formatter)
 {
     return(o =>
     {
         var request = new GetStringRequest(_accessor, o, null)
         {
             Format = "{0:s}"
         };
         return formatter.GetDisplay(request);
     });
 }
        public string GetString(GetStringRequest request)
        {
            if (request == null || request.RawValue == null || (request.RawValue as String) == string.Empty)
                return string.Empty;
            PropertyOverrideStrategy propertyOverride = _overrides.FirstOrDefault(o => o.Matches(request.Property));

            if (propertyOverride != null)
            {
                return propertyOverride.StringFunction(request);
            }

            return findConverter(request)(request);
        }
        public void stringifier_can_use_a_service_to_get_at_a_display()
        {
            locator.Add <IWidgetDisplayer>(new WidgetDisplayer());

            configure(
                x => { x.IfCanBeCastToType <Widget>().ConvertBy((r, w) => r.Get <IWidgetDisplayer>().ToDisplay(w)); });

            var widget = new Widget {
                Color = "Red"
            };

            var request = new GetStringRequest(null, widget, locator);

            stringifier.GetString(request).ShouldEqual("A Red widget");
        }
Example #12
0
        public IActionResult Index()
        {
            _logger.LogError("打印日志");
            GetStringRequest request = new GetStringRequest()
            {
                Id         = 1,
                Name       = "测试",
                CreateTime = 1212121,
                Age        = 12
            };
            var testRequest = _mapper.Map <TestRequest>(request);

            string str = _testService.GetTestString(testRequest);

            return(View());
        }
Example #13
0
        public void SetUp()
        {
            theColumn = GridColumn <Case> .ColumnFor(x => x.Condition).TimeAgo();

            var harness = new ColumnFillerHarness();

            var request = new GetStringRequest(theColumn.Accessor, theRawValue, null)
            {
                Format = "{0:s}"
            };

            harness.Formatter.Stub(x => x.GetDisplay(request)).Return(theFormattedValue);

            theResultingDto = harness.RunColumn <Case>(theColumn, data =>
            {
                data.SetValue(c => c.Condition, theRawValue);
            });

            theColModel = theColumn.ToDictionary().Single();
        }
Example #14
0
        public async Task <T> Execute <T>()
            where T : VKApiResponse
        {
            GetStringRequest request = await GetStringRequest.Execute(RequestUri);

            switch (request.Result)
            {
            case RequestResult.NoNetworkConnection:
                throw new VKApiException("Нет подключения к интернету");

            case RequestResult.Error:
                throw new VKApiException("Не удалось загрузить данные");

            case RequestResult.NoResponse:
                throw new VKApiException("Удаленыый сервер не отвечает");

            case RequestResult.Cancelled:
                throw new VKApiException("Запрос отменен");
            }

            return(JsonConvert.DeserializeObject <T>(request.Response));
        }
        private Func<GetStringRequest, string> findConverter(GetStringRequest request)
        {
            if (request.PropertyType.IsNullable())
            {
                if (request.RawValue == null) return r => string.Empty;

                return findConverter(request.GetRequestForNullableType());
            }

            if (request.PropertyType.IsArray)
            {
                if (request.RawValue == null) return r => string.Empty;

                return r =>
                {
                    if (r.RawValue == null) return string.Empty;

                    return r.RawValue.As<Array>().OfType<object>().Select(GetString).Join(", ");
                };
            }        

            StringifierStrategy strategy = _strategies.FirstOrDefault(x => x.Matches(request));
            return strategy == null ? toString : strategy.StringFunction;
        }
Example #16
0
        public void stringifier_can_use_a_service_to_get_at_a_display()
        {
            locator.Add<IWidgetDisplayer>(new WidgetDisplayer());

            configure(
                x => { x.IfCanBeCastToType<Widget>().ConvertBy((r, w) => r.Get<IWidgetDisplayer>().ToDisplay(w)); });

            var widget = new Widget{
                Color = "Red"
            };

            var request = new GetStringRequest(null, widget, locator);

            stringifier.GetString(request).ShouldEqual("A Red widget");
        }
Example #17
0
        public void register_a_property_override_for_a_string_conversion_passing_raw_value()
        {
            object passedRawValue = null;

            configure(x =>
            {
                //specific override formatting for Address objects named Shipping
                x.IfPropertyMatches<Address>(prop => prop.Name == "Shipping").ConvertBy((req, value) =>
                {
                    passedRawValue = req.RawValue;
                    return "{1}-{0}".ToFormat(value.Address1, value.City);
                });

                //default formatting for Address objects
                x.IfIsType<Address>().ConvertBy(a => "{0}, {1}".ToFormat(a.Address1, a.City));
            });

            var address = new Address();

            var shippingRequest = new GetStringRequest(ReflectionHelper.GetAccessor<FakeSite>(s => s.Shipping), address,
                                                       locator);

            stringifier.GetString(shippingRequest);

            passedRawValue.ShouldBeTheSameAs(address);
        }
Example #18
0
        public void register_a_property_override_for_a_string_conversion()
        {
            configure(x =>
            {
                //specific override formatting for Address objects named Shipping
                x.IfPropertyMatches<Address>(prop => prop.Name == "Shipping").ConvertBy(
                    a => "{1}-{0}".ToFormat(a.Address1, a.City));

                //default formatting for Address objects
                x.IfIsType<Address>().ConvertBy(a => "{0}, {1}".ToFormat(a.Address1, a.City));
            });

            var address = new Address{
                Address1 = "2050 Ozark",
                City = "Joplin"
            };

            const string expectedDefaultFormatting = "2050 Ozark, Joplin";
            const string expectedOverrideFormatting = "Joplin-2050 Ozark";
            var billingRequest = new GetStringRequest(ReflectionHelper.GetAccessor<FakeSite>(s => s.Billing), address,
                                                      locator);
            var shippingRequest = new GetStringRequest(ReflectionHelper.GetAccessor<FakeSite>(s => s.Shipping), address,
                                                       locator);
            stringifier.GetString(billingRequest).ShouldEqual(expectedDefaultFormatting);
            stringifier.GetString(shippingRequest).ShouldEqual(expectedOverrideFormatting);
        }
Example #19
0
 private static string ToString(GetStringRequest value) => value.RawValue?.ToString() ?? string.Empty;
Example #20
0
 public string GetDisplayForValue(Accessor accessor, object rawValue)
 {
     var request = new GetStringRequest(accessor, rawValue, _locator);
     return _stringifier.GetString(request);
 }
Example #21
0
 public string GetDisplay(Accessor accessor, object target)
 {
     var request = new GetStringRequest(accessor, target, _locator);
     return _stringifier.GetString(request);
 }
Example #22
0
 public string GetDisplay(GetStringRequest request)
 {
     request.Locator = _locator;
     return _stringifier.GetString(request);
 }
 public string Format(GetStringRequest request, DateTime time)
 {
     var timeToDisplay = _source(request, time);
     return timeToDisplay.ToString(_formats[request.Property.Name]);
 }
        public string Format(GetStringRequest request, DateTime? time)
        {
            if (time == null) return string.Empty;

            return Format(request, time.Value);
        }
 private static string toString(GetStringRequest value)
 {
     return value.RawValue == null ? string.Empty : value.RawValue.ToString();
 }
Example #26
0
        public string Format(GetStringRequest request, DateTime time)
        {
            var timeToDisplay = _source(request, time);

            return(timeToDisplay.ToString(_formats[request.Property.Name]));
        }
Example #27
0
        // TODO -- unit tests around this method
        public string StringValue()
        {
            var request = new GetStringRequest(Accessor, RawValue, _services);

            return(Get <Stringifier>().GetString(request));
        }