Created
November 18, 2010 13:39
-
-
Save ryankelley/704978 to your computer and use it in GitHub Desktop.
ParentIdUrlPolicy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IN FubuRegistry you add this | |
Routes.UrlPolicy<ParentIdUrlPolicy>(); | |
//Route input looks like this: | |
public class ViewContactRequest : IRequestById, IRequireParentId | |
{ | |
public Guid Id { get; set; } | |
public Guid ParentId | |
{ get; set; } | |
} | |
// Folder Structure is: Actions/Customers/Contacts/ViewContactAction.cs in this scenario. | |
// So final route is /cusomter/{ParentId}/contacts/{ContactId} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ParentIdUrlPolicy : IUrlPolicy | |
{ | |
public bool Matches(ActionCall call, IConfigurationObserver log) | |
{ | |
if(log != null) | |
log.RecordCallStatus(call, "Applying custom url policy for sub routes dependent on the parents Id"); | |
return call.HasInput && call.InputType().CanBeCastTo<IRequireParentId>(); | |
} | |
public IRouteDefinition Build(ActionCall call) | |
{ | |
var folders = call.HandlerType.Namespace.Replace(call.HandlerType.Assembly.GetName().Name, "") | |
.Replace(".Actions.", "").Replace('.', '/').ToLower(); | |
var foldersList = folders.Split('/'); | |
var definition = call.ToRouteDefinition(); | |
definition.Append(foldersList.FirstOrDefault()); | |
definition.AddRouteInput(new RouteInput(ReflectionHelper.GetAccessor<IRequireParentId>(x => x.ParentId)), true); | |
if(foldersList.Length >= 2) | |
{ | |
definition.Append(foldersList[1]); | |
} | |
if (call.InputType().CanBeCastTo<IRequestById>()) | |
{ | |
definition.AddRouteInput(new RouteInput(ReflectionHelper.GetAccessor<IRequestById>(x => x.Id)), true); | |
} | |
if(foldersList.Length > 2) | |
{ | |
for (int i = 2; i < foldersList.Length; i++) | |
{ | |
definition.Append(foldersList[i]); | |
} | |
} | |
handlePosts(call, definition); | |
handlePuts(call,definition); | |
handleGets(call,definition); | |
handleDeletes(call,definition); | |
return definition; | |
} | |
private void handleDeletes(ActionCall call, IRouteDefinition definition) | |
{ | |
if(call.Method.Name.Equals("Delete")) | |
definition.AddRouteConstraint(RouteConstraintPolicy.HTTP_METHOD_CONSTRAINT, | |
new HttpMethodConstraint("DELETE")); | |
} | |
private void handleGets(ActionCall call, IRouteDefinition definition) | |
{ | |
if (call.Method.Name.Equals("Get")) | |
definition.AddRouteConstraint(RouteConstraintPolicy.HTTP_METHOD_CONSTRAINT, | |
new HttpMethodConstraint("GET")); | |
} | |
private void handlePuts(ActionCall call, IRouteDefinition definition) | |
{ | |
if (call.Method.Name.Equals("Put")) | |
{ | |
definition.AddRouteConstraint(RouteConstraintPolicy.HTTP_METHOD_CONSTRAINT, | |
new HttpMethodConstraint("PUT")); | |
definition.Category = Categories.EDIT; | |
} | |
} | |
private void handlePosts(ActionCall call, IRouteDefinition definition) | |
{ | |
if (call.Method.Name.Equals("Post")) | |
{ | |
definition.AddRouteConstraint(RouteConstraintPolicy.HTTP_METHOD_CONSTRAINT, | |
new HttpMethodConstraint("POST")); | |
definition.Category = Categories.NEW; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment