Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I inject a service into the controller, not a repository, and then use <a href="http://automapper.codeplex.com/" rel="noreferrer">AutoMapper</a> to convert it into a view model. The benefit of the service layer in this case is that it could aggregate multiple simple operations from one or more repositories into a single operation exposing a domain model. Example:</p> <pre><code>private readonly ICustomerService _service; public CustomerController(ICustomerService service) { _service = service; } [AutoMap(typeof(Customer), typeof(CustomerViewModel))] public ActionResult Create(int id) { Customer customer = _service.GetCustomer(id); return View(customer); } </code></pre> <p>in this example AutoMap is a custom action filter that I can write which executes after the controller action, inspects the returned object and uses defined AutoMapper mappings to map it to the specified destination type. So the view gets the corresponding CustomerViewModel as model type. Would have been equivalent to:</p> <pre><code>public ActionResult Create(int id) { Customer customer = _service.GetCustomer(id); CustomerViewModel vm = Mapper.Map&lt;Customer, CustomerViewModel&gt;(customer); return View(vm); } </code></pre> <p>it's just that it is too much plumbing and repetitive code that could be centralized.</p> <p>I would also recommend you watching the <a href="http://www.viddler.com/explore/mvcconf/videos/1/" rel="noreferrer"><em>putting your controllers on a diet video</em></a> from Jimmy Bogard.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload