what is Model in MVC pattern

2019-06-09 18:31发布

I am creating an application were I am using MVC pattern.For this I am considering my view as jsps,controller as servlets and model as DAO objects.I have a doubt that me considering DAO objects as Model is right or wrong?

2条回答
家丑人穷心不美
2楼-- · 2019-06-09 18:46

Model is not a DAO. It is a layer, which contains all the domain logic, and is composed mostly from two types of elements, with following responsibilities:

  • business logic
  • data access (usually implemented as DataMapper)

The idea is that business logic should never be tied to the storage mechanism. When you are creating an invoice, the domain object should not care, if data comes from SQL database, MSWord document, remote REST API or just a mocked up data.

You might find this article interesting and relevant: GUI Architectures.

查看更多
Melony?
3楼-- · 2019-06-09 18:51

A model in MVC is where the business logic lives.

Looking at the sun Java EE pattern definitions we see that DAOs encapsulate persistence mechanisms and are used by a Business Object. I don't therefore see DAOs as natuarally having any business logic.

In simple systems, a few database tables, or those where the business logic is implemented in the database (stored procedures, referential integrity checks, triggers) then the DAO is effectively a facade in front of Business Logic, so they pretty much look like the Model. So in some introductory material you may see the DAO as pretty much the only Java expression of the Model.

When we choose to implement our business logic in Java, it would lie in a layer above the DAO, in for example Session Beans, which use the DAOs, and in my mind it's the Session Bean or equivalent which is the Model.

So ask yourself: where is the business logic? That's where the Model really is.

查看更多
登录 后发表回答