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

Advertisements

5 Comments »

  1. I feel that is among the so much significant info for me.

    And i’m satisfied reading your article. However want to remark on few basic things, The web site style is wonderful, the articles is in point of fact excellent : D. Excellent process, cheers

    Comment by student relocation — April 16, 2013 @ 11:41 am |Reply

  2. At this moment I am going away to do my breakfast, later than
    having my breakfast coming again to read further
    news.

    Comment by http://www.youtube.com/watch?v=u_2OfUyBz5U — May 10, 2013 @ 9:23 pm |Reply

  3. Hi, i read your blog occasionally and i own a similar one
    and i was just curious if you get a lot of spam remarks?
    If so how do you prevent it, any plugin or anything you can
    recommend? I get so much lately it’s driving me mad so any help is very much appreciated.

    Comment by phen375">does wartrol work — May 23, 2013 @ 2:45 am |Reply

  4. For most up-to-date information you have to visit web and on the web I found this web site
    as a best website for most recent updates.

    Comment by michigan laser spine institute — June 3, 2013 @ 1:11 pm |Reply

  5. Hello There. I discovered your weblog the usage of msn. That is a very neatly written article.
    I will be sure to bookmark it and come back to read more of your helpful information. Thanks for the post.
    I will definitely return.

    Comment by Desert Plants — September 24, 2014 @ 6:21 am |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: