THBBFT
THBBFT

Reputation: 1211

What is Best Practices with System.Web.Mvc ViewModels

I am working on a medium sized MVC project.

The View Models have all been pulled out the web assembly to a separate library. One of the data structures is represented by a listbox and has a MultiSelectList property.

The external VM project does not currently have a reference to the System.Web.Mvc assembly.

Should I add the reference?

Should I refactor the code to not have a reference to the MultiSelectList class? If this is the case where does the RawOptionValues property get converted to a MultiSelectList?

public class SelectListPrompt{

    public IDictionary<string, string> RawOptionValues { get; set; }

    public MultiSelectList OptionValues
    {
        get
        {
            return new MultiSelectList(RawOptionValues.ToList(), "Key", "Value");
        }
    }
}

Upvotes: 1

Views: 312

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93464

I see no benefit whatsoever of putting your view models in a separate project. View models must be tightly coupled to your views, so all you are doing is adding complexity for no appreciable benefit.

I feel the same about people that put their controllers in a separate project. I really don't see the benefit. It doesn't have any structural or design benefits and just makes things more complex.

Of course, the exception is portable areas, but then you're placing everything in the separate project, not just the views or the controllers.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1039418

View models are tightly coupled to your views. This is their purpose. So, yes, they should absolutely know about System.Web.Mvc. You have put them in a separate class library - great, but while this might not be necessary, if you decide to go that way, this library should know about classes like SelectListItem, MultiSelectList, ... So go ahead and add reference. Don't refactor your view models - they should work with those classes.

What you should be careful about is not to couple your domain models with ASP.NET MVC.

Upvotes: 2

Related Questions