c# - 为什么 Controller 首先在 ASP.NET MVC 中运行?

标签 c# asp.net-mvc model-view-controller asp.net-mvc-3

我想改进 ASP.NET MVC 框架的当前实现。 当前代码:

routes.MapRoute(null, "I-want-to-fly", new { controller = "Airport", action = "Fly" });

public class AirportModel 
{
   public List<Plane> Planes { get; private set; }
   public List<Pilot> 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 && p.CityTo == to
                            select p).ToList();
       return View(planes);
   }

   [HttpPost]
   public ActionResult Fly(Plane plane, Passenger passenger, DateTime time)
   {
       if (!(ModelState.IsValid && plane.TimeOfDeparture == time))
            return View();

       var pilot = (from p in model.Pilots 
                    where p.Free && p.CanAviate(plane.Id) 
                    select p).First();
       model.AddFly(plane, pilot, passenger);

       return RedirectToAction("Succeed");
   }
}

我的建议:

routes.MapRoute(null, "I-want-to-fly", new { model = "Airport", action = "Fly" });

public class AirportModel 
{
    private List<Plane> planes;
    private List<Pilot> 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 && p.CityTo == to
                             select p).ToList();
        return View(suitablePlanes);
    }

    [HttpPost]
    public ActionResult Fly(Plane plane, Passenger passenger, DateTime time)
    {
        if (!(ModelState.IsValid && new PlaneController().CanFly(plane, time)))
                return View();

        var pilot = (from p in pilots 
                     where p.Free && 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
    }
}

你看,这样我们就不需要过多的 Controller 和它们的方法。模型只能通过强制创建 Controller :主要是为了验证用户输入(不是输入验证,业务验证)

你怎么看,这个想法能有延续性吗?或者,它有什么问题?

感谢您的回复!

更新:我注意到,由于更改了模型的状态(主要是),我们需要替换 Controller 和 View 的实现。那么,如果模型导致更改实现,为什么模型不能呢?

更新 2: 在我看来,我的解释不正确。我不想让模型做所有的工作,当然不!我想说的是,不是 Controller 应该决定如何处理模型以及哪种 View 最​​适合该用户请求。

这不奇怪吗,那个模型不知道如何可视化自己,但是一些 Controller 知道?

这不奇怪吗,我们需要 Controller 来处理 GET 请求,而那里没有什么可控制的?

我试图消除那些奇怪的东西。

更新 3: 我知道它不能应用于任何地方。主要问题是:它可以改进 MVC 当前实现的某些部分吗?我主要对 ASP.NET MVC 感兴趣——我们可以

  1. 删除冗余 Controller 或它的一些方法
  2. 直接与模型合作

使用这个想法?有没有可能,这个想法有什么问题?

发现问题:

  1. 模型和 View / Controller 之间的连接更牢固 -- 但目前我认为这不是问题。实际上,它表明 View 和 Controller 的创建是为了帮助主要元素——模型。

更新 4: 我更改了代码,显示“之前/之后”。也许这个例子会更好。

最佳答案

这不是违反了MVC的整个思路吗? ?您的模型与 Controller 和 View 是分开的。通过这种方式(您建议的方式),您将无法用另一个实现或您的 Controller 替换您的模型。

更新我:

当然,您也可以让您的模型充当 Controller 的一部分,但从那一刻起,您就不再谈论 MVC 设计模式了。对于 MVC,模型现在不应该也不应该关于 View 。那是 Controller 的工作。

The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

在 MVC 模式中,模型不仅仅固定在您的数据库模型中,它还可以是您的数据库模型和存储库模式的组合,您可以在其中实现业务逻辑。

我看到您的提案的最大问题是它使代码不可重用。我得到一个与其 View 紧密耦合的模型,如果我想以我可能想要的任何方式重用该模型,我真的不想要它。


更新二

  • 我认为您被“ Controller ”这个词误导了,我有过一段时间的想法,您最近的评论在某种程度上证实了这一点

    Controllers are some objects, that check correspondence of user input to business-logic.

    Controller 根据用户输入采取行动,他们可能会检查用户输入,但他们检查有效性的责任就此止步。业务逻辑进入模型(同样,模型由 MVC 模式定义,而不是数据模型中的模型)。他们的主要目的是决定显示什么 View 。

  • 同样来自您的最新评论之一:

    How do you think, if [asp.net mvc] would be developed in my way, would it solve problem of redundant controllers?

    Asp.Net MVC 遵循 MVC 设计模式。你的提议没有。它看起来更像是一个 ModelControlled View 模式,只是为了创造一个名称。此外,没有冗余 Controller , Controller 没有问题,它们是解决方案不可或缺的一部分。

并努力通过代码示例简单地阐明我的意思:

namespace DataProject.Model
{
    public class AirportModel 
    {
        public List<Plane> Planes { get; set; }
        public List<Pilot> Pilots { get; set; }
        public List<Passenger> Passengers { get; set; }
        public List<Flight> Flights { get; set; }
    }

}

namespace SomeProject.Repository
{
    public class AirportRepository
    {
        private DataProject.Model.AirportModel model;

        //constructor sets the model somehow

        public bool AddFlight(Plane plane, List<Passenger> passengers, DateTime time)
        {
            //Business logic
            if (plane.TimeOfDeparture != time) return false;

            var pilot = (from p in model.Pilots 
                         where p.Free && 
                               p.CanAviate(plane.Id) 
                         select p).FirstOrDefault();
            //More Business logic
            if (pilot == null) return false;

            //Add plane, pilot and passenger to database
            model.Flights.add(new Flight{Pilot = pilot, Plane = plane, Passengers = passengers});
            //Even here you could decide to do some error handling, since you could get errors from database restrictions
            model.Save(); 

            return true;    
        }

        public List<Planes> GetPlanes(string from, string to)
        {
            return (from p in model.Planes 
                        where p.CityFrom == from && p.CityTo == to
                        select p).ToList();
        }
    }
}

namespace MVCApp.Controllers
{
    public class AirportController
    {
        private SomeProject.Repository.AirportRepository repository;

        [HttpGet]
        public ViewResult Fly(string from, string to)
        {
            var viewModel = repository.GetPlanes(from, to);
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Fly(Plane plane, List<Passenger> passengers, DateTime time)
        {
            if (!ModelState.IsValid) return View(); 

            if (!repository.AddFlight(plane, pilot, passenger)) return View();

           return RedirectToAction("Succeed");
        }
    }
}

关于c# - 为什么 Controller 首先在 ASP.NET MVC 中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7216936/

相关文章:

javascript - MVC 从下拉列表中过滤 actionResult

asp.net - 我无法从添加引用对话框中找到 System.Web.MVC [我位于 Webform 应用程序中]

asp.net-mvc - 可以从 Controller 传递大量数据以在 MVC3 中使用 viewmodel 进行查看吗?这里'

.net - 当 View 不与模型交互时,它是 MVC 吗?

c++ - 在不使用就地编辑的情况下更改 Qt AbstractListModel

c# - 当影响经度、纬度到位置时,输入字符串的格式不正确

c# - 如何在 ASP.net 自定义控件中处理 PostBack

c# - XML 到 Excel (2007) 使用 Windows XP 和 C#.Net 的想法

c# - 在 asp.net 中将数据表从一个页面发送到另一个页面

asp.net-mvc - 如何从 asp.net mvc unittest 中的 ViewResult 获取模型?