I am developing a website using Asp.Net MVC framework. We need to add general user information across a number of pages, in a similar way to the reputation bar at the top of stack overflow.
I consider it important that there should be no overhead added to the creation of a controller method in order to add this extra information to the view. This excludes the option of passing this info in the ViewData object, or amending the ViewModels to accept the reputation field as it would result in every controller looking like this:
public ActionResult Index()
{
ViewData["reputationScore"] = GetUsersReputation(userId);
// main controller logic here
}
If this is used across 90% of the pages on a site, it could be quite time consuming to alter if we also wanted to display the users badge count.
I can think of 4 solutions to this
Use Master pages Retrieve the user reputation in the master page code behind, and put the mark up for the reputation in the master page mark up.
Disadvantages:
- This seems to be moving away from everything that MVC represents.
- I have been looking at moving to using alternative view engines (e.g. razor) I am not sure how well these will mix.
- It tends to restrict the layout of the reputation - hard to put it in the middle of the page.
Extend HtmlHelper to add a GetUsersReputation method This seems to be a slight violation of what the Html object should be used for - it is not just rendering output, but hitting the database. I can't think of any other significant issues with this other than the violation of the metaphor.
Override the System.Web.Mvc.ViewPage Overriding Viewpage to expose an object in addition to Html that can be used to call a selection of methods that access the database. Make sure that extension methods can be added in the same way as HtmlHelper, so that as and when new methods are needed it can be extended appropriately. This could allow us to write something like:
<% DataAccess.GetUsersReputation() %>
Create a base generic view model Rather than passing your view model straight to the view, wrap it in a base view model that can hold all the methods you need:
public ActionResult Index()
{
MyViewModel viewCoreInfo = model.GetData();
return View(new BaseViewModel<MyViewModel>(viewCoreInfo));
}
BaseViewModel can expose all the properties that you need for extra information required on the web page. e.g. UsersReputation (it could either query the database in the constructor, or load the data when the property is accessed). I think this maintains the MVC metaphor better, but is a little cumbersome.
- Has someone else come up with a better solution
- Which is best - if you have used them have they been effective /problematic?