0

Automated Business Logic Layer for ZK with Hibernate/JPA

asked 2011-08-29 15:48:59 +0800

ValHuber gravatar image ValHuber
24

Frameworks continue to evolve, simplifying application construction:
   *ZK does a great job of simplifying Rich Internet Applications
   *Hibernate/JPA provide valuable services to make data models available within the MVC paradigm.

Business Logic, however, remains a bit a mystery - problem, since this is often 50% of the development effort for update-oriented applications.

I have observed a number of fundamentally different approaches for implementing business logic. Some advocate a services layer, others advocate adding logic to domain objects. I have not yet read anything entirely convincing. Though the term is often used, there is considerable confusion, ranging from definition to architecture to proper tool utilization.

Having pondered this for a while now, I have collected some thoughts in this paper. It’s an approach that accepts a Service Layer as an anti-pattern, and proposes event injection of declarative logic into Domain Objects - a Rich Active Declarative Data Model.

While the concept can be applied to multiple architectures, our company has implemented what we believe is a preferred implementation, namely:

1. Specify business logic in "logic classes" that parallel your domain objects. Logic includes


   a. Derivation rules, in particular multi-table derivation rules such as
               Customer.balance = sum(order.amount where paid = false)
      Note that:
         * Derivations are multi-table, as in the example above
         * Derivations can chain, eg,
               Order.total = sum(Lineitem.amount)
               Lineitem.amount = quantity * partPrice
               Lineitem.partPrice = Copy(Part.price)
         * Chaining and dependencies are automatically handled

   b. Constraint Rules - in particular multi-attribute constraints, such as
               customer.balance < company.creditLimit

   c. Action Rules - address requirements such as auditing,
      or invoking a business rules engine such as Drools or BPM


2. A Logic Engine plugs into Hibernate events. At commit time (no code generation), it locates Logic Classes (if any) for each update, and executes the logic in a correct and efficient order...

    a. Correct means that the logic dependencies are determined (using byte code analysis), so changes to dependent data are propagated in a correct order including across tables

   b. Efficient means such propagation is pruned where dependent data is not altered.
      This is particularly important for multi-table logic such as sums/counts,
      to avoid unnecessary data access


This architecture has a number of advantages

   1- Excellent Architectural Separation

      a. Domain objects are unaffected

      b. Existing Hibernate APIs are unchanged.
         In particular, automation is retained for frameworks that automate Hibernate calls.
         This has been verified with ZK and Grails/Gorm (often used for zk persistence)

   2- Automated Active Enforcement,
      Logic is encapsulated with Domain objects, so re-use is automated over all related Use Cases.
      So, the rules below, while perhaps conceived for add order,
      are automatically applied to delete order, update order etc:
         Customer Constraint: balance < creditLimit
         Customer.balance = sum (order.total where paid = false)
         Order.total = sum (Lineitem.amount)
         Lineitem.amount = quantity * partPrice
         Lineitem.partPrice = copy(Part.price)

A Service Layer, often motivated by the complexities of multi-table logic, is not required for most cases. If desired (e.g., to publish APIs for SOA access), services can be thin since they invoke the logic encapsulated into the Rich Domain Objects.

It complements Rete-based Business Rule Engines (BREs)

   * Automates active enforcement - no explicit calls to invoke the logic engine are required

   * Transition Logic - integration with Hibernate means logic expressions can reference old values (eg, salary > old.salary)

   * Optimized Aggregates - Hibernate integration means that the system can prune updates requiring no changes (eg, changing order date does not affect customer balance), and when updates are required, the system can use adjustment logic to avoid SQL aggregate queries or reading all related objects into memory

   * BREs remains appropriate for Business User rule management, e.g., Decision Tables, and for cases not related to persisting transactions


Business advantages

   * Agility - the 5 rules noted above replace over 100 lines of traditional Java code since they automate dependency management and re-use over all related Use Cases (add order, delete order, update order, reassign order, etc) [deleted rules since repetitive]

   * Transparence - the rules can be read by Business Users, who can identify errors/omissions to reduce requirements risk

   * TCO - optimizations/ordering are repeated on each change, reducing maintenance costs. Compliance verification is dramatically simpler, since you need to verify just the logic, not all the calling code.

   * Architectural automation - architects can be assured the logic is re-used and optimized, with a significant simplification over manual approaches which involve many non-trivial design choices

I am interested in feedback and reactions.

delete flag offensive retag edit

6 Replies

Sort by » oldest newest

answered 2011-08-29 18:59:47 +0800

xcom gravatar image xcom
564 3

it posible change hibernate for ibatis?? only hibernate??

link publish delete flag offensive edit

answered 2011-08-29 20:04:07 +0800

ValHuber gravatar image ValHuber
24

For now, it is just Hibernate and JPA. The architecture is designed to minimize Hibernate dependence, so it's possible but it's bigger than a breadbox.

link publish delete flag offensive edit

answered 2011-08-30 08:02:27 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Hi Val,

how does the transaction scope is working or how it's set in code?
I.e. in 'common' Services they capsulate the call of several Dao methods in one Transaction.

thanks
Stephan

link publish delete flag offensive edit

answered 2011-08-30 14:51:06 +0800

ValHuber gravatar image ValHuber
24

Stephan...

You manage transactions just as you do now - no changes to that. Our logic fires on commit... if a constraint is violated, we simply throw an exception (so the commit does not occur) where the UI can provide for error correction, or issue a rollback. Constraint exceptions include the list of each each constraint violated, as well as the error attributes associated with the constraint (again, for the UI).

link publish delete flag offensive edit

answered 2011-08-31 03:10:25 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Val,

i have one question more about the transaction. In our case we work with the spring framework who handles the transaction self with an Aspect which lies on the dao and service packages. So we call only save(Object entity) and spring handles the commit/rollback stuff. Can your framework be integrated in this flow.

thanks
Stephan

link publish delete flag offensive edit

answered 2011-08-31 15:45:35 +0800

maxtardiveau gravatar image maxtardiveau
3

Hi Stephan,

I work with Val - he asked me to answer your question.

Absolutely -- our Spring demo is an example of that. It doesn't matter whether transactions are hand-controlled, or left to the framework or the container, the business logic is fired at commit time, and the transaction is committed if all went as expected, or rolled back if not. Because the logic engine works *underneath* Hibernate (using Hibernate's events), the application doesn't have to change.

-- Max

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-08-29 15:48:59 +0800

Seen: 527 times

Last updated: Aug 31 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More