Skip to content

Commit

Permalink
removed the conditional media writer checking in conneg
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Aug 24, 2015
1 parent 2728fb6 commit e0edb2c
Show file tree
Hide file tree
Showing 23 changed files with 107 additions and 386 deletions.
2 changes: 0 additions & 2 deletions src/FubuMVC.Core/FubuMVC.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,8 @@
<Compile Include="Resources\Conneg\FormatterReader.cs" />
<Compile Include="Resources\Conneg\FormatterWriter.cs" />
<Compile Include="Resources\Conneg\HtmlStringWriter.cs" />
<Compile Include="Resources\Conneg\IMedia.cs" />
<Compile Include="Resources\Conneg\IMediaWriter.cs" />
<Compile Include="Resources\Conneg\InputBehavior.cs" />
<Compile Include="Resources\Conneg\Media.cs" />
<Compile Include="Resources\Conneg\ModelBindingReader.cs" />
<Compile Include="Resources\Conneg\OutputBehavior.cs" />
<Compile Include="Resources\Conneg\StringWriter.cs" />
Expand Down
3 changes: 1 addition & 2 deletions src/FubuMVC.Core/Registration/Nodes/BehaviorChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,14 @@ public virtual string Title()
{
var views =
Output.Media()
.Select(x => x.Writer)
.OfType<IViewWriter>()
.Select(x => Description.For(x.View).Title);
return "View(s): " + views.Join("");
}

if (HasOutput() && Output.Media().Any())
{
return Output.Media().Select(x => Description.For(x.Writer).Title).Join(", ");
return Output.Media().Select(x => Description.For(x).Title).Join(", ");
}

if (InputType() != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected override DoNext applyInputs(IInputNode node, BehaviorChain chain, Conn
protected override DoNext applyOutputs(IOutputNode node, BehaviorChain chain, ConnegSettings settings)
{
var graph = settings.Graph ?? new ConnegGraph();
graph.WriterTypesFor(node.ResourceType).Each(type => node.Add(Activator.CreateInstance(type)));
graph.WriterTypesFor(node.ResourceType).Each(type => node.Add((IMediaWriter) Activator.CreateInstance(type)));

return DoNext.Continue;
}
Expand Down
20 changes: 0 additions & 20 deletions src/FubuMVC.Core/Resources/Conneg/IMedia.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/FubuMVC.Core/Resources/Conneg/IMediaCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace FubuMVC.Core.Resources.Conneg
{
public interface IMediaCollection<T> where T : class
{
IMedia<T> SelectMedia(CurrentMimeType mimeTypes, IFubuRequestContext logger);
IEnumerable<IMedia<T>> Media { get; }
IMediaWriter<T> SelectWriter(CurrentMimeType mimeTypes, IFubuRequestContext logger);
IEnumerable<IMediaWriter<T>> Writers { get; }
}
}
9 changes: 7 additions & 2 deletions src/FubuMVC.Core/Resources/Conneg/IMediaWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

namespace FubuMVC.Core.Resources.Conneg
{
public interface IMediaWriter<T>
public interface IMediaWriter
{
void Write(string mimeType, IFubuRequestContext context, T resource);
IEnumerable<string> Mimetypes { get; }
}

public interface IMediaWriter<T> : IMediaWriter
{
void Write(string mimeType, IFubuRequestContext context, T resource);

}
}
15 changes: 6 additions & 9 deletions src/FubuMVC.Core/Resources/Conneg/IOutputNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,28 @@ public interface IOutputNode
/// Add an IFormatter strategy for writing with an optional condition
/// </summary>
/// <param name="formatter"></param>
/// <param name="condition"></param>
void Add(IFormatter formatter, IConditional condition = null);
void Add(IFormatter formatter);

/// <summary>
/// Add a media writer and optional condition by an open type
/// of IMediaWriter<T> where T is the resource type
/// </summary>
/// <param name="mediaWriterType"></param>
/// <param name="condition"></param>
void Add(Type mediaWriterType, IConditional condition = null);
void Add(Type mediaWriterType);

/// <summary>
/// Explicitly register an IMediaWriter<T> where T is the resource type
/// </summary>
/// <param name="writer"></param>
/// <param name="condition"></param>
void Add(object writer, IConditional condition = null);
void Add(IMediaWriter writer);

/// <summary>
/// All the explicitly configured Media.
/// </summary>
/// <returns></returns>
IEnumerable<IMedia> Media();
IEnumerable<IMediaWriter> Media();

IEnumerable<IMedia<T>> Media<T>();
IEnumerable<IMediaWriter<T>> Media<T>();



Expand Down Expand Up @@ -76,7 +73,7 @@ public interface IOutputNode
/// </summary>
Instance ResourceNotFound { get; set; }

bool HasView(IConditional conditional);
bool HasView();


IViewToken DefaultView();
Expand Down
66 changes: 0 additions & 66 deletions src/FubuMVC.Core/Resources/Conneg/Media.cs

This file was deleted.

27 changes: 13 additions & 14 deletions src/FubuMVC.Core/Resources/Conneg/MediaCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,39 @@ namespace FubuMVC.Core.Resources.Conneg
{
public class MediaCollection<T> : IMediaCollection<T> where T : class
{
private readonly Lazy<IEnumerable<IMedia<T>>> _media;
private readonly Lazy<IEnumerable<IMediaWriter<T>>> _media;

public MediaCollection(IOutputNode node)
{
_media = new Lazy<IEnumerable<IMedia<T>>>(() => node.Media<T>().ToArray());
_media = new Lazy<IEnumerable<IMediaWriter<T>>>(() => node.Media<T>().ToArray());
}

public IEnumerable<IMedia<T>> Media
public IEnumerable<IMediaWriter<T>> Writers
{
get { return _media.Value; }
}

public IMedia<T> SelectMedia(CurrentMimeType mimeTypes, IFubuRequestContext context)
public IMediaWriter<T> SelectWriter(CurrentMimeType mimeTypes, IFubuRequestContext context)
{
foreach (var acceptType in mimeTypes.AcceptTypes)
{
var candidates = Media.Where(x => x.Mimetypes.Contains(acceptType));
if (candidates.Any())
var candidate = Writers.FirstOrDefault(x => x.Mimetypes.Contains(acceptType));
if (candidate != null)
{
var writer = candidates.FirstOrDefault(x => x.MatchesRequest(context));
if (writer != null)
if (candidate != null)
{
context.Logger.DebugMessage(() => new WriterChoice(acceptType, writer, writer.Condition));
return writer;
context.Logger.DebugMessage(() => new WriterChoice(acceptType, candidate));
return candidate;
}

context.Logger.DebugMessage(() => NoWritersMatch.For(acceptType, candidates));
}


}

if (mimeTypes.AcceptsAny())
{
var media = Media.FirstOrDefault(x => x.MatchesRequest(context));
context.Logger.DebugMessage(() => new WriterChoice(MimeType.Any.Value, media, media.Condition));
var media = Writers.FirstOrDefault();
context.Logger.DebugMessage(() => new WriterChoice(MimeType.Any.Value, media));

return media;
}
Expand Down
8 changes: 2 additions & 6 deletions src/FubuMVC.Core/Resources/Conneg/NoWritersMatch.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using FubuCore;
using FubuCore.Descriptions;
using FubuCore.Logging;

namespace FubuMVC.Core.Resources.Conneg
{
public class NoWritersMatch : LogRecord, DescribesItself
{
public static NoWritersMatch For<T>(string acceptType, IEnumerable<IMedia<T>> candidates)
public static NoWritersMatch For<T>(string acceptType, IEnumerable<IMediaWriter<T>> candidates)
{
var match = new NoWritersMatch{
WriterList = candidates.Select(writer =>
{
var title = Description.For(writer).Title;
var condition = Description.For(writer.Condition).Title;

return "{0} ({1})".ToFormat(title, condition);
return Description.For(writer).Title;
}).Join(", ")
};

Expand Down
6 changes: 3 additions & 3 deletions src/FubuMVC.Core/Resources/Conneg/OutputBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void WriteResource(CurrentMimeType mimeTypes, T resource)
// Select the appropriate media writer
// based on the mimetype and other runtime
// conditions
var media = _media.SelectMedia(mimeTypes, _context);
var media = _media.SelectWriter(mimeTypes, _context);

if (media == null)
{
Expand All @@ -106,9 +106,9 @@ public void WriteResource(CurrentMimeType mimeTypes, T resource)

// ENDSAMPLE

public IEnumerable<IMedia<T>> Media
public IEnumerable<IMediaWriter<T>> Media
{
get { return _media.Media; }
get { return _media.Writers; }
}

public void WriteHeaders()
Expand Down
Loading

0 comments on commit e0edb2c

Please sign in to comment.