Five ‘s Weblog

October 25, 2007

Exposed Domain Model Pattern

Filed under: Five's thought — by powerdream5 @ 11:10 pm
Tags: , , , ,

       In my former articles, I have mentioned “open session in view” many times. What is open session in view? When we use Hibernate framework, the Hibernate session will be closed after a transaction being submitted. Then, when the presenter layer of a web application accesses the detached objects, it may access an unloaded object because of the mechanism of lazy loading. Therefore, an exception would thrown.

10_25_1_2007.jpg

       One of the good way to handle this problem is using Exposed Domain Model Pattern instead of POJO Facade Pattern. We also call Exposed Domain Model pattern as Open Session in View Pattern. It means the presenter layer can access the business logic layer directly instead of through a facade class.  It is obvious that the advantages of POJO Facade pattern are the disadvantages of Exposed Domain Model. However, If you want to avert the complexity of writting the detached objects, in fact, which are also weak,  Exposed Domain Model pattern is still a good choice.

       In POJO Facade, the facade class need to take care of managing transaction and database connection. In Exposed Domain Model, we have to distribute the two functions to the other parts of the system. Because we have to keep the Hibernate session availabe until the presenter layer finishes rendering the page, the servlet filter is a good place to open and close a session. Spring framework provides such a filter named “OpenSessionInViewFilter” to help us realize this.  Furthermore, we can also start and submit a transaction in the servlet filter. However, it is too complicate to write code to restart a transaction in the servlet filter. Usually, we let the classes in the business layer (for example, the service classes in the domain model) to deal with the transaction.

The following is an example of web.xml file to show how to config spring framework in web.xml:

<web-app>
         /*the listener watches over the creation of ServletContext which store the spring applicationContext*/
        <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
 
        <context-param>/*list the spring configuration files*/
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>

        <filter>
               <filter-name>OpenSessionInViewFilter</filter-name>
               <filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>
               <init-param>
                        <param-name>sessionFactoryBeanName</param-name>
                        <param-value>sessionFactory</param-value>
              <init-param>
       </filter>

       <filter-mapping>
               <filter-name>OpenSessionInViewFilter</filter-name>
               <url-pattern>/*</url-pattern>
       </filter-mapping>

       ……..
</web-app>

10_25_2_2007.jpg

Blog at WordPress.com.