static IEnumerable <BindingError> SetProperties(string prefix, Type type, object instance, IRequest request, RouteValues routeValues, object owner) { var bindingErrors = new List <BindingError>(); var settableProperties = GetSettableProperties(type); foreach (var property in settableProperties) { var typeSpecifierName = (prefix != null ? prefix + "." + property.Name : property.Name) + ".$Type"; var propertyType = GetTypeFromTypeSpecifier(typeSpecifierName, request, routeValues, owner) ?? property.PropertyType; var binder = BinderCollection.FindBinderFor(property.Name, propertyType, request, routeValues, owner); if (binder == null) { continue; } var name = prefix != null ? prefix + "." + property.Name : property.Name; var bindingResult = binder.Bind(name, property.PropertyType, request, routeValues, true, owner); property.SetValue(instance, bindingResult.Object, null); if (bindingResult.Result == BindingResult.ResultType.Failure) { bindingErrors.AddRange(bindingResult.BindingErrors); } } return(bindingErrors); }
public BindingResult Bind(string name, Type type, IRequest request, RouteValues routeValues, bool isNestedCall, object owner) { var prefix = name + "["; var bindingErrors = new List <BindingError>(); var genericArgument = type.GetGenericArguments()[0]; var list = Activator.CreateInstance(typeof(ExpandableList <>).MakeGenericType(genericArgument)) as IList; foreach (var key in request.Form.AllKeys) { if (key.StartsWith(prefix, true, CultureInfo.InvariantCulture)) { var start = prefix.Length; var end = key.IndexOf(']', start); var index = int.Parse(key.Substring(start, end - start)); var indexedName = name + "[" + index + "]"; var innerBinder = BinderCollection.FindBinderFor(indexedName, genericArgument, request, routeValues, owner); var bind = innerBinder.Bind(indexedName, genericArgument, request, routeValues, true, owner); list[index] = bind.Object; if (bind.Result == BindingResult.ResultType.Failure) { bindingErrors.AddRange(bind.BindingErrors); } } } return(new BindingResult(list, bindingErrors.ToArray(), bindingErrors.Any() ? BindingResult.ResultType.Failure : BindingResult.ResultType.Success)); }
public BindingResult Bind(Type type, string name, string value, IRequest request, RouteValues routeValues, object owner) { var bindingErrors = new List <BindingError>(); var innerType = GetEnumerableInnerType(type); var list = new List <object>(); var innerBinder = BinderCollection.FindBinderFor(null, innerType, request, routeValues, owner); var stringValue = value; if (!string.IsNullOrEmpty(stringValue)) { var strings = stringValue.Split(innerType == typeof(string) ? '\n' : ','); foreach (var str in strings) { var bindingResult = innerBinder.Bind(innerType, name, str, request, routeValues, owner); if (bindingResult.Result != BindingResult.ResultType.Failure) { list.Add(bindingResult.Object); } else { bindingErrors.AddRange(bindingResult.BindingErrors); } } } var enumerable = list.Cast(innerType); return(new BindingResult(enumerable, bindingErrors.ToArray(), bindingErrors.Any() ? BindingResult.ResultType.Failure : BindingResult.ResultType.Success)); }
protected override void When() { var binder = BinderCollection.FindBinderFor("datey", typeof(DateTime), this.Request, new RouteValues(), null); var bindingResult = binder.Bind("datey", typeof(bool?), this.Request, new RouteValues(), false, null); this.target = (DateTime)bindingResult.Object; }
protected override void When() { var binder = BinderCollection.FindBinderFor(null, typeof(InnerClass), this.Request, new RouteValues(), null); var bindingResult = binder.Bind("parameter", typeof(InnerClass), this.Request, new RouteValues(), false, null); this.target = bindingResult.Object as InnerClass; }
protected override void When() { var binder = BinderCollection.FindBinderFor("json", typeof(Json), this.Request, new RouteValues(), null); var bindingResult = binder.Bind("json", typeof(Json), this.Request, new RouteValues(), false, null); this.target = (Json)bindingResult.Object; }
public override bool CanBind(string name, Type outerType, IRequest request, RouteValues routeValues, object owner) { if (!outerType.IsGenericType) { return(false); } var genericOuterType = outerType.GetGenericTypeDefinition(); if (genericOuterType != typeof(IEnumerable <>)) { return(false); } var middleType = outerType.GetGenericArguments()[0]; if (!middleType.IsGenericType) { return(false); } var genericMiddleType = middleType.GetGenericTypeDefinition(); if (genericMiddleType != typeof(IEnumerable <>)) { return(false); } var innerType = middleType.GetGenericArguments()[0]; var innerBinder = BinderCollection.FindBinderFor(null, innerType, request, routeValues, owner); return(innerBinder != null); }
protected override void When() { var binder = BinderCollection.FindBinderFor(null, typeof(NullableEnumTestViewModel), this.Request, new RouteValues(), null); var bindingResult = binder.Bind("parameter", typeof(NullableEnumTestViewModel), this.Request, new RouteValues(), false, null); this.target = (NullableEnumTestViewModel)bindingResult.Object; }
protected override void When() { var binder = BinderCollection.FindBinderFor(null, typeof(IEnumerable <IFile>), this.Request, new RouteValues(), null); var bindingResult = binder.Bind("files", typeof(IEnumerable <IFile>), this.Request, new RouteValues(), false, null); this.target = bindingResult.Object as IEnumerable <IFile>; }
public PBinderHost() { // Unique ID this.Guid = Guid.NewGuid(); // Host Collection this.Items = new BinderCollection(null); this.Items.CollectionChanged += Items_CollectionChanged; }
protected override void Given() { this.container = new Container(); BinderCollection.Initialize(this.container); this.Request = Stub <IRequest>(); this.Request.Headers.Returns(new HeaderCollection()); this.Request.Form.Returns(new HeaderCollection()); }
public override BindingResult Bind(Type type, string name, string value, IRequest request, RouteValues routeValues, object owner) { var underlyingType = Nullable.GetUnderlyingType(type); if (string.IsNullOrEmpty(value)) { return(new BindingResult(null, BindingResult.ResultType.Default)); } var binder = BinderCollection.FindBinderFor(name, underlyingType, request, routeValues, owner); return(binder.Bind(type, name, value, request, routeValues, owner)); }
public override BindingResult Bind(Type type, string name, string value, IRequest request, RouteValues routeValues, object owner) { var middleType = type.GetGenericArguments()[0]; var innerType = middleType.GetGenericArguments()[0]; var innerBinder = BinderCollection.FindBinderFor(null, innerType, request, routeValues, owner); var bound = new List <List <object> >(); var strValue = value; if (!string.IsNullOrEmpty(strValue)) { var rows = strValue.Split('\n'); foreach (var row in rows) { var cleanRow = row.Trim(); if (cleanRow == string.Empty) { continue; } var newRow = new List <object>(); var cells = cleanRow.Split(','); foreach (var cell in cells) { var bindingResult = innerBinder.Bind(innerType, name, cell, request, routeValues, owner); if (bindingResult.Result != BindingResult.ResultType.Failure) { newRow.Add(bindingResult.Object); } else { throw new NotImplementedException(); } } bound.Add(newRow); } } var enumerable = bound.Select(inner => inner.Cast(innerType)); var result = enumerable.Cast(typeof(IEnumerable <>).MakeGenericType(innerType)); return(new BindingResult(result)); }
public void Bootstrap() { Container.Global.RunAllTypeProcessors(); Container.Global.RunAllRegistries(); Container.Global.RemoveAllRegistrationsAndInstancesOf <IHandlerCache>(); Container.Global.Register <IHandlerCache>(c => { var cache = new HandlerCache(Container.Global); var types = this.GetType().Assembly.GetTypes().Where(t => typeof(IHandler).IsAssignableFrom(t)).ToArray(); cache.Warm(types); return(cache); }); BinderCollection.Initialize(Container.Global); }
protected override void Given() { var container = new Container(); subject = new RequestBinder(container, Substitute.For <IErrorMessageResolver>(), new ViolationPropertyNameResolver()); this.handler = new TestHandler(); this.methodInfo = this.handler.GetType().GetMethod("Get"); this.request = new TestRequest(); this.request.SetRequestBody("{\"Text\":\"some text\",\"Number\":3}"); this.request.ContentType = "application/json; charset=utf-8"; this.request.HttpMethod = HttpMethod.Post; this.routeValues = new RouteValues { { "routeParam", "3A814A58-C5B2-408D-BDA8-B80DDC725735" } }; BinderCollection.Initialize(container); }
static Type GetTypeFromTypeSpecifier(string name, IRequest request, RouteValues routeValues, object owner) { var binder = BinderCollection.FindBinderFor(name, typeof(string), request, routeValues, owner); if (binder == null) { return(null); } var bindingResult = binder.Bind(name, typeof(string), request, routeValues, true, owner); if (bindingResult.Result == BindingResult.ResultType.Success && bindingResult.Object as string != null) { return(Type.GetType((string)bindingResult.Object, false) ?? null); } return(null); }
public BindingResult Bind(Type type, string name, string value, IRequest request, RouteValues routeValues, object owner) { var bindingErrors = new List <BindingError>(); var keyType = GetGenericType(type, 0); var valueType = GetGenericType(type, 1); var keyBinder = BinderCollection.FindBinderFor(null, keyType, request, routeValues, owner); var valueBinder = BinderCollection.FindBinderFor(null, valueType, request, routeValues, owner); Regex dictKeyRegex = new Regex("^" + Regex.Escape(name) + "\\[([^]]+)\\]"); var matchedKeys = request.Form.AllKeys.Select(x => dictKeyRegex.Match(x)).Where(r => r.Success).Distinct(x => x.Value).ToArray(); var dictType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType); var dict = Activator.CreateInstance(dictType) as IDictionary; foreach (var matchedKey in matchedKeys) { var dictKeyPart = matchedKey.Groups[1].Value; var formKey = matchedKey.Value; var keyBinding = keyBinder.Bind(keyType, formKey, dictKeyPart, request, routeValues, owner); var valueBinding = valueBinder.Bind(formKey, valueType, request, routeValues, true, owner); if (keyBinding.Result == BindingResult.ResultType.Success && valueBinding.Result == BindingResult.ResultType.Success) { dict.Add(keyBinding.Object, valueBinding.Object); } else { bindingErrors.AddRange(keyBinding.BindingErrors); bindingErrors.AddRange(valueBinding.BindingErrors); } } return(new BindingResult(dict, bindingErrors.ToArray(), bindingErrors.Any() ? BindingResult.ResultType.Failure : BindingResult.ResultType.Success)); }
void DoBindToForm(string method, NameValueCollection formValues, int bindIterations) { BinderCollection.Initialize(Container.Global); var parameterInfos = typeof(TestHandler).GetMethod(method)?.GetParameters(); var request = new TestRequest { HttpMethod = HttpMethod.Post, ContentType = "multipart/form-data", Form = formValues }; var requestBinder = this.Subject <RequestBinder>(); var stopwatch = Stopwatch.StartNew(); for (var i = 0; i < bindIterations; i++) { requestBinder.Bind(null, parameterInfos?.Single(), request, new RouteValues()); } Console.WriteLine(stopwatch.Elapsed); }
public static void GenericMethodTest10() { BinderCollection <string> binder = new BinderCollection <string>(); binder.BindToGList <object>(123, (a, b) => { }); }