Home  /  Questions  /  Question

108   96.7
Aug 09, 2011

Update Method in the Business Layer

It seems in the project that the BusinessLogic is only relevant in the web application. The MVC application does the dataValidation on the Entity object in the presentation and the Web application does the validation in the BL. Also, the Web application bascially has to to 2 calls to the database for an update for the BL to work? For an update it seems to do another call for a select even though you are doing an update, updates the object with the values and then does a call to the update which then the BL layer does it's validation.


830   99.9
Aug 10, 2011

I think you bring up an interesting question. I myself came from the database world and initially asked the same question.
However, to keep thing uniform and simple, there are only a hand-full of statements that usually are executed against the database.
Usually these are your typical CRUD (CREATE, READ, UPDATE, DELETE) operations: i.e. INSERT record, UPDATE record, DELETE record, and SELECT record.

As you said, with the UPDATE you could only update the columns that were changed. But to keep things relatively simple, usually the entire record, that is, all columns are updated, whether they were changed or not.

For example you may have a page where a USER name is changed, a second page where USER address is changed, and a third page where USER membership options are changed.  If all fields live in the same table, ie. USER, you will see that each of these pages uses the exact same UPDATE statement.   To make this possible you need to retrieve the entire record, before updating.

Indeed, this is redundant but databases are so fast nowadays that it does not really matter.  Also, updates are usually less frequent than SELECTS (or INSERTS most of the time). Now, if performance is extremely critical you could of course write a single, optimized, and lightweight UPDATE statement with only the relevant columns. This requires fine-grained SQL handcoding which is why it is typically not done that way.

Hope this helps.

"To make this possible you need to retrieve the entire record, before updating. " I don't know why this is. I write CRUD operations all the time where you just update by key and all the changes are applied. This is fairly standard as far as I know. Mind you I typically would be relying on an ORM to do my CRUD operations anyway, but if not, I still would have a single, simple update operation without requiring a database read. --- Trevor de Koekkoek  Aug 10, 2011
Yeah, I still don't get it. when you are updating a record you in theory already have the record. When you click save/update it should in theory just be one database call to update the record which in the process your BL should get fired and do its stuff. --- Dennis Tucker  Aug 10, 2011