This is going to be a long question, but in short, I still cannot figure out why it is BETTER to use View Model instead of Model when dealing with CRUD actions
This is what I've learnt so far, please correct me along the way. So when trying to implement a "Create" action method, it is "best practice" to do the following(I am using entity framework code first here):
DAL
- You create your entities
- You map these entities to the database
- You interact with the database in DAL using the repository pattern
- You create your interface
BLL(Controller)
- You do dependency injection to access the methods created in DAL using Unity/Ninject/Nhibernate etc...
- Now you go on create your View Model(optional) Views and Controller actions
When creating controller actions for example you can do this:
public ActionResult Create()
{
return View();
}
//
// POST: /CRUD/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee) ---- Using actual entities
{
if (ModelState.IsValid)
{
repository.Add(employee);
return RedirectToAction("Index");
}
return View(employee);
}
In view you do:
@model foo.Entities.Employee
Or:
public ActionResult Create()
{
var model = new EmployeeViewModel();
return View(model);
}
//
// POST: /CRUD/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(EmployeeViewModel model) ---- Using viewmodel
{
if (ModelState.IsValid)
{
//map here or use automapper
var employee = new Employee()
employee.Name = model.Name;
repository.Add(employee);
return RedirectToAction("Index");
}
return View(employee);
}
In view you do this:
@model foo.Model.EmployeeViewModel
What exactly is the difference? why use the View Model with more code involved??
and also I am trying to implement update controller action using view model as well:
public ActionResult Edit(int jobId)
{
Job job = repository.FindJob(jobId);
if (job == null)
{
return HttpNotFound();
}
var model = new JobEditViewModel();
//mapping to get information from database and display
ConfigureEditViewModel(job, model);
return View(model);
}
private void ConfigureCreateViewModel(JobCreateViewModel model)
{
model.Title = job.Title;
model.NumberOfPosition = job.NumberOfPosition;
model.Salary = job.Salary;
model.SelectedSalaryPeriodId = job.SalaryPeriodId;
model.PostCode = job.PostCode;
model.SelectedLocationId = job.LocationId;
model.IndustryExperiencePeriod = job.IndustryExperiencePeriod;
model.Role = job.Role;
model.Description = job.Description;
model.SelectedEmploymentHourId = job.EmploymentHourId;
model.SelectedDurationId = job.DurationId;
model.SelectedShiftId = job.ShiftId;
model.SelectedWorkDayId = job.WorkDayId;
}
and when posting
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(JobEditViewModel model) ---- Using viewmodel
{
if (ModelState.IsValid)
{
//mapping model back to the entity...
var job = new Job()
job.Title = model.Title;
job.NumberOfPositions = .......
repository.Edit(job);
return RedirectToAction("Index");
}
return View(job);
}
When using view model, you are mapping entity to model to display information and then mapping model back to the entity when updating(post action). I think i am missing something during the mapping process, to me it just seems like I am constantly mapping back and forth between view model and actual entity, am i missing something(is that why automapper is used?).
I searched online and using View Model seems like the better approach(strongly-typed view?), but there is just so much more code involved, it that suppose to happen or am i doing it wrong.