开发者

Which overload is the most efficient for HttpPost Edit action method?

Which overload is the most efficient for HttpPost Edit action method?

  1. [HttpPost] public ActionResult Edit(int id, FormCollection collection)
  2. [HttpPost] public ActionResult Edit(OurDomainDataModel obj)

EDIT

Using OurDomainDataModel

    [HttpPost]
    public ActionResult Create(Movie m)
    {
        if (ModelState.IsValid)
        {
  开发者_开发百科          db.Movies.Add(m);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
            return View(m);
    }

Using FormCollection

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        Movie movie = new Movie();
        if (TryUpdateModel(movie))
        {
            db.Movies.Add(movie);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
            return View(movie);
    }

Which one is recommended as the best practice?


The first one doesn't have a overhead of creating and populating OurDomainDataModel instance, but I wouldn't think this has a big impact on performance.

Remember, that premature optimization is the root of all the evil.


If by efficient you mean faster, then I would guess (without testing) that using FormCollection will be faster as it doesn't have the overhead of using model binding.

However the results of this will be negligible and using FormCollection for this reason would be premature optimisation. MVC was designed with integrated model binding and if you have a performance issue, I would bet it is somewhere else.

Now, if you mean efficient in regards to developer time, then that depends entirely on the project and the developer.

If the form is only posting a few simple values then using FromCollection may well be quicker to code than having to create a model. However if that's the case then using parameters would be quicker and you would have typed values. ActionResult Edit(int id, string name, bool someOtherValue). Although, if you're using either of these methods in anything but simplest of projects you will soon find you need to create a model. So what was initially fast to code will take more development time to refactor and maintain.

If you already have a OurDomainDataModel model defined then passing that will be far quicker to code.

If what you really wanted to ask was

"What would be the more elegant, maintainable, testable code that would gain the respect of anyone else that had to use it?"

Then the answer is pass a Model or a ViewModel


Writing all the code yourself using a FormCollection can be faster than using the built-in features that ASP.NET MVC offers, depending on how good of a programmer you are, and how efficient your mapping layer is.

However, when performance isn't critical; you will save yourself alot of valuable time when just using the

ActionResult Edit(Model model);

syntax.

Our 250 mio pageviews / month MVC site just does fine with using the MVC binder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜