|
From: Colin S. <col...@ex...> - 2003-10-27 16:05:38
|
Cameron Braid wrote: > I am just beginning to use the Spring framework, and so far I am very > impressed. > > One thing that I don't quite understand is the mandatory requirement > for each DAO and BO (Business Object) to support the > set/getSessionFactory method. > > The reason that I say it is mandatory is because the only way to > obtain the current session (as far as I know) is to use > SessionFactoryUtils.getSession(SessionFactory...). This means that > each DAO/BO requires a setSessionFactory property to be able to be > passed it from the spring container. This also requires that each > DAO/BO to be configured to bind the SessionFactory instance within the > applicationContext.xml > > What I would think would be a simple and quite common use case would > be for an app to require only one session factory. Therefore I think > that the SessionFactoryUtils, and related classes , could use a static > field to store the 'default' session factory. > > Has something like this been considered ? > Do you think that this is a good or a bad idea ? > Cameron, To clarify, your business objects would generally do not need to have a setSessionFactory method. They would need setters for one or more Mapper/DAO objects, and those Mapper/DAO objects would need the setSessionFactory method. As for the idea of having the Mapper/DAO objects not need the session factory parameter, and getting it from a singleton of some sort, that approach would work for some use cases, but using a singleton like this is just not very clean. It becomes harder to test code in isolation, and usually locks you into the singleton approach. You are at most going to declare each mapper/dao once in the application context. Giving it the session factory paramter is not a big deal. In terms of the actual code in the mapper/dao to support this, this can be provided by an abstract base class, and in fact Spring has such a class (for Hibernate) called HibernateDaoSupport. Regards, Colin |