3

I have created my own customized lots of architecture including n-tier layers for different different technology.

Currently working on n-tier architecture with asp.net mvc framework. The problem is I have entity framework at data access layer. As the entities will have all it's relational metadata and navigation properties, it becomes heavier one. I am feeling like it is not wise to expose this entities directly over mvc view.

I am more favor in exposing own customized model of entities over mvc view which one be lighter one.

But this also leads me overhead of converting data from my original entities to customized model.

For example I have Employee entity which is as generated from edmx file of entity framework. It contains total 20 fields with all navigation properties.

Now over view in mvc I need to show only 2 fields for edit.

So do we need to expose original entity to view or need to create DTO/customized model of that two field and than expose that view?

2 Answers2

0

You can expose original entity, that is nothing bad. But as soon as you need some other information on the view e.g. instead of Name and Lastname you need FullName, then you should create an EmployeeViewModel with only needed properties. You initialize it with desired values in an action and then pass it to the view.

Michal B.
  • 5,676
  • 6
  • 42
  • 70
  • what will be the behaviour when i expose original entity over view and then post my form? will the asp.net mvc framework try to search all the fields present in object of original entity from Request.Form[""] to get value? –  Mar 22 '12 at 09:48
  • In your post action you will have Employee object as a parameter. The default ModelBinder will try to bind all values. If you have e.g. EmployeeId (Int32) and you do not have this value on the form, then the ModelBinder will give it its default value = 0. If you prefer null then you need to make EmployeeId **Int32?** (nullable). If you do not want this behavior, then you should create EmployeeViewModel with only desired fields pass it to the view and also put it as a parameter of a post action. Then only desired fields will be bound. – Michal B. Mar 22 '12 at 10:20
0

I would use a view model. I have learnt not to expose my domain objects to the view, I rather map my domain object to the view model and return this view model to the view.

Here is a partial view model, you might have more properties if you need more employee data to create/edit or display:

public class EmployeeViewModel
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
}

In my action method of my controller it would look something like this:

public ActionResult Edit(int id)
{
     Employee employee = employeeRepository.GetById(id);

     // Mapping can be done here by using something like Auto Mapper, but I am
     // manually mapping it here for display purposes

     EmployeeViewModel viewModel = new EmployeeViewModel();
     viewModel.FirstName = employee.FirstName;
     viewModel.LastName = employee.LastName;

     return View(viewModel);
}

And then your view might look something like this:

<td>First Name:</td>
<td>@Html.TextBoxFor(x => x.FirstName, new { maxlength = "15" })
    @Html.ValidationMessageFor(x => x.FirstName)
</td>

I prefer to have a view model that has only the values of employee that is needed on the view. Let say your employee has 20 properties, and you only need to update 2 fields, why then pass all 20 to the view? Use only what you need.

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
  • Yes this is what i believe. Also it's good practice that never expose full entity to view instead of it just pass what you need. Thanks for reply. –  Mar 22 '12 at 16:06