ASP.NET MVC: Updates the Model of an “expensive” operation (due to Reflection)?

I wondered if UpdateModel is considered an “expensive” operation (due to the search in Reflection of the model’s properties), especially when viewed in the context of a larger web application (I think, StackOverflow)?

I do not want to engage in premature optimization, but I think that this is the choice of design to use UpdateModel, so I would like to know how appropriate or not to go with it. Another (tedious) choice is written by my own UpdateModel method for various domain objects with fixed properties.

Thanks!

+6
c # asp.net-mvc
source share
3 answers

You are smart not to engage in premature optimization. Moreover, this "optimization" would favor CPU time compared to yours, which is much more expensive.

The basic optimization rule is to optimize slow material first. Therefore, consider how often you actually update the model or select from your database. I guess this is 1/10, both often and less. Now consider the cost of choosing from a database compared to the cost of reflection. Reflection cost is measured in milliseconds. The cost of selecting from a database database can be measured in seconds in the worst case scenario. My experience is that POSTs are rarely very slow, and when they are usually a database, not a reflection. I think you are likely to spend most of your optimization time on GET.

+5
source share

Compared to network latency, database calls, and general IO, the call to UpdateModel () is trivial, and I would not bother with it.

+2
source share

I think UpdateModel is a bit of a shortcut that brings about a huge connection between the view and the model.

I prefer not to use “built-in” models (for example, the ability to transfer objects created by LINQ to a view directly from the database) because I want the option to replace my model with something more complex - or even just another database provider. It is very tempting to use LINQtoSQL (or ADO.NET Entities) for rapid prototyping.

What I usually do is create my MVC application and then set the “services” layer, which is then connected to the “model” (which is the OO representation for my domain). This way I can easily create a web service layer, swap databases, write new workflows, etc. No problem.

(and make sure you write your tests and use DI - it saves a lot of trouble!)

Rob

0
source share

All Articles