Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for MVC.. ViewModel Binding using Interfaces Example

I am new to ASP.NET MVC 3.0 and trying to build an application using the MVC ViewModel design..

I was wondering what the best practices are regrading controllers for ViewModels and have a few questions below.. This is my understanding so far (which might be wrong)..

  • We create Models.
  • Create ViewModels by making a new class and declairing attributes with the same name and type as the base model classes (including ID fields of the base models you want to update later.. and the classes are not linked in any way).
  • Create a Repository for each of the base models (to find and save data ect.).
  • Create a Controller action for each of the ViewModels (which access the repositories of the base classes to retrieve values and put these values into the ViewModel, then pass the ViewModel to the ViewModels View).
  • Create Views from the ViewModels (ViewModel Views)
  • In the ViewModel controller Update (POST) method recieve the updated ViewModel object and convert it into base model objects (Maybe use Automapper?) and next save the base model objects back using their repositories and apply binding like this? "TryUpdate<'IPerson>(person)", "TryUpdate<'IPlace>(place);" (this looks wrong, the aim is to put the values back into the base classes from the ViewModel, apply binding, save base models back using the repositories!.. This doesnt appear to use the repositories.. Instead of TryUpdate<'IPerson>(person); I would expect to see something like this: person.Save(IPerson).. where "person contains the values", ".Save is the repository", and "IPerson contains the binding attributes to use for binding"?.. Not sure if this is right..

So far I have created ViewModels by making a new class and adding attributes from different base models using the same names. At this point i have the following questions:

Q1: Does each ViewModel have its own controller and access each of the base models repository classes to get its values?

Q2: in the ViewModel should you include the ID field of all of the base models that you are using attributes from, considering that you might want to POST an Update back through the ViewModels Controller to the base Models repository (needing the ID values)?

Q3: How would you bind attributes using an interface for binding the model in the controller using the repository to save.

I have been unable to find a tutorial or resource that explains everything in a step by step example, A complete answer would be the following example:

  • 2x simple models, 1x simple viewModel, 1x interface for binding, 1x simple controller using an interface class for binding on update, 1x repository.. i.e.

//Model1

public class Person
{
  int PersonID {get;set;}
  string FirstName {get;set;}
  string LastName {get;set;}
  DateTime DOB {get;set}
}

//Model2

public class Place
{
  int PlaceID {get;set;}
  string Description {get;set;}
  string AreaType {get;set;}
  string PostCode {get;set;}
}

//ViewModel (containing attributes from models)

publc class ViewModel
{
  //Person attributes
  int PersonID {get;set;}
  string FirstName {get;set;}
  string LastName {get;set;}

  //Place attributes
  int PlaceID {get;set;}
  string Description {get;set;}
  string AreaType {get;set;}

  //other attributes
  string someOtherAttributeForDisplay {get;set}
}

//Model1 interface (for binding on model)

public interface IPerson
{
  string FirstName {get;set;}
}

//Model2 interface (for binding on model)

public interface IPlace
{
  string Description {get;set;}
  string AreaType {get;set}
}

//ViewModelController?

{
  //What goes here?
}

//Repository?

{
  //what goes here?
}
like image 282
HonourCode Avatar asked Oct 27 '11 10:10

HonourCode


1 Answers

I think you may have overcomplicated a very simple concept.

First off some general rules:

  • Don't use TryUpdateModel. Just don't.
  • For the same reasons, don't use any "auto" mapping mappers to map from your view model to your entities. Auto mapping the other way round (from entity to view model) is fine.

Your use of interfaces is unnecessary.

View models are supposed to be very simple classes that contain just the information you need for your view. If your view POSTs different information from what it displays, then just create a different view model for POST.

We use a naming convention of {Controller}{Action}Model for our view models. So for an action named "List" on a "Post" controller we will have a model called "PostListModel".

Finally, check out my response here Real example of TryUpdateModel, ASP .NET MVC 3

like image 85
Ben Foster Avatar answered Sep 18 '22 23:09

Ben Foster