RESTful URL generation from models #79
Comments
Hi @ricec, Sorry for the late reply. I am in the process of moving but I wanted to make sure to answer your question. I normally (and assume many other MVC devs) use ViewModels for each view. This means you don't have a particular Model that is tied to a resource. Instead I like to treat Resources as conceptual rather than exact. The users controller deals with the users domain model, but might have multiple viewmodels: IndexModel, ShowModel, EditModel. The reason I have this approach is I can clearly set values required for each of those view. IndexModel is the best example since the index action is normally where I page the collection. Here is what one of my actions looks like.
And generally what my index model looks like.
I have created a library called WhatRoute that has this construct. I also have a library called Fluorescent that helps generate routes (based off of restful routing) for clientside use. Take a look at both of them :)
I know this is a long response, but I wanted to give you some background as to how a lot of devs operate in the .NET space. I also know routing is really annoying :). So let me answer your questions.
Sorta... WhatRoute was an attempt to move towards using the names on routes, but not to leverage the implicit values carried over from the previous request. Fluorescent looks at routes and tries to create a template that can be used clientside. Both still have the issue of needing explicit values passed in.
I like the idea of the LinkTo taking a model, but I don't like the idea of tying routes to a particular model. I think it would cause more issues than solve. A happy medium would be this.
LinkTo would take these steps:
What do you think? |
Hi, @khalidabuhakmeh. Thanks for the response. I definitely agree that view models can be quite useful for decoupling your domain models from your views and for storing additional view-specific data. I might have misled you by all my Rails-related talk, but I actually do have quite a bit of experience with .NET MVC, so I'm very familiar with this pattern. I've just come off a year-long stint of Rails work, though, so my mind is still transitioning back into the .NET world. I think the source of our disagreement on this is our opinions of what a resource is. You say that resources are conceptual, but I believe that they can be more concrete. Since in REST, a URI uniquely identifies a resource, let's look at a URI from the users example, http://example.com/users/1. This URI very clearly identifies an instance of our User domain model, and the HTTP verbs represent actions performed on this concrete resource. If I am correct, this means that a domain model that has been exposed as a resource could be used to retrieve its associated URI, as I have suggested. Since the routing layer is where we determine whether or not to expose something as a resource, it seems logical to identify what the resource is in this same layer. I'm not saying that all resources are domain models, but I believe they can be. That's my view on things, but I would welcome some debate on how a resource is defined. I believe that the answer to that question determines how this feature is implemented and whether it even makes sense. How to resolve my perspective with the MVVM pattern depends on the nature of your view model:
@Html.LinkTo("Show", Model)
@Html.LinkTo("Show", Model.DomainModelProperty)
All of this to say that I still think a RESTful route should be directly tied to the resource it is exposing, and I think that in many situations, that resource is a domain model. What do you think? |
Hi @ricec, So let's step back and define what the issue is and work our way to a solution. ProblemIf I understand correctly:
So here are some of the questions we have to answer:
The model and an action string is where we would like to pull that extra information from, the issue is we don't want to clutter our "domain" models with extra information.
RouteData holds the following information:
Model (possibly) holds the following information:
User input provides:
SolutionPossible algorithm for getting the above information.
Attribute Example:
Non-Attribute Example:
ThoughtsThe interesting thing about the algorithm provided above is it can work outside of Restful Routing and we can spike it without having to do it in Restful Routing. If it ends up working we can merge it in as an added feature of Restful Routing. What do you think? Do I make a compelling argument or do you still want to link the resource to the controller? |
Hi, @khalidabuhakmeh. Thanks for humoring me with this discussion even though I'm clearly wrong. :) I've done a little more reading (specifically, the Fielding dissertation on REST), and it has helped to reshape my opinions. From the dissertation:
This directly contradicts what I've been saying! So here I am, doing a 180. I resource is abstract, as you said, and in the case of RESTful Routing, we are defining the resource in the routes. Sorry for wasting your time with my faulty argument, and thank you for pushing me in the right direction. Anyway, back to the problem... With my new perspective, I like your approach, but I am a little concerned over the nomenclature. Giving a class a "Resource" attribute seems like we are tagging it as a resource, but that's not what we're doing. I actually think that in this case, it's best to move away from REST terminology, since all we are really doing is flagging a class as something that can be used to generate a URL. Maybe we call it the RoutableAttribute? For implementation, we could probably use the model binders to fill in URL parameters when reverse routing. Regardless, I think we're aligned on direction now. Let's start working on some code. |
@ricec No need to apologize, I am just happy someone is using Restful Routing :) I love this library and hope you grow to love it too!
I can dig it 👍. Naming stuff is hard :P Do you want to branch off of main and give it a try? I can help you figure out the attribute scanning stuff or any other part if you like. I've done a lot of routing in WhatRoute so I have extensive knowledge in resolving routes. Also, what do you think about some "safe" or "sane" defaults?
is equivalent to
|
@khalidabuhakmeh, I like the defaults. I'll work that in... I'll try to put some work in on this tonight or tomorrow, and I'll let you know if I could use some guidance. |
One of the more powerful side effects of having RESTful routes is that no longer do you have to pull values from your models and format them into a URL. Your routing should do this for you. All you need to do is supply the model. This can be seen in the Rails url_for method.
Instead of...
You have...
.NET MVC is helpful in this scenario by filling in route parameters from the original request. If I'm on users/1 and I want to get to users/1/edit,
will work just fine. This breaks down when these "ambient" values do you no good. For example, if I'm on users/1 and I want to link to a nested resource at users/1/things/1, I'm left with this nasty bit of code...
Ideally, we could instead write...
To accomplish this, we need to do a few things:
Something like this might work...
So, I have two questions:
The text was updated successfully, but these errors were encountered: