I am presently along the way of transforming some small personal internet sites from WebForms to MVC. Using the existing sites, the database schema is solid however i had not really taken time to construct proper data/business models/layers. The aspx pages all spoken towards the database directly using a number of Sights and Saved Methods which were produced when needed for convenience. With MVC, I am now attempting to "still do itInch as the saying goes and employ such things as LINQ to SQL and/or even the Entity Framework to construct an effective data model or models for that application.

My question involves what goals I ought to have for building data models. I have read various pattern related articles and that i understand that ultimately the reply is likely likely to rely on the qualities of my data. But generally must i make an effort to build bigger models that encompass because the database as you possibly can to ensure that there's only one method to communicate with confirmed group of tables? Or must i build more compact custom models for every MVC View that only retain the data and access that View will require?

Or must i build more compact custom models for every MVC View that only retain the data and access that View will require?

This could most likely be superior.

Don't forget, you are able to stick your models in hierarchies, so common qualities, like ids, names, preferences can trouble each model.

Body fat broadened models could get better because of enterprise application, where framework instantly does large amount of stuff according to preloaded user preferences, user roles, access privileges etc. For any small personal project would most likely be superior to try and keep the models small , clean. It's also a protection. By not putting unnecessary data right into a model you be sure that your view won't in error display wrong records or posting an application wouldn't in error overwrite another data.

I'd choose the model representing the particular data logic in your current system and have your remotes return the bit of the model that the view needs for example:

Controller:

public ActionResult index()
{
    var ListOfObjects = DataHelper.GetAll();
    ViewData.Add(ListOfObjects);
    return View();
}

public ActionResult ViewObject(int id)
{
    var Object= DataHelper.GetObject();
    ViewData.Add(Object);
    return View();
}

public ActionResult ViewObjectChild(int Objectid, int ChildId)
{
    var Child= DataHelper.GetChildObject(Objectid, ChildId);
    ViewData.Add(Child);
    return View();
}

Around the view

/

<%  var myListOfObjects = ViewData.Get<IList<Object>>(); %>

/ViewObject/1/

<%  var myobject= ViewData.Get<Object>(); %>

/ViewChild/1/1/

<%  var myChild = ViewData.Get<Child>(); %>

Note I have tried personally MVC Contrib typed functions I recommend these.

Generally, you'd have one comprehensive domain model for that database. You should use (modify/add/remove/etc.) the domain model inside your service layer or even the controller if it's a little application.

However, for the sights, you should use presentation objects to create the sights simpler to keep. These are generally also known as DTO or view model objects. Essentially that which you do is create an item that consists of all of the data in the model that's essential for the vista to become populated.

For instance:

Your model can include:

public class Car() 
{
  public string Model;
}

public class Driver()
{
  public string Name;
}

You would like the vista to output the title and type of the vehicle and you would need to pass both Vehicle and Driver model objects the vista.

Instead of delivering the 2 model objects from the controller towards the view, you may create an item which consists of only the data you'll need:

public class CarAndDriverViewModel()
{
  public string CarMake;
  public string DriverName;
}

You'd populate this object in the domain data and pass that towards the view. And also the view could be:

model.DriverName + ": " + model.CarMake

Now it's not necessary to be worried about lazy loading issues or complicated view logic to cope with model peculiarities. It's more try to create these view model objects however they help much keep your view neat and provides a good way to complete formatting before delivering data towards the view.

You will find projects and conventions will help automate the development of the vista models, if you wish to consider them. AutoMapper is definitely an example.