Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wouldn't be fetching data from the database in your ViewModel. The ViewModel exists to promote separation of concerns (between your View and your Model). Tangling up persistance logic in there kind of defeats the purpose.</p> <p>Luckily, the ASP.NET MVC framework gives us more integration points, specifically the ModelBinder.</p> <p>I've got an implementation of a generic ModelBinder pulling information from the service layer at:-</p> <p><a href="http://www.iaingalloway.com/going-further-a-generic-servicelayer-modelbinder" rel="nofollow noreferrer">http://www.iaingalloway.com/going-further-a-generic-servicelayer-modelbinder</a></p> <p>It doesn't use a ViewModel, but that's easily fixed. It's by no means the only implementation. For a real-world project, you're probably better off with a less generic, more customised solution.</p> <p>If you're diligent, your GET methods don't even need to know that the service layer exists.</p> <p>The solution probably looks something like:-</p> <p>Controller action method:-</p> <pre><code>public ActionResult Details(MyTypeIndexViewModel model) { if( ModelState.IsValid ) { return View(model); } else { // Handle the case where the ModelState is invalid // usually because they've requested MyType/Details/x // and there's no matching MyType in the repository // e.g. return RedirectToAction("Index") } } </code></pre> <p>ModelBinder:-</p> <pre><code>public object BindModel ( ControllerContext controllerContext, BindingContext bindingContext ) { // Get the Primary Key from the requestValueProvider. // e.g. bindingContext.ValueProvider["id"] int id = ...; // Get an instance of your service layer via your // favourite dependancy injection framework. // Or grab the controller's copy e.g. // (controllerContext.Controller as MyController).Service IMyTypeService service = ...; MyType myType = service.GetMyTypeById(id) if (myType == null) { // handle the case where the PK has no matching MyType in the repository // e.g. bindingContext.ModelState.AddModelError(...) } MyTypeIndexViewModel model = new MyTypeIndexViewModel(myType); // If you've got more repository calls to make // (e.g. populating extra fields on the model) // you can do that here. return model; } </code></pre> <p>ViewModel:-</p> <pre><code>public class MyTypeIndexViewModel { public MyTypeIndexViewModel(MyType source) { // Bind all the properties of the ViewModel in here, or better // inherit from e.g. MyTypeViewModel, bind all the properties // shared between views in there and chain up base(source) } } </code></pre> <p>Build your service layer, and register your ModelBinder as normal.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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