views:

304

answers:

3

My domain model is this: we have a bunch of schools as the root of the "hierarchy". Each school has teachers and courses, and each course has one teacher. I am trying to model this with the logic of the mvc framework and I 'm quite confused. For example, the \school\details\x should give the first page of a school. That should contain a link to a list of its teachers, and a list to each courses.

A list of teachers means that the index action should be parametric to the school the user is looking at: \teacher\id where id is the school. The same with the course list. And then create teacher or course should also be parametric to what school we are looking at:\teacher\create\x where x=school.

How do I carry around the school id? Is there some neat way to do it, or do I need to pass it around all the time, into every view that needs it? It also makes the site URLs very cryptic. I was thinking of a way to make the url structure like {school-alias}\{controller}\{action}\{id}, still I have to find a way to pass around the school. If this is accomplished, then I need to implement some kind of filter that will not allow a user to perform certain actions if the schoolId he is requesting does not match the one in his profile.

I figure that if I 'm carrying the schoolid around the URL, the site is more REST-like, compared to, for example, getting the schoolId from the user's profile.

+1  A: 
LukLed
I am trying that right now. The problem is that I have to play fetch with myself with the schoolId or school acronym. I have to pass it as an argument to every action, and send it back with all my links.I guess there is no way to automise this?
thanos panousis
I had the exact same idea--what route though would get me to the ~/{acronym} view, for example /UCLA that would be the index page of school with acronym UCLA? That should have to work in parallel with the rule you just posted.
thanos panousis
If user is associated with only one school, you don't have to have it in url. You can also make combobox with available schools and store selected school in Session object.My rule actually solves the problem with /UCLA. Opening /UCLA URL will take you to "School" controller, "Details" action and You will have "UCLA" as acronym.
LukLed
If I store the school in the session then all links to a certain view of a teacher or course will be dead links. I believe that its in the mvc spirit to avoid that as much as possible. I would prefer to find a neat way to pass it around.
thanos panousis
They will be dead links only if user is not logged in or he doesn't have access to the teacher.Look at this site. If you go to http://stackoverflow.com/badges, it shows Your badges. Stackoverflow application propably stores Your information in session:)
LukLed
+1  A: 

Write an extension method to overload rendering of links that extracts the school identifier ( acronym or whatever you choose to use ) from the routing data and adds it to the route values already passed in. This way your action can choose to use the identifier if it is present but is not required to add it to the view data and you do not have to remember to include it in any action links ( you just have to remember to use your action link overload ).

I would make the action link overload quite obviously different so anyone following behind you can see you are doing something unusual. This could be as simple as Html.SchoolActionLink( ...).

For example: If your url is http://mydomain.com/abc/teachers/list and your route is defined as {school}/{controller}/{action} then the route value dictionary will have the value "abc" at the key "school". The route values can be accessed via HtmlHelper.ViewContext.RouteData.Values.

Neal
So you mean that the extension method for a custom Actionlink would take the schoolId as a parameter? I don't understand what you mean by "extracts the school identifier from the routing data".
thanos panousis
Updated answer with details on how the school can be extracted from the routing data
Neal
A: 

In the end I 'm answering my own question.

The real solution to this is :Restfull Routing. It implements the functionality in RoR, which is exactly what I need. Too bad this is not a requirement from more people so that it can go into mvc-trunk.

thanos panousis