views:

89

answers:

3

I have user friendly urls that are generated and stored in a database..

When I want to get a url I have a helper class that gives me the urls..

this helper method sometimes calls to the database to generate a url..

According to separation of concerns should i generate all the urls needed in a page in the controller and pass them to the view..

or is it ok to generate urls in the view?

Regards

+1  A: 

Your process to generate url seems pretty complex (interacting with the model) and I believe they should be generated in the controller.

In my opinion, views should be kept as simple as possible

Aurélien Bottazzini
I would have to agree... +1
J.13.L
+3  A: 

Matt, controller in this case.

Here is a small tip: What helps me sometimes, is to say it outloud ( despite the fear to look like a lunatic :-) )

Anytime you think about your code and you can say outloud: "generate", "create", "destroy" - most likely you should do this in a controller. If you are thinking about a piece of code in terms of "validate", "data", "complex search", mostly likely should be offloaded to your model. If you are thinking well this form should be 5 pixels over, or that button should be green - that's view.

Sometimes this may be confusing. Here is an example from my recent project. Let's say you want to show certain parts to admins and some to users, there are two approaches. One is to load different layout in your controller based on permissions, the other is to check for permissions as you go through view. Both have their merits. The first one is more mvc - but will create duplication of code, the second approach messier and less mvc but keeps your code DRYer. There is also a third approach ( that I ended up using) to use 2nd approach - have just one layout, but offload all of the difficult parts that have dual or tripple views based on permitions into partials.

Hope that helps.

Nick Gorbikoff
+2  A: 

My $0.02: The real Separation of Concerns issue here is doing DB calls and generating URIs in the same method. Put the DB calls in the controller and generate the URI in the view. Something like this:

Controller:

// stuff
var importantNum = (from d in Repository.SomeData
                    where d.Id == id
                    select new 
                    {
                        Num = d.ImportantForUri
                    }).First().Num;
var model.LinkData = new RouteValueDictionary { { "Important", importantNum" }, 
                                                { "Constant", "Foo"} // etc
                                              };
return View(model);

View:

<!-- stuff -->
<%= Html.ActionLink("Hi there", "ActionName", model.LinkData);
Craig Stuntz
That's it - views should be as simple as possible. All logic is in the controller or (even better) the services (business logic) which the controller should call.
Pure.Krome