通过模型进行数据库查询
当使用 MVC 框架并使用模型从控制器查询数据库时,最佳实践是什么?
模型是否应该提供非常灵活的功能来允许控制器查询数据库?就像来自控制器的调用一样:
User->find ([
{
or => [
{field => 'name', value => 'john', op => '~' },
{
and => [
{ field => 'organization', value => 'acme', op => '~' },
{ field => 'city', value => 'tokyo', op => '=' }
]
}
],
});
}
或者模型应该有一个严格的 API,它会导致如下调用:
User->find_john_or_people_from_acme_in_tokyo();
最好的方法是什么? SQL 应该遍布整个模型吗?或者包含在一个 queryFactory 函数中?你能指出我正确的方向吗?一些操作系统代码会很棒。
谢谢!
When working with an MVC framework and querying the database from the controller using the model, what is the best practice?
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将模型和数据访问分离为两个不同的层始终是一个好习惯。
这描述了模型层逻辑分离的最佳实践。当需要更改底层数据访问实体而不修改 ViewModel 层时,这会有所帮助。
最佳实践将模型代码划分为 MVC 中的逻辑部分?哪个最好?
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?
一个好的做法是将所有与数据库相关的代码分离到单独的 DAO 层中。这样,您的业务逻辑代码就不依赖于您正在使用的特定数据库技术(JDBC、Hibernate、JPA 等),甚至不依赖于数据是否存储在数据库中或其他存储中。
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.