DAO pattern - where do transactions fit in?

前端 未结 5 1507
独厮守ぢ
独厮守ぢ 2021-02-05 04:54

So I\'ve got this generic DAO thing going on and at face value it appears to be ok. It\'s basically modeled after the CaveatEmptor sample application from the Hibernate guys.

5条回答
  •  佛祖请我去吃肉
    2021-02-05 05:43

    Maybe this is a bit too late for an answer, but how about creating another class for specific transactions, which sits between the business layer and the dao layer? E.g. if methods a() and b() from a DAO are to be run in a transaction for some specific foo() business method, then create something like fooInTransaction() that starts a transaction and calls a() and b() in it. The business method foo() delegates to it.

    This will keep the business code clean and duplication can be avoided by re-factoring.

提交回复
热议问题