Skip to content

Commit c6008c6

Browse files
Merge pull request NancyFx#2361 from thecodejunkie/benchmark-defaultmodelbinderlocator
Optimizaiton of DefaultModelBinderLocator
2 parents feb8a12 + 90d8e3e commit c6008c6

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/Nancy/ModelBinding/DefaultModelBinderLocator.cs

+21-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class DefaultModelBinderLocator : IModelBinderLocator
1212
/// <summary>
1313
/// Available model binders
1414
/// </summary>
15-
private readonly IEnumerable<IModelBinder> binders;
15+
private readonly IReadOnlyCollection<IModelBinder> binders;
1616

1717
/// <summary>
1818
/// Default model binder to fall back on
@@ -27,7 +27,11 @@ public class DefaultModelBinderLocator : IModelBinderLocator
2727
public DefaultModelBinderLocator(IEnumerable<IModelBinder> binders, IBinder fallbackBinder)
2828
{
2929
this.fallbackBinder = fallbackBinder;
30-
this.binders = binders;
30+
31+
if (binders != null)
32+
{
33+
this.binders = binders.ToArray();
34+
}
3135
}
3236

3337
/// <summary>
@@ -38,7 +42,20 @@ public DefaultModelBinderLocator(IEnumerable<IModelBinder> binders, IBinder fall
3842
/// <returns>IModelBinder instance or null if none found</returns>
3943
public IBinder GetBinderForType(Type modelType, NancyContext context)
4044
{
41-
return this.binders.FirstOrDefault(modelBinder => modelBinder.CanBind(modelType)) ?? this.fallbackBinder;
45+
if (this.binders == null)
46+
{
47+
return this.fallbackBinder;
48+
}
49+
50+
foreach (var modelBinder in this.binders)
51+
{
52+
if (modelBinder.CanBind(modelType))
53+
{
54+
return modelBinder;
55+
}
56+
}
57+
58+
return this.fallbackBinder;
4259
}
4360
}
44-
}
61+
}

0 commit comments

Comments
 (0)