Storing Business Logic in Database

后端 未结 11 1547
感情败类
感情败类 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:16

    One easy way to do it is to use a OODBMS. There, methods are encapsulated with slots into objects, and they can even be executed in the database (like triggers).

    Now if you insist on a SQL database, what you can do is to use a dynamic programming language, and have a table to store code, perhaps associated to other tables or rows.

    A few years ago I saw a tender for the Tax system of the Algerian Government, in which they planed to store the business rules (tax rules), as Visual Basic code in the RDBMS.

    You could choose any language for which you can easily embed the interpreter in your application (Common Lisp http://ecls.sourceforge.net ; or http://common-lisp.net/project/armedbear/ if you write your application in Java), Lua, Javascript, Scheme, etc.

    It'd tend to favor Common Lisp or Scheme, since with those languages you can easily write a DSL for the business rules.

    The example given could be written as a symbolic expression such as:

    (rule :name "RuleName"
          :description "Some description"
          :body (if (and (< (change-in total-visitor)   (percent 10))
                         (> (change-in unique-visitors) (percent 2)))
                    (do-something)))
    

    in lisp such a symbolic expression can be printed readably with the PRINT or PRINT-TO-STRING operators, so that you can insert this expression into a SQL database:

    insert into rules (id,expression) values (42,"(rule :name \"RuleName\"
          :description \"Some description\"
          :body (if (and (< (change-in total-visitor)   (percent 10))
                         (> (change-in unique-visitors) (percent 2)))
                    (do-something)))");
    

    And you can get it back from SQL, read it back as a symbolic expression with the lisp READ or READ-FROM-STRING operators, and then, with the right DSL, you can evaluate it with the lisp EVAL operator:

    ;; with the right DSL written:
    (eval (read-from-string (sql-select (expression) :where (= id 42))))
    

提交回复
热议问题