Here's my situation. I've noticed that code gets harder to maintain when you keep embedding queries in every function that will use them. Some queries tend to grow very fast and tend to lose readability after concatenating every line. Another issue that comes with all of the concatenation is when you must test a specific query and paste it. You are forced to remove all of the " that held your query string together.
这是我的情况。我注意到,当你在每个使用它们的函数中嵌入查询时,代码变得越来越难以维护。有些查询往往增长得非常快,并且在连接每一行后往往会失去可读性。所有连接附带的另一个问题是,您必须测试特定查询并粘贴它。您*删除所有“将查询字符串保存在一起的所有内容”。
So my question is, what methods are being used to separate queries from the code? I have tried searching but it doesn't look like it's the right thing because i'm not finding anything relevant.
所以我的问题是,使用什么方法将查询与代码分开?我试过搜索,但它看起来不是正确的,因为我找不到任何相关的东西。
I'd like to note that views and stored procedure are not possible since my queries fetch data from a production database.
我想请注意,由于我的查询从生产数据库中获取数据,因此无法查看视图和存储过程。
Thank you.
6 个解决方案
#1
I avoid this problem by wrapping queries in classes that represent the entities stored in the table. So the accounts table has an Account object. It'll have an insert/update/delete query.
我通过在表示存储在表中的实体的类中包装查询来避免此问题。所以accounts表有一个Account对象。它将有一个插入/更新/删除查询。
I've seen places where the query is stored in a file and templates are used to replace parts of the query.
我已经看到查询存储在文件中的位置,模板用于替换部分查询。
Java had something called SQLJ - don't know if it ever took off.
Java有一种叫做SQLJ的东西 - 不知道它是否曾经起飞过。
LINQ might provide some way around this as an issue too.
LINQ也可能提供一些解决方案。
#2
If you follow an MVC pattern, then your queries should be all in the model - i.e. the objects representing actual data.
如果您遵循MVC模式,那么您的查询应该都在模型中 - 即表示实际数据的对象。
If not, then you could just put all your repetitive queries in script files, including only those needed in each request.
如果没有,那么您可以将所有重复查询放在脚本文件中,包括每个请求中只需要的那些。
However, concatenating and that kind of stuff is hard to get rid of; that's why programmers exist :)
然而,连接和那种东西很难摆脱;这就是程序员存在的原因:)
#3
These two words will be your best friend: Stored Procedure
这两个词将是你最好的朋友:存储过程
#4
Risking being accused of not answering the question, my suggestion to you would be simply don't. Use an O/RM of your choice and you'll see this problem disappear in no time.
冒着被指责没有回答这个问题的风险,我给你的建议就是不这样做。使用您选择的O / RM,您会立即看到此问题消失。
#5
I usually create a data class that represents the data requirements for objects that are represented in the database. So if I have a customer class, then I create a customerData class as well that houses all the data access logic in them. This keeps data logic out of your entity classes. You would add all you CRUD methods here as well as custom data methods.
我通常创建一个数据类,表示数据库中表示的对象的数据要求。因此,如果我有一个客户类,那么我创建一个customerData类,其中包含所有数据访问逻辑。这可以使数据逻辑远离实体类。您可以在此处添加所有CRUD方法以及自定义数据方法。
Youc an also use Stored Proceedures or an ORM tool depending on your language.
您也可以根据您的语言使用存储过程或ORM工具。
The main key is to get your data logic away from your business and entity logic.
主要关键是使您的数据逻辑远离业务和实体逻辑。
#6
I use stored procedures in my production environment, and we process the rows too...
我在生产环境中使用存储过程,我们也处理行...
#1
I avoid this problem by wrapping queries in classes that represent the entities stored in the table. So the accounts table has an Account object. It'll have an insert/update/delete query.
我通过在表示存储在表中的实体的类中包装查询来避免此问题。所以accounts表有一个Account对象。它将有一个插入/更新/删除查询。
I've seen places where the query is stored in a file and templates are used to replace parts of the query.
我已经看到查询存储在文件中的位置,模板用于替换部分查询。
Java had something called SQLJ - don't know if it ever took off.
Java有一种叫做SQLJ的东西 - 不知道它是否曾经起飞过。
LINQ might provide some way around this as an issue too.
LINQ也可能提供一些解决方案。
#2
If you follow an MVC pattern, then your queries should be all in the model - i.e. the objects representing actual data.
如果您遵循MVC模式,那么您的查询应该都在模型中 - 即表示实际数据的对象。
If not, then you could just put all your repetitive queries in script files, including only those needed in each request.
如果没有,那么您可以将所有重复查询放在脚本文件中,包括每个请求中只需要的那些。
However, concatenating and that kind of stuff is hard to get rid of; that's why programmers exist :)
然而,连接和那种东西很难摆脱;这就是程序员存在的原因:)
#3
These two words will be your best friend: Stored Procedure
这两个词将是你最好的朋友:存储过程
#4
Risking being accused of not answering the question, my suggestion to you would be simply don't. Use an O/RM of your choice and you'll see this problem disappear in no time.
冒着被指责没有回答这个问题的风险,我给你的建议就是不这样做。使用您选择的O / RM,您会立即看到此问题消失。
#5
I usually create a data class that represents the data requirements for objects that are represented in the database. So if I have a customer class, then I create a customerData class as well that houses all the data access logic in them. This keeps data logic out of your entity classes. You would add all you CRUD methods here as well as custom data methods.
我通常创建一个数据类,表示数据库中表示的对象的数据要求。因此,如果我有一个客户类,那么我创建一个customerData类,其中包含所有数据访问逻辑。这可以使数据逻辑远离实体类。您可以在此处添加所有CRUD方法以及自定义数据方法。
Youc an also use Stored Proceedures or an ORM tool depending on your language.
您也可以根据您的语言使用存储过程或ORM工具。
The main key is to get your data logic away from your business and entity logic.
主要关键是使您的数据逻辑远离业务和实体逻辑。
#6
I use stored procedures in my production environment, and we process the rows too...
我在生产环境中使用存储过程,我们也处理行...