This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
/
DefaultBinder.cs
186 lines (151 loc) · 6.79 KB
/
DefaultBinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
namespace Nancy.ModelBinding
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
/// <summary>
/// Default binder - used as a fallback when a specific modelbinder
/// is not available.
/// </summary>
public class DefaultBinder : IBinder
{
private readonly IEnumerable<ITypeConverter> typeConverters;
private readonly IEnumerable<IBodyDeserializer> bodyDeserializers;
private readonly IFieldNameConverter fieldNameConverter;
private readonly BindingDefaults defaults;
public DefaultBinder(IEnumerable<ITypeConverter> typeConverters, IEnumerable<IBodyDeserializer> bodyDeserializers, IFieldNameConverter fieldNameConverter, BindingDefaults defaults)
{
if (typeConverters == null)
{
throw new ArgumentNullException("typeConverters");
}
if (bodyDeserializers == null)
{
throw new ArgumentNullException("bodyDeserializers");
}
if (fieldNameConverter == null)
{
throw new ArgumentNullException("fieldNameConverter");
}
if (defaults == null)
{
throw new ArgumentNullException("defaults");
}
this.typeConverters = typeConverters;
this.bodyDeserializers = bodyDeserializers;
this.fieldNameConverter = fieldNameConverter;
this.defaults = defaults;
}
/// <summary>
/// Bind to the given model type
/// </summary>
/// <param name="context">Current context</param>
/// <param name="modelType">Model type to bind to</param>
/// <param name="blackList">Blacklisted property names</param>
/// <returns>Bound model</returns>
public object Bind(NancyContext context, Type modelType, params string[] blackList)
{
var result = this.DeserializeRequestBody(context, modelType, blackList);
if (result != null)
{
return result;
}
var bindingContext = this.CreateBindingContext(context, modelType, blackList);
foreach (var modelProperty in bindingContext.ValidModelProperties)
{
var stringValue = this.GetValue(modelProperty.Name, bindingContext);
if (!String.IsNullOrEmpty(stringValue))
{
this.BindProperty(modelProperty, stringValue, bindingContext);
}
}
return bindingContext.Model;
}
private BindingContext CreateBindingContext(NancyContext context, Type modelType, string[] blackList)
{
return new BindingContext()
{
Context = context,
DestinationType = modelType,
Model = this.CreateModel(modelType),
ValidModelProperties = this.GetProperties(modelType, blackList),
FormFields = this.GetFormFields(context),
TypeConverters = this.typeConverters.Concat(this.defaults.DefaultTypeConverters),
};
}
private IDictionary<string, string> GetFormFields(NancyContext context)
{
var formDictionary = (DynamicDictionary)context.Request.Form;
return formDictionary.GetDynamicMemberNames().ToDictionary(
memberName => this.fieldNameConverter.Convert(memberName),
memberName => (string)formDictionary[memberName]);
}
private void BindProperty(PropertyInfo modelProperty, string stringValue, BindingContext context)
{
var destinationType = modelProperty.PropertyType;
var typeConverter =
this.typeConverters.Where(c => c.CanConvertTo(destinationType)).FirstOrDefault();
if (typeConverter != null)
{
this.SetPropertyValue(modelProperty, context.Model, typeConverter.Convert(stringValue, destinationType, context));
return;
}
if (destinationType == typeof(string))
{
this.SetPropertyValue(modelProperty, context.Model, stringValue);
}
}
private void SetPropertyValue(PropertyInfo modelProperty, object model, object value)
{
// TODO - catch reflection exceptions?
modelProperty.SetValue(model, value, null);
}
private IEnumerable<PropertyInfo> GetProperties(Type modelType, string[] blackList)
{
return modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanWrite && !blackList.Contains(p.Name, StringComparer.InvariantCulture));
}
private object CreateModel(Type modelType)
{
return Activator.CreateInstance(modelType);
}
private string GetValue(string propertyName, BindingContext context)
{
// TODO - check captured variables too if possible?
return context.FormFields.ContainsKey(propertyName) ? context.FormFields[propertyName] : String.Empty;
}
private object DeserializeRequestBody(NancyContext context, Type modelType, string[] blackList)
{
if (context == null || context.Request == null)
{
return null;
}
var contentType = this.GetRequestContentType(context);
var bodyDeserializer = this.bodyDeserializers.Where(b => b.CanDeserialize(contentType)).FirstOrDefault();
if (bodyDeserializer != null)
{
return bodyDeserializer.Deserialize(contentType, modelType, context.Request.Body, context);
}
bodyDeserializer = this.defaults.DefaultBodyDeserializers.Where(b => b.CanDeserialize(contentType)).FirstOrDefault();
if (bodyDeserializer != null)
{
return bodyDeserializer.Deserialize(contentType, modelType, context.Request.Body, context);
}
return null;
}
private string GetRequestContentType(NancyContext context)
{
if (context == null || context.Request == null)
{
return String.Empty;
}
IEnumerable<string> contentTypeHeaders;
context.Request.Headers.TryGetValue("Content-Type", out contentTypeHeaders);
if (contentTypeHeaders == null || !contentTypeHeaders.Any())
{
return string.Empty;
}
return contentTypeHeaders.First();
}
}
}