Move the Builder into it's own file

This commit is contained in:
Tim Clem
2012-04-28 12:46:37 -07:00
parent b5c93c1236
commit f68b5fe4d9
4 changed files with 63 additions and 49 deletions
+2
View File
@@ -41,9 +41,11 @@
<Compile Include="Helpers\Ensure.cs" />
<Compile Include="GitHubClient.cs" />
<Compile Include="AuthenticationType.cs" />
<Compile Include="Http\Builder.cs" />
<Compile Include="Http\Connection.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Http\IBuilder.cs" />
<Compile Include="Http\RequestHandler.cs" />
<Compile Include="Http\ResponseHandler.cs" />
<Compile Include="IApiObjectMap.cs" />
+46
View File
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Burr.Helpers;
namespace Burr.Http
{
/// <summary>
/// Standard Implementation of a <see cref="IBuilder"/>.
/// A <see cref="Builder"/>'s job is to build up the middleware for a connection.
/// </summary>
public class Builder : IBuilder
{
readonly List<Func<IApplication, IApplication>> handlers = new List<Func<IApplication, IApplication>>();
bool frozen;
public List<Func<IApplication, IApplication>> Handlers
{
get { return handlers; }
}
public void Use(Func<IApplication, IApplication> handler)
{
Ensure.ArgumentNotNull(handler, "handler");
if (frozen) throw new NotSupportedException("The middleware stack has already been built. You cannot modify the handlers now");
handlers.Add(handler);
}
public IApplication Run(IApplication adapter = null)
{
if (frozen) throw new NotSupportedException("The middleware stack has already been built. You can only call run once.");
frozen = true;
// The builder sets up a chain of apps to be called just like Rack
// The inner most app is always an adapter which must produce the actual response
// The call tree for a basic middleware stack will look like this:
//
// request( response ( adapter(env) ) )
adapter = adapter ?? new HttpClientAdapter();
return Enumerable.Reverse(handlers).Aggregate(adapter, (app, handler) => handler(app));
}
}
}
-49
View File
@@ -7,55 +7,6 @@ using Burr.Helpers;
namespace Burr.Http
{
/// <summary>
/// A <see cref="IBuilder"/>'s job is to build up the "application" for a connection.
/// </summary>
public interface IBuilder
{
List<Func<IApplication, IApplication>> Handlers { get; }
IApplication Run(IApplication adapter);
void Use(Func<IApplication, IApplication> handler);
}
/// <summary>
/// Standard Implementation of a <see cref="IBuilder"/>.
/// A <see cref="Builder"/>'s job is to build up the middleware for a connection.
/// </summary>
public class Builder : IBuilder
{
readonly List<Func<IApplication, IApplication>> handlers = new List<Func<IApplication, IApplication>>();
bool frozen;
public List<Func<IApplication, IApplication>> Handlers
{
get { return handlers; }
}
public void Use(Func<IApplication, IApplication> handler)
{
Ensure.ArgumentNotNull(handler, "handler");
if (frozen) throw new NotSupportedException("The middleware stack has already been built. You cannot modify the handlers now");
handlers.Add(handler);
}
public IApplication Run(IApplication adapter = null)
{
if (frozen) throw new NotSupportedException("The middleware stack has already been built. You can only call run once.");
frozen = true;
// The builder sets up a chain of apps to be called just like Rack
// The inner most app is always an adapter which must produce the actual response
// The call tree for a basic middleware stack will look like this:
//
// request( response ( adapter(env) ) )
adapter = adapter ?? new HttpClientAdapter();
return Enumerable.Reverse(handlers).Aggregate(adapter, (app, handler) => handler(app));
}
}
public interface IResponse<T>
{
string Body { get; set; }
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
namespace Burr.Http
{
/// <summary>
/// A <see cref="IBuilder"/>'s job is to build up the "application" for a connection.
/// </summary>
public interface IBuilder
{
List<Func<IApplication, IApplication>> Handlers { get; }
IApplication Run(IApplication adapter);
void Use(Func<IApplication, IApplication> handler);
}
}