Storing Business Logic in Database

后端 未结 11 1433
感情败类
感情败类 2021-01-30 00:04

We want to write some business logic rules that work on top of certain data to build reports. Not sure which is the best to store them in the database MySQL.

11条回答
  •  不知归路
    2021-01-30 00:17

    I am guessing that the purpose of the rules is to name calculated fields from an existing database table (or tables). Otherwise, for mere reporting purposes, you could dump the data into Excel and let users use Excel functions and pivot tables for their purposes.

    The key question is how are you going to transform the rules into action. If the purpose is only to store business rules, so you can create a report of business rules, then a simple data structure in SQL is sufficient.

    However, if you want to turn the rules into code, you need to consider where the code will run. When the data is stored in SQL, you have several options:

    • Create SQL code that will extract the results of the "business rule".
    • Create a user defined function that will parse and execute the business rule.
    • Extract the data to another environment, such as C++ or C#, and run the code there.

    I have a bias toward the first of these. The primary reason is that it limits the tools to one: SQL.

    I am not sure what your rules are doing; the key is what the "statement" component does. Let me assume that this is a constant or an expression on data that can be calculated. In that case, your rules start to look a lot like a case statement. One caveat is that the statement may require looking at more than one row in your data table (to handle changes over time).

    My recommendation is to store these rules in the database. This storage would allow you to construct a query from a sequence of business rules using SQL coding. Mysql allows dynamic SQL (nowadays). Without knowing a little more about the underlying table and the rules, it is hard to give more information.

    I can say that I designed a much more complicated system used for scenario analysis. The scenarios themselves were stored in spreadsheets, in a series of tables, constants, and so on -- much like your business rules. The system worked by using SQL (and some Excel) to transform the spreadsheet representation of a scenario into a (giant) query. It could then run the query to generate the associated reports. This system has proven flexible, performant, and powerful.

提交回复
热议问题