0

I'm moving all definitions like the following:

 @(Model.Store.EmployeeType.Name == "Manager" ? Model.Store.HQ.Manager.DisplayName : Model.Store.Manager.DisplayName )

Out of my View and into a viewModel:

public class ManagerViewModel 
{
    public string Manager {get;set;}
}

And defining them in the controller:

var viewModel = new ManagerViewModel();

viewModel.Manager = Model.Store.EmployeeType.Name == "Manager" ? Model.Store.HQ.Manager.DisplayName : Model.Store.Manager.DisplayName;

return View(viewModel);

Now, in my View I can do this:

@Model.Manager

My question is - does this violate the skinny controller best practice? I have about 30 fields that need this type of treatment, so my controller is going to be pretty big - I'm creating a new property for every field.

tereško
  • 58,060
  • 25
  • 98
  • 150
RobVious
  • 12,685
  • 25
  • 99
  • 181

1 Answers1

2

Don't worry too much about premature optimization. I think you're on the right path here and wouldn't worry too much about it.

You could populate the ViewModel from within the Constructor if you're really worried about "skinny" controllers.

public class ManagerViewModel 
{

    public ManagerViewModel(ManagerModel model){
        // initialize in here
        this.Manager = model.Store.EmployeeType.Name == "Manager" ? model.Store.HQ.Manager.DisplayName : model.Store.Manager.DisplayName;
    }

    public string Manager {get;set;}
}
var viewModel = new ManagerViewModel(model);
return View(viewModel);
Community
  • 1
  • 1
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • Thanks Chase. How would this change if I was working on a Details page? If I initialize everything within the viewModel, where would the "var manager = db.Managers.Find(id);" go? – RobVious Jan 25 '13 at 05:09
  • Every View should have it's own ViewModel. Therefore you'll need a `ManagersViewModel`(list of managers) and a `ManagerViewModel`(details for a single manager.) – Chase Florell Jan 25 '13 at 05:11
  • ps: I was assuming that your current `ManagerViewModel` IS a details View. If it's not, please elaborate on what it IS. – Chase Florell Jan 25 '13 at 05:12
  • It is a Details view. With the ManagerViewModel(model), are you passing "manager" from my previous comment to the ViewModel, as "model"? – RobVious Jan 25 '13 at 05:14