MVC :: What is a model?

前端 未结 5 1853
情深已故
情深已故 2021-02-05 04:07

I am at a point where I must make a decision about models. I am aware that models are what you use to do all your database manipulation. But are models restricted to this?

<
5条回答
  •  执笔经年
    2021-02-05 04:30

    No they are not just restricted to database access.

    In an MVC application the M will usually be the model of your domain. This means it can encapsulate business logic and data. I would suggest that you avoid an anaemic domain model. You can even setup your model to be persistence ignorant. To get an idea of what I mean have a look at this talk on Crafting Wicked Domain Models.

    On the View and Controller side I'd recommend always using what's called a View Model, even when it feels like a 1 to 1 mapping. Sooner or later you'll find out the models are actually different and you don't want to give a View any more responsibility than translating a simple View Model directly into HTML or other rendering format.

    The Controllers job is then to just execute behaviours on your Model and create View Models for the Views.

提交回复
热议问题