views:

20

answers:

1

I have a bunch of support Cases and Users. Each Case is assigned to a User with a unique ID. My data model represents this with an AssignedUser user ID reference. This is easy to store and retrieve, but not convenient to display.

When I want to display a list of Cases, the user property should be formatted as the user's username and not their ID, but this information is not available inside the case. It is difficult to retrieve usernames inside the case repository, since it is provided by a separate user repository.

At the moment, I am doing it by creating a DisplayFor(...) template for a case's AssignedUser property and building a dictionary of users in the HttpContext at OnActionExecuting which is then referenced by the template and translated to a username. This works fairly well, but following this approach, I am populating the HttpContext at every request with a number of dictionaries (Branches, Products, etc.) that are not always necessary.

Instinctively, I should pass the user data in the view model, but that means retrieving it properly. What is the best-practice in ASP.NET-MVC without doing unnecessary queries and populating the HttpContext with useless data?

A: 

I ended up using AutoMapper to populate view models for display and map user IDs to user names, et cetera.

FreshCode