Created
February 2, 2012 15:37
-
-
Save ryankelley/1724029 to your computer and use it in GitHub Desktop.
Variable Output Convention
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RenderJsonBehavior<T> : BasicBehavior where T : class | |
| { | |
| private readonly IFubuRequest _request; | |
| private readonly IJsonWriter _writer; | |
| public RenderJsonBehavior(IJsonWriter writer, IFubuRequest request) | |
| : base(PartialBehavior.Executes) | |
| { | |
| _writer = writer; | |
| _request = request; | |
| } | |
| protected override DoNext performInvoke() | |
| { | |
| var output = _request.Get<T>(); | |
| _writer.Write(output); | |
| return DoNext.Continue; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RenderJsonNode : OutputNode | |
| { | |
| private readonly Type _modelType; | |
| public RenderJsonNode(Type modelType) | |
| : base(typeof(RenderJsonBehavior<>).MakeGenericType(modelType)) | |
| { | |
| _modelType = modelType; | |
| } | |
| public Type ModelType { get { return _modelType; } } | |
| public override string Description { get { return "Json"; } } | |
| public bool Equals(RenderJsonNode other) | |
| { | |
| if (ReferenceEquals(null, other)) return false; | |
| if (ReferenceEquals(this, other)) return true; | |
| return Equals(other._modelType, _modelType); | |
| } | |
| public override bool Equals(object obj) | |
| { | |
| if (ReferenceEquals(null, obj)) return false; | |
| if (ReferenceEquals(this, obj)) return true; | |
| if (obj.GetType() != typeof(RenderJsonNode)) return false; | |
| return Equals((RenderJsonNode)obj); | |
| } | |
| public override int GetHashCode() | |
| { | |
| return (_modelType != null ? _modelType.GetHashCode() : 0); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class VariableOutputConvention : IConfigurationAction | |
| { | |
| public void Configure(BehaviorGraph graph) | |
| { | |
| graph.Actions().Where(x => (x.OutputType().CanBeCastTo<IEnumerable>() && !x.OutputType().IsString()) || x.OutputType().CanBeCastTo<IReturnPdf>()) | |
| .Each(x => | |
| { | |
| OutputNode output = null; | |
| var modelType = x.OutputType(); | |
| var view = getRenderViewNode(x); | |
| if (x.HasAnyOutputBehavior()) | |
| { | |
| output = view ?? x.OfType<OutputNode>().FirstOrDefault(); | |
| } | |
| var json = new RenderJsonNode(modelType); | |
| var pdf = new RenderPdfNode(modelType); | |
| var variableOut = new VariableOutputNode(); | |
| if (output != null) | |
| output.ReplaceWith(variableOut); | |
| else | |
| { | |
| x.AddToEnd(variableOut); | |
| } | |
| if (modelType.CanBeCastTo<PagedListBase>()) | |
| { | |
| variableOut.AddOutput(a => a.RenderFormat == "paged", new PagedJsonRenderNode(modelType)); | |
| } | |
| variableOut.AddOutput(a => a.RenderFormat == "json", json); | |
| variableOut.AddOutput(a => a.AcceptsFormat("application/json"), json); | |
| variableOut.AddOutput(a => a.AcceptsFormat("text/javascript"), json); | |
| variableOut.AddOutput(a => a.RenderFormat == "pdf", pdf); | |
| variableOut.AddOutput(a => a.AcceptsFormat("application/pdf"), pdf); | |
| if (view != null) | |
| { | |
| variableOut.AddOutput(a => a.AcceptsFormat("text/html"), view); | |
| variableOut.AddOutput(a => a.AcceptsFormat("*/*"), view); | |
| variableOut.AddOutput(a => a.RenderFormat == "pdf", pdf); | |
| variableOut.AddOutput(a => a.AcceptsFormat("application/pdf"), pdf); | |
| } | |
| graph.Observer.RecordCallStatus(x, "Adding variable output behavior"); | |
| }); | |
| graph.Actions().Where(x => x.OutputType().CanBeCastTo<IReturnJson>()) | |
| .Each(x => | |
| { | |
| OutputNode output = null; | |
| var view = getRenderViewNode(x); | |
| if (x.HasAnyOutputBehavior()) | |
| { | |
| output = view ?? x.OfType<OutputNode>().FirstOrDefault(); | |
| } | |
| var modelType = x.OutputType(); | |
| var json = new CustomRenderJsonNode(modelType); | |
| var variableOut = new VariableOutputNode(); | |
| if (output != null) | |
| output.ReplaceWith(variableOut); | |
| else | |
| { | |
| x.AddToEnd(variableOut); | |
| } | |
| variableOut.AddOutput(a => a.RenderFormat == "json", json); | |
| variableOut.AddOutput(a => a.AcceptsFormat("application/json"), json); | |
| variableOut.AddOutput(a => a.AcceptsFormat("text/javascript"), json); | |
| if (view != null) | |
| { | |
| variableOut.AddOutput(a => a.AcceptsFormat("text/html"), view); | |
| variableOut.AddOutput(a => a.AcceptsFormat("*/*"), view); | |
| } | |
| else | |
| { | |
| variableOut.AddOutput(a => a.AcceptsFormat("*/*"), json); | |
| } | |
| graph.Observer.RecordCallStatus(x, "Adding variable output behavior"); | |
| }); | |
| } | |
| private static OutputNode getRenderViewNode(ActionCall x) | |
| { | |
| return x.OfType<OutputNode>().FirstOrDefault(y => TypeExtensions.CanBeCastTo<RenderFubuWebFormView>(y.BehaviorType)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment