Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created June 9, 2010 16:56
Show Gist options
  • Save jmarnold/431796 to your computer and use it in GitHub Desktop.
Save jmarnold/431796 to your computer and use it in GitHub Desktop.
public class EntityDetailsActionSource : IActionSource
{
public IEnumerable<ActionCall> FindActions(TypePool types)
{
var entityTypes = types.TypesMatching(t => RootEntityTypes.Types.Contains(t));
foreach (var entityType in entityTypes)
{
var handlerType = typeof(RenderEntityDetailsAction<>).MakeGenericType(entityType);
if (handlerType == null)
{
continue;
}
var actionMethod = handlerType.GetMethod("Get", BindingFlags.Public | BindingFlags.Instance);
if (actionMethod != null)
{
yield return new ActionCall(handlerType, actionMethod);
}
}
}
}
public class EntityDetailsUrlPolicy : IUrlPolicy
{
/// <summary>
/// Returns a flag indicating whether the policy matches the specified action call.
/// </summary>
/// <param name="call"></param>
/// <param name="log"></param>
/// <returns></returns>
public bool Matches(ActionCall call, IConfigurationObserver log)
{
return typeof (RenderEntityDetailsAction<>).IsAssignableFrom(call.HandlerType.GetGenericTypeDefinition());
}
/// <summary>
/// Builds a route definition for the specified call.
/// </summary>
/// <param name="call"></param>
/// <returns></returns>
public IRouteDefinition Build(ActionCall call)
{
var routeDefinition = call.ToRouteDefinition();
var genericArguments = call.HandlerType.GetGenericArguments();
var entityType = genericArguments[0];
//TODO -- Handle plurals
routeDefinition.Append(entityType.Name.ToLower() + "s");
routeDefinition.Append("details");
return routeDefinition;
}
}
public class MyFubuRegistry : FubuRegistry
{
public MyFubuRegistry()
{
IncludeDiagnostics(true);
Applies
.ToThisAssembly();
var httpVerbs = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase){"GET","POST","PUT","HEAD"};
Actions
.FindWith<EntityDetailsActionSource>();
httpVerbs.Each(verb => Routes.ConstrainToHttpMethod(action => action.Method.Name.Equals(verb, StringComparison.InvariantCultureIgnoreCase), verb));
Views.TryToAttach(findViews => findViews.by_ViewModel());
Policies
.WrapBehaviorChainsWith<RedirectToSearchWhenEntityNotFoundBehavior>();
Routes
.UrlPolicy<EntityDetailsUrlPolicy>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment