Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question is a result out of the discussion HERE and was moved from HERE.

Is it really good practice to supply EVERY value you display in any view via a model?
Especially variables like the current username. Even Microsoft odes the following in their default template for the _LoginPartial in ASP.NET MVC 3+:

"Hello " + User.Identity.GetUserName() + "!"

I think this is OK to not supply this via a model as it is much better in light of maintainability. Or should you obay the "MVC rules" and add a model/sub-model to every page/model to supply such stuff and pass it around in your views and partials?

How do you solve such "problems" or what do you think is the best way of doing such stuff?

Inheritance would be an option:

public class BaseUserModel
{
    public string UserName { get; set; }
}

Require every model to inherit from a base model like above containing such values.
But could that cause problems if you need/want to inherit another class for some model?

share|improve this question
    
I don't think this is the case at all, considering - for example - a ViewBag object is not a model but it is used to transmit data from the controller to the view. –  Jeroen Vannevel Apr 25 at 15:48

1 Answer 1

User is a model, in the generic sense of the word, even if not according to Microsoft's narrower definition in their particular implementation of the MVC pattern. You're not doing something like User.OutputGreeting(), so the model and view are still separated, with all the concomitant benefits. The only thing you're missing out on is some of the sugar provided by the framework.

It doesn't make sense to put in more effort and more complexity to do something "the easy way." Additionally, creating another model for something already provided violates design principles like single source of truth.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.