Permalink
Browse files

updated how serialization works

  • Loading branch information...
1 parent 0ed9886 commit 12a5e797b0deb14a14b9ea2a1a77e69284cf0485 @nberardi nberardi committed Apr 18, 2011
View
Binary file not shown.
@@ -1,9 +1,60 @@
using System;
using ManagedFusion.Web.Mvc;
+using System.Web.Mvc;
namespace ManagedFusion.Web.Mvc
{
public class AutoSerializedView : SerializedView
{
+ public AutoSerializedView()
+ {
+ StatusCode = 200;
+ StatusDescription = "OK";
+ }
+
+ public override void Render(System.Web.Mvc.ViewContext viewContext, System.IO.TextWriter writer)
+ {
+ var focus = (IView)null;
+
+ ResponseType responseType = ResponseType.None;
+ var routeData = viewContext.RouteData.Values;
+
+ object tempObj;
+ if (routeData.TryGetValue("responseType", out tempObj) && tempObj is ResponseType)
+ responseType = (ResponseType)tempObj;
+
+ if (responseType == ResponseType.None)
+ responseType = ServiceHelper.GetResponseType(viewContext);
+
+ switch (responseType)
+ {
+ case ResponseType.JavaScript:
+ focus = new JavaScriptCallbackResult();
+ break;
+
+ case ResponseType.Json:
+ focus = new JsonView();
+ break;
+
+ case ResponseType.Xml:
+ focus = new XmlView();
+ break;
+
+ case ResponseType.Csv:
+ focus = new CsvResult();
+ break;
+
+ case ResponseType.Html:
+ default:
+ focus = viewContext.View;
+ break;
+ }
+
+ if (focus is SerializedView)
+ ServiceHelper.CopyProperties(this, (SerializedView)focus);
+
+ if (focus != null)
+ focus.Render(viewContext, writer);
+ }
}
}
View
@@ -37,7 +37,6 @@ protected internal override string GetContent()
FollowFrameworkIgnoreAttributes = FollowFrameworkIgnoreAttributes
};
-
// don't want to build a response because we want this as flat as possible
var response = serializer.FromObject(Model, csvSerializer);
@@ -5,9 +5,6 @@ namespace ManagedFusion.Web.Mvc
public interface ISerializableActionResult
{
object Model { get; }
-
- int StatusCode { get; set; }
-
- string StatusDescription { get; set; }
+ SerializedView ModelSerializer { get; }
}
}
@@ -13,18 +13,9 @@ protected SerializedResult()
: this(null) { }
public SerializedResult(object model)
- : this(model, new AutoSerializedView()) { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ServiceResult"/> class.
- /// </summary>
- public SerializedResult(object model, SerializedView view)
{
Model = model;
- ModelSerializer = view;
-
- StatusCode = 200;
- StatusDescription = "OK";
+ ModelSerializer = new AutoSerializedView();
}
/// <summary>
@@ -46,16 +37,6 @@ public virtual object Model
protected set;
}
- /// <summary>
- ///
- /// </summary>
- public int StatusCode { get; set; }
-
- /// <summary>
- ///
- /// </summary>
- public string StatusDescription { get; set; }
-
/// <summary>
///
/// </summary>
@@ -72,73 +53,10 @@ protected override ViewEngineResult FindView(ControllerContext context)
/// <param name="context">The context.</param>
public override void ExecuteResult(ControllerContext context)
{
- if (ModelSerializer is AutoSerializedView)
- UpdateModelSerializer(context);
-
- ModelSerializer.StatusCode = StatusCode;
- ModelSerializer.StatusDescription = StatusDescription;
-
View = ModelSerializer;
ViewData = new ViewDataDictionary(Model);
base.ExecuteResult(context);
}
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="context"></param>
- private void UpdateModelSerializer(ControllerContext context)
- {
- ResponseType responseType = ResponseType.None;
- var routeData = context.RouteData.Values;
-
- object tempObj;
- if (routeData.TryGetValue("responseType", out tempObj) && tempObj is ResponseType)
- responseType = (ResponseType)tempObj;
-
- if (responseType == ResponseType.None)
- responseType = ServiceHelper.GetResponseType(context);
-
- var view = ModelSerializer;
-
- if (view == null)
- view = new AutoSerializedView();
-
- switch (responseType)
- {
- case ResponseType.JavaScript:
- ModelSerializer = new JavaScriptCallbackResult();
- break;
-
- case ResponseType.Json:
- ModelSerializer = new JsonView();
- break;
-
- case ResponseType.Xml:
- ModelSerializer = new XmlView();
- break;
-
- case ResponseType.Csv:
- ModelSerializer = new CsvResult();
- break;
-
- case ResponseType.Html:
- default:
- break;
- }
-
- if (ModelSerializer is SerializedView)
- {
- var resultX = (ModelSerializer as SerializedView);
-
- resultX.FollowFrameworkIgnoreAttributes = view.FollowFrameworkIgnoreAttributes;
- resultX.SerializePublicMembers = view.SerializePublicMembers;
- resultX.SerializedRootName = view.SerializedRootName;
-
- foreach (var header in view.SerializedHeader)
- resultX.SerializedHeader.Add(header.Key, header.Value);
- }
- }
}
}
@@ -57,16 +57,18 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
/// <param name="filterContext">The filter context.</param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
- if (filterContext != null && filterContext.Exception != null && ExceptionResult != null)
+ if (filterContext != null
+ && filterContext.Exception != null
+ && ExceptionResult != null)
{
var result = Activator.CreateInstance(ExceptionResult) as ISerializableExceptionResult;
result.ExceptionMessage = filterContext.Exception.Message;
result.Exception = filterContext.Exception;
if (filterContext.Exception is HttpException)
{
- result.StatusCode = (filterContext.Exception as HttpException).GetHttpCode();
- result.StatusDescription = ((HttpStatusCode)result.StatusCode).ToString().FromPascalCase();
+ result.ModelSerializer.StatusCode = (filterContext.Exception as HttpException).GetHttpCode();
+ result.ModelSerializer.StatusDescription = ((HttpStatusCode)result.ModelSerializer.StatusCode).ToString().FromPascalCase();
}
filterContext.Result = result as ActionResult;
@@ -102,16 +104,7 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
}
if (result.View is SerializedView && view != null)
- {
- var resultX = (result.View as SerializedView);
-
- resultX.FollowFrameworkIgnoreAttributes = view.FollowFrameworkIgnoreAttributes;
- resultX.SerializePublicMembers = view.SerializePublicMembers;
- resultX.SerializedRootName = view.SerializedRootName;
-
- foreach (var header in view.SerializedHeader)
- resultX.SerializedHeader.Add(header.Key, header.Value);
- }
+ ServiceHelper.CopyProperties(view, (SerializedView)result.View);
}
else if (filterContext.Result is ISerializableActionResult && !typeof(SerializedResult).IsAssignableFrom(filterContext.Result.GetType()))
{
@@ -123,10 +116,8 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
case ResponseType.Json:
case ResponseType.Xml:
case ResponseType.Csv:
- filterContext.Result = new SerializedResult(result.Model) {
- StatusCode = result.StatusCode,
- StatusDescription = result.StatusDescription
- };
+ var resultX = new SerializedResult(result.Model);
+ ServiceHelper.CopyProperties(result.ModelSerializer, resultX.ModelSerializer);
break;
case ResponseType.Html:
@@ -8,6 +8,20 @@ namespace ManagedFusion.Web.Mvc
{
public static class ServiceHelper
{
+ public static void CopyProperties(SerializedView fromResult, SerializedView toResult)
+ {
+ toResult.FollowFrameworkIgnoreAttributes = fromResult.FollowFrameworkIgnoreAttributes;
+ toResult.SerializePublicMembers = fromResult.SerializePublicMembers;
+ toResult.SerializedRootName = fromResult.SerializedRootName;
+ toResult.StatusCode = fromResult.StatusCode;
+ toResult.StatusDescription = fromResult.StatusDescription;
+ toResult.ContentType = fromResult.ContentType;
+ toResult.ContentEncoding = fromResult.ContentEncoding;
+
+ foreach (var header in fromResult.SerializedHeader)
+ toResult.SerializedHeader.Add(header.Key, header.Value);
+ }
+
/// <summary>
///
/// </summary>

0 comments on commit 12a5e79

Please sign in to comment.