Permalink
Browse files

Incorporate code review feedback for ResultConverters

- Reuse singleton converter instances where possible.
- Throw on null controllerContext
  • Loading branch information...
1 parent f95cdef commit 6bda9407809a28e6ebef4bc97fd88ca98b68e544 @marcind marcind committed Apr 6, 2012
View
11 src/System.Web.Http/Controllers/HttpActionDescriptor.cs
@@ -27,6 +27,9 @@ public abstract class HttpActionDescriptor
private HttpActionBinding _actionBinding;
+ private static readonly ResponseMessageResultConverter _responseMessageResultConverter = new ResponseMessageResultConverter();
+ private static readonly VoidResultConverter _voidResultConverter = new VoidResultConverter();
+
protected HttpActionDescriptor()
{
_filterPipeline = new Lazy<Collection<FilterInfo>>(InitializeFilterPipeline);
@@ -62,7 +65,7 @@ public HttpConfiguration Configuration
public virtual HttpActionBinding ActionBinding
{
get
- {
+ {
if (_actionBinding == null)
{
IActionValueBinder actionValueBinder = _controllerDescriptor.ActionValueBinder;
@@ -73,7 +76,7 @@ public virtual HttpActionBinding ActionBinding
}
set
{
- if (value == null)
+ if (value == null)
{
throw Error.PropertyNull();
}
@@ -165,11 +168,11 @@ internal static IActionResultConverter GetResultConverter(Type type)
if (type == null)
{
- return new VoidResultConverter();
+ return _voidResultConverter;
}
else if (typeof(HttpResponseMessage).IsAssignableFrom(type))
{
- return new ResponseMessageResultConverter();
+ return _responseMessageResultConverter;
}
else
{
View
5 src/System.Web.Http/Controllers/ResponseMessageResultConverter.cs
@@ -12,6 +12,11 @@ public class ResponseMessageResultConverter : IActionResultConverter
{
public HttpResponseMessage Convert(HttpControllerContext controllerContext, object actionResult)
{
+ if (controllerContext == null)
+ {
+ throw Error.ArgumentNull("controllerContext");
+ }
+
HttpResponseMessage response = (HttpResponseMessage)actionResult;
if (response == null)
{
View
5 src/System.Web.Http/Controllers/ValueResultConverter.cs
@@ -13,6 +13,11 @@ public class ValueResultConverter<T> : IActionResultConverter
{
public HttpResponseMessage Convert(HttpControllerContext controllerContext, object actionResult)
{
+ if (controllerContext == null)
+ {
+ throw Error.ArgumentNull("controllerContext");
+ }
+
HttpResponseMessage resultAsResponse = actionResult as HttpResponseMessage;
if (resultAsResponse != null)
{
View
5 src/System.Web.Http/Controllers/VoidResultConverter.cs
@@ -12,6 +12,11 @@ public class VoidResultConverter : IActionResultConverter
{
public HttpResponseMessage Convert(HttpControllerContext controllerContext, object actionResult)
{
+ if (controllerContext == null)
+ {
+ throw Error.ArgumentNull("controllerContext");
+ }
+
Contract.Assert(actionResult == null);
return controllerContext.Request.CreateResponse();
}
View
6 test/System.Web.Http.Test/Controllers/ResponseMessageResultConverterTest.cs
@@ -30,6 +30,12 @@ public void Convert_WhenValueIsResponseMessage_ReturnsResponseMessageWithRequest
}
[Fact]
+ public void Convert_WhenContextIsNull_Throws()
+ {
+ Assert.ThrowsArgumentNull(() => _converter.Convert(controllerContext: null, actionResult: new HttpResponseMessage()), "controllerContext");
+ }
+
+ [Fact]
public void Convert_WhenValueIsNull_Throws()
{
Assert.Throws<InvalidOperationException>(() => _converter.Convert(_context, null),
View
6 test/System.Web.Http.Test/Controllers/ValueResultConverterTest.cs
@@ -22,6 +22,12 @@ public ValueResultConverterTest()
}
[Fact]
+ public void Convert_WhenContextIsNull_Throws()
+ {
+ Assert.ThrowsArgumentNull(() => _objectValueConverter.Convert(controllerContext: null, actionResult: new object()), "controllerContext");
+ }
+
+ [Fact]
public void Convert_WhenValueTypeIsNotCompatible_Throws()
{
Assert.Throws<InvalidCastException>(() => _animalValueConverter.Convert(_context, new object()),
View
6 test/System.Web.Http.Test/Controllers/VoidResultConverterTest.cs
@@ -20,6 +20,12 @@ public VoidResultConverterTest()
}
[Fact]
+ public void Convert_WhenContextIsNull_Throws()
+ {
+ Assert.ThrowsArgumentNull(() => _converter.Convert(controllerContext: null, actionResult: null), "controllerContext");
+ }
+
+ [Fact]
public void Convert_ReturnsResponseMessageWithRequestAssigned()
{
var result = _converter.Convert(_context, null);

0 comments on commit 6bda940

Please sign in to comment.