architecture - What is the usage of Model in MVC? Is it actually useful? -


i'm new in this, bear me. i've been using 1 mvc framework in couple of projects lately, , after while, i'm disillusioned in perceived usefulness of 'model' in mvc.

i usefulness of controllers , views, know separation between presentation , logic important make code more maintainable in future, although not faster or more robust.

if logic should placed inside controller in first place, don't see use model, active-record. have language robust , easy use communicate database, right? it's called sql. me when models implemented active-record, it's usefulness depends on whether or not want app fit in multiple databases.

so i'm asking is, if you're using 1 database, why bother models , active-records? why don't use sql? why layer of complexity? guys have case studies/real-life stories models can things better using database class , sql-away?

again, i'm sorry if seem ignorant, don't know why models important. answering.

first, assuming model layer uses kind of orm, in order abstract sql away. not true: may create model layer loosely-coupled controller layer tightly-coupled particular dbms, , avoid using full-featured orm.

there orm libraries, hibernate (java), nhibernate (.net), doctrine (php) or activerecord-rails (ruby) can generate actual sql statements you; if think orm unnecessary, , want define sql statements hand, don't use them.

still, imho not mean should place db related logic inside controller layer. called "fat controller" approach, , road leads, many times, bloated, unmaintainable code. use simple crud projects, beyond demand existence of real "model".

you seem care mvc. please, read tdd. wise man once said "legacy code code without tests". when learn automated unit tests as important as "real" code, understand why there many layers in enterprise application, , why model layer should separate controller. block of code tries (presentation, business logic, data persistence) cannot tested (nor debugged way).

edit

"model" little bit fuzzy term. depending at, can mean different. instance, php e ruby programmers use synonym active record, not accurate. other developers seem believe "model" kind of dto, not right.

i rather use definition of model seen in wikipedia:

the central component of mvc, model, captures application's behavior in terms of problem domain, independent of user interface. model directly manages application's data, logic , rules.

so model biggest, important layer in mvc applications. that's why divided in sub-layers: domain, service, data access , on. model exposed through domain, because it's there you'll find methods controller call. data access layer belongs "model" too. related data persistence , business logic belongs it.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -