Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy controllers run first in the ASP.NET MVC?
    text
    copied!<p>I want to improve current implementation of the ASP.NET MVC Framework. Current code:</p> <pre><code>routes.MapRoute(null, "I-want-to-fly", new { controller = "Airport", action = "Fly" }); public class AirportModel { public List&lt;Plane&gt; Planes { get; private set; } public List&lt;Pilot&gt; Pilots { get; private set; } public void AddFly(Plane plane, Pilot pilot, Passenger passenger) { // . . . } } public class AirportController { private AirportModel model; [HttpGet] public ViewResult Fly(string from, string to) { var planes = return (from p in model.Planes where p.CityFrom == from &amp;&amp; p.CityTo == to select p).ToList(); return View(planes); } [HttpPost] public ActionResult Fly(Plane plane, Passenger passenger, DateTime time) { if (!(ModelState.IsValid &amp;&amp; plane.TimeOfDeparture == time)) return View(); var pilot = (from p in model.Pilots where p.Free &amp;&amp; p.CanAviate(plane.Id) select p).First(); model.AddFly(plane, pilot, passenger); return RedirectToAction("Succeed"); } } </code></pre> <p>My proposal:</p> <pre><code>routes.MapRoute(null, "I-want-to-fly", new { model = "Airport", action = "Fly" }); public class AirportModel { private List&lt;Plane&gt; planes; private List&lt;Pilot&gt; pilots; private void AddFly(Plane plane, Pilot pilot, Passenger passenger) { // . . . } [HttpGet] public ViewResult Fly(string from, string to) { var planes = return (from p in model.Planes where p.CityFrom == from &amp;&amp; p.CityTo == to select p).ToList(); return View(suitablePlanes); } [HttpPost] public ActionResult Fly(Plane plane, Passenger passenger, DateTime time) { if (!(ModelState.IsValid &amp;&amp; new PlaneController().CanFly(plane, time))) return View(); var pilot = (from p in pilots where p.Free &amp;&amp; p.CanAviate(plane.Id) select p).First(); AddFly(plane, pilot, passenger); return RedirectToAction("Succeed"); } } public static class PlaneController { public static bool CanFly(Plane plane, DateTime time) { return plane.TimeOfDeparture == time; // it will be more complex } } </code></pre> <p>You see, in such way we don't need excessive count of controllers and their methods. Model would create controller only by perforce: mostly to verify user input <b>(not input validation, business validation)</b>.</p> <p>What do you think, can this idea have a continuation? Or, what is wrong with it?</p> <p>Thanks for your replies!</p> <p><strong>UPDATE:</strong> I noticed, that we need to replace implementations of controller and view as a result of changing the model's state (mostly). So, if model causes to change the implementation, why model cannot <strong>do</strong> it?</p> <p><strong>UPDATE 2:</strong> It seems to me I explained incorrectly. I don't want model to do all work, of course no! I try to say, that <strong>not controller</strong> should decide what to do with model and what view is the most suitable for this user request. </p> <p>Doesn't it strange, that model doesn't know how to visualize itself, but some controller knows?</p> <p>Doesn't it strange, than we need controller for the GET request, where there is nothing to control?</p> <p>I try to remove those strangenesses.</p> <p><strong>UPDATE 3:</strong> I understand that it cannot be applied anywhere. The main question is: can it improve some part of current implementations of MVC ? Mostly I'm interested in ASP.NET MVC -- can we </p> <ol> <li>remove redundant controllers or some its methods </li> <li>work directly with models </li> </ol> <p>using this idea? Is it possible and what are the problems of this idea?</p> <p>Found problems: </p> <ol> <li><em>More strong connection between model and view/controller</em> -- but currently I don't think it's a problem. Actually it shows that views and controllers were created in help for the major element -- model.</li> </ol> <p><strong>UPDATE 4:</strong> I changed the code, showing "before/after". Maybe this example will be better.</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