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.
On top of that I have a business layer...the guts of the application. It's completely unaware of any specific DAO implementation.
Everything up to this point seems fine, until I start thinking about transactions. If transactions are left to the client to implement, then how in the world do I maintain the nice separation I've got going on between my layers? That is, I'm using Hibernate at the moment, and I don't really feel much like adding hibernate-specific transactions to my business layer code.
I could create a simple transaction interface with begin, commit, and rollback methods and pass an implementation to my business layer...but...I'm not sure...
So here is the challenge: can you recommend a way for me to do this without using the word Spring (or EJB, or any other additional framework)?
DAO stands for Data Access Object. DAO Design Pattern is used to separate the data persistence logic in a separate layer. This way, the service remains completely in dark about how the low-level operations to access the database is done. This is known as the principle of Separation of Logic.
The Data Access Object ( DAO ) pattern abstract away the details of persistence in an application. Instead of having the domain logic communicate directly with the database, file system, web service, or any other persistence mechanism : The domain logic speaks to a DAO layer instead.
Access is transparent because the implementation details are hidden inside the DAO. Enables Easier Migration A layer of DAOs makes it easier for an application to migrate to a different database implementation. The business objects have no knowledge of the underlying data implementation.
ORM and DAO are orthogonal concepts. One has to do with how objects are mapped to database tables, the other is a design pattern for writing objects that access data. You don't choose 'between' them. You can have ORM and DAO is the same application, just as you don't need ORM to use the DAO pattern.
I remember that Martin Fowler advices to keep the control of the transaction in the business layer because transaction is a business problem. (If you design a BankAccount class, a transaction is part of the domain language).
You can try to implement a TransactionScope as in .NET it works something like that
using (TransactionScope ts = new TransactionScope()) { ... }
It's the same thing as (not exactly but if you are a Java guy, it's more explicit to you)
TransactionScope scope = new TransactionScope(); try { ... scope.Commit(); } catch(Exception ex) { scope.Rollback(); throw; }
To decouple your business layer from any DAO technologies you can add a TransactionFactory in your domain language, which return a ITransactionScope (an interface) that you have defined with a Commit and Rollback methods. This way your domain layer is not bound to your DAO layer, only a concrete implementation of TransactionFactory is.
ITransactionScope scope = transactionFactory.CreateTransaction(); try { ... scope.Commit(); } catch(Exception ex) { scope.Rollback(); throw; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With