MVC Mayhem; Sorting out Model responsibility and structure

后端 未结 1 1903
天涯浪人
天涯浪人 2021-02-14 18:58

My understanding of MVC-type architectures over the last few weeks/months has advanced (I\'d say) considerably, and I owe most of my thanks to fellow SO enthusiasts; so, thank y

相关标签:
1条回答
  • 2021-02-14 19:40

    Perhaps it's just semantics but I would say that The Model is the representation of the the data, the classes (and by extension the objects) that encapsulate the entities in your application. There's another, missing piece, which I would call the persistence or data access layer (DAL). MVC as an abstraction doesn't really concern itself with persistence because you don't actually have to have persistence to develop using the MVC pattern. In (almost?) all web applications using MVC, you do have a database and thus do need a persistence layer. The persistence layer understands The Model and makes it available to the controller, but it's not really part of the model.

    If you separate out the concepts of inserting/retrieving/updating the data into the persistence layer, what you're left with are the containers and associated business/validation logic that encapsulate the representation of your applications entities. These should be relatively small, well-focused, and only interdependent on the actual data dependencies between your entities. This small models, one per entity, in total comprise The Model for your application. Your persistence layer (DAL/ORM), too, is not particularly large, but rather is focuses solely on interacting with the database to obtain/store the models.

    0 讨论(0)
提交回复
热议问题