Skip to content

Commit

Permalink
Added the ability to specify a factory expression for your applicatio…
Browse files Browse the repository at this point in the history
…n classes to enable DI scenarios and updated TinyUrl sample to use it; added example route exclusions to TinyUrl sample
  • Loading branch information
mattburton committed Jan 18, 2011
1 parent 74bb3e8 commit 0b4c575
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
5 changes: 4 additions & 1 deletion Nina.Demo.Tinyurl/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public class NinaHttpApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new MountingPoint<TinyUrl>("/"));
routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
routes.Add(new Route("favicon.ico", new StopRoutingHandler()));

routes.Add(new MountingPoint<TinyUrl>("/", () => new TinyUrl(new Urls())));
}

protected void Application_Start()
Expand Down
2 changes: 1 addition & 1 deletion Nina.Demo.Tinyurl/Nina.Demo.Tinyurl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>23687</DevelopmentServerPort>
<DevelopmentServerPort>56628</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:8080/tinyurl</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
Expand Down
7 changes: 3 additions & 4 deletions Nina.Demo.Tinyurl/TinyUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ namespace Nina.Demo.Tinyurl
{
public class TinyUrl : Nina.Application
{
private static readonly Urls Urls = new Urls();
public TinyUrl()
public TinyUrl(Urls urls)
{
Get("/", (m,c) => Text("<html><body>Tiny url!<form method='post'><input type='text' name='url'/><input type='submit' value='tiny!'/></form></body></html>"));

Post("/", (m,c)=>
{
var url = Urls.Save(c.Request.Form["url"]);
var url = urls.Save(c.Request.Form["url"]);
return Text(string.Format("<html><body>Your url: <a href='{0}'>{0}</a></body></html>", c.Request.Url +url));
});

Get("/{tinyurl}", (m, c) => Redirect(Urls.Get(m["tinyurl"])) );
Get("/{tinyurl}", (m, c) => Redirect(urls.Get(m["tinyurl"])) );
}
}

Expand Down
2 changes: 1 addition & 1 deletion Nina.Demo/Nina.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>23683</DevelopmentServerPort>
<DevelopmentServerPort>56631</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:8080/va2</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
Expand Down
11 changes: 8 additions & 3 deletions Nina/Routing/MountedRouteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@
// See LICENSE.txt for details.
//
#endregion

using System;
using System.Web;
using System.Web.Routing;

namespace Nina.Routing
{
internal class MountedRouteHandler<T> : IRouteHandler where T : NinaBaseHandler, new()
internal class MountedRouteHandler<T> : IRouteHandler where T : NinaBaseHandler
{
private readonly RouteBase _route;
private readonly string _mountingPoint;
private readonly T _httpHandler;
private readonly string _absolute;

public MountedRouteHandler(RouteBase route, string mountingPoint)
public MountedRouteHandler(RouteBase route, string mountingPoint, Func<T> applicationFactory)
{
_route = route;
_mountingPoint = mountingPoint;

//optimization: we have the same handler instance for all requests
_absolute = VirtualPathUtility.ToAbsolute(_mountingPoint);

_httpHandler = new T { Route = _route, MountingPointVirtual = _mountingPoint, MountingPointPath = _absolute };
_httpHandler = applicationFactory();
_httpHandler.Route = _route;
_httpHandler.MountingPointVirtual = _mountingPoint;
_httpHandler.MountingPointPath = _absolute;
}

public IHttpHandler GetHttpHandler(RequestContext requestContext)
Expand Down
25 changes: 18 additions & 7 deletions Nina/Routing/MountingPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@
// See LICENSE.txt for details.
//
#endregion

using System;
using System.Web;
using System.Web.Routing;

namespace Nina.Routing
{
public class MountingPoint<T> : RouteBase where T : NinaBaseHandler, new()
public class MountingPoint<T> : RouteBase where T : NinaBaseHandler
{
private readonly string _mountedUrl;
private MountedRouteHandler<T> _factory;
private readonly MountedRouteHandler<T> _factory;

public MountingPoint(string mountedUrl) : this(mountedUrl, Activator.CreateInstance<T>)
{
}

public MountingPoint(string mountedUrl)
public MountingPoint(string mountedUrl, Func<T> applicationFactory)
{
_mountedUrl = mountedUrl;
if (applicationFactory == null)
{
throw new ArgumentNullException("applicationFactory");
}

_mountedUrl = mountedUrl;

if (mountedUrl.StartsWith("/"))
{
Expand All @@ -30,8 +41,7 @@ public MountingPoint(string mountedUrl)
_mountedUrl = "~/" + mountedUrl;
}


_factory = new MountedRouteHandler<T>(this, _mountedUrl);
_factory = new MountedRouteHandler<T>(this, _mountedUrl, applicationFactory);
}

public override RouteData GetRouteData(HttpContextBase httpContext)
Expand All @@ -40,7 +50,8 @@ public override RouteData GetRouteData(HttpContextBase httpContext)
if(!httpContext.Request.AppRelativeCurrentExecutionFilePath.StartsWith(_mountedUrl))
return null;

RouteData rdata = new RouteData(this, _factory);
var rdata = new RouteData(this, _factory);

return rdata;
}

Expand Down

0 comments on commit 0b4c575

Please sign in to comment.