First read these questions and their answers:
- MVC: data models and view models
- Why two classes, model and domain model?
also this article can help:
In conclusion, I think that in most scenarios it is useful to have a puffy domain model (DM), as well as light weight representation (PM) models associated with it. Therefore, when we want to edit only a small piece of this bold DM, one of our PM will raise his hand.
Imagine this class in DM:
namespace DomainModels { public class Person { public int ID { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public DateTime? DoB { get; set; } public MyAddressDM Address { get; set; } public string Phone { get; set; } public IEnumerable<MyCarModel> Cars { get; set; }
Now imagine that in one view we only need to edit the address and phone. Lightweight PM may be as follows:
namesapce PresentationModels { public PersonAddressPhone { public int ID { get; set;} public string FullName { get; set;} public string AddressSteet { get; set; } public string AddressCity { get; set; } public string AddressState { get; set; } public string AddressZipCode { get; set; } public string Phone { get; set; } } }
and in another form we need to add / remove cars for a person:
namesapce PresentationModels { public PersonCars { public int ID { get; set;} public string FullName { get; set;} public IEnumerable<PMCar> Cars { get; set;} } }
The juxtaposition between DO and PM is the golden part of this puzzle. Be sure to check out AutoMapper .
Tohid
source share