DAO pattern - where do transactions fit in?

前端 未结 5 1494
独厮守ぢ
独厮守ぢ 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:40

    Your right that the application is a good place to cordinate transactions as this allows composition of more complex actions implemented by various services / managers / or whatever you want to call them.

    An easy solution is to define an ITransaction interface, and use some type of factory or DI to hide the actual ITransaction implementor from your application. I rolled my own like this in .net using nHibernate and essentially I have a base class that all my managers (A manager in this case contains business logic for a logical set of entities like Membership, Order which might use one or more repositories). My base class has a ITransaction BeginTransaction(), which dynamically creates a type based on a configuration file.

    This class then works with nHibernate's Session to begin and commit transactions.

提交回复
热议问题