开发者

Database Querying through a Model

When working with an MVC framework and querying the database from the controller using the model, what is the best pr开发者_运维知识库actice?

Should the model provide a very flexible function to allow the controller to query the database? Like a call from the controller as so:

User->find ([
    { 
        or => [ 
            {field => 'name', value => 'john', op => '~' },
            { 
                and => [
                    { field => 'organization', value => 'acme', op => '~' },
                    { field => 'city', value => 'tokyo', op => '=' }
                ]
             }
        ],
     });

    } 

Or should the model have a strict API which results in calls like:

User->find_john_or_people_from_acme_in_tokyo();

What is the best way of going about it? Should the SQL be all over the model? Or contained in one queryFactory function? Can you please point me in the right direction? Some OS code would be awesome.

Thanks!


It is always a good practice to separate out Model and DataAccess as two different layers. This describes best practices for logical separation at Model layer. This helps when there is a need to change underlying data access entities without modifying ViewModel layer.

Best practices to partition Model code to logical parts in MVC? Which is the best?


A good practice is to separate all your database-related code into a separate DAO layer. That way your business logic code does not depend on the particular database technology you are using (JDBC vs. Hibernate vs. JPA, etc.) or even on whether the data is stored in the database or in some other store.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜