Skip to content

Instantly share code, notes, and snippets.

@mikeobrien
Created July 6, 2011 16:32
Show Gist options
  • Save mikeobrien/1067679 to your computer and use it in GitHub Desktop.
Save mikeobrien/1067679 to your computer and use it in GitHub Desktop.
public class Configuration : FubuRegistry
{
public Configuration()
{
//...
Routes.HomeIs<DashboardHandler>(x => x.Query())
.ConstrainMethodPrefixToHttpGet("Query")
.ConstrainMethodPrefixToHttpPost("Command")
.UrlPolicy(FullNameUrlPolicy.Create().
IgnoreNamespace<LoginHandler>().
IgnoreClassName("Handler").
IgnoreMethodName("Query", "Command"));
// ...
}
}
public class FullNameUrlPolicy : IUrlPolicy
{
private readonly List<string> _ignoreNamespace = new List<string>();
private readonly List<string> _ignoreClass = new List<string>();
private readonly List<string> _ignoreMethod = new List<string>();
public static FullNameUrlPolicy Create()
{
return new FullNameUrlPolicy();
}
public bool Matches(ActionCall call, IConfigurationObserver log)
{
return true;
}
public FullNameUrlPolicy IgnoreNamespace<T>()
{
_ignoreNamespace.Add(typeof(T).Namespace);
return this;
}
public FullNameUrlPolicy IgnoreClassName(params string[] names)
{
_ignoreClass.AddRange(names);
return this;
}
public FullNameUrlPolicy IgnoreMethodName(params string[] names)
{
_ignoreMethod.AddRange(names);
return this;
}
public IRouteDefinition Build(ActionCall call)
{
var route = call.ToRouteDefinition();
AppendNamespace(route, call, _ignoreNamespace);
AppendClass(route, call, _ignoreClass);
AppendMethod(route, call, _ignoreMethod);
return route;
}
private static void AppendNamespace(IRouteDefinition route, ActionCall call, IEnumerable<string> ignore)
{
route.Append(RemovePattern(call.HandlerType.Namespace, ignore).Replace('.', '/').ToLower());
}
private static void AppendClass(IRouteDefinition route, ActionCall call, IEnumerable<string> ignore)
{
route.Append(RemovePattern(call.HandlerType.Name, ignore).ToLower());
}
private static void AppendMethod(IRouteDefinition route, ActionCall call, IEnumerable<string> ignore)
{
var methodName = RemovePattern(call.Method.Name, ignore);
if (MethodToUrlBuilder.Matches(call.Method.Name) && call.HasInput)
{
MethodToUrlBuilder.Alter(route, methodName, new TypeDescriptorCache().GetPropertiesFor(call.InputType()).Keys, x => { });
route.ApplyInputType(call.InputType());
}
else if (!string.IsNullOrEmpty(methodName)) route.Append(methodName);
}
private static string RemovePattern(string source, IEnumerable<string> pattern)
{
return pattern.Aggregate(source, (a, i) => a.Replace(i, string.Empty));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment