12 Replies Latest reply on Sep 28, 2007 1:18 PM by Miguel Angel Goicochea

    Layering the code in Seam

    Miguel Angel Goicochea Newbie


      First of all after reading some documentation about seam I have to say that I am impressed by the framework, it seems to solve a lot of problems that I had when trying to develop web applications regardless whether it was .NET or J2EE.

      However, after seeing the examples I didn't like that the presentation, business and data access code is all mixed up on the same action bean. On the hotel booking example it mentions that for big applications it may be a good idea to layer the code, however, I have not seen this on any example.

      I am used to do this when working with Spring/Hibernate using Services and DAOs, so at the end I have one layer managing the presentation layer, another for business logic and finally one for data access logic. How would this pattern be implemented on a Seam Application?

        • 1. Re: Layering the code in Seam
          Monkey Den Master

          Seam does not prevent you in any way from taking that same approach. In fact, it makes some of that much simpler. For instance, using Seam Events or annotated methods you can invoke a business processes from your action without having to explicitly implement the Observer pattern and your action doesn't need to know anything about the process itself. It also integrates nicely with jBPM and jPDL, to simplify the overall implementation of business process. In my estimation, Seam was developed not only to simplify the interaction between the front end and the web tier, but the interaction of all tiers of your application.

          • 2. Re: Layering the code in Seam
            Fernando Montaño Expert

            Yes, magoicochea is absolutely right.

            By instance, my application is using ACTION + DAO layers. Actions are Seam POJOs and DAOs are Session Beans (EJB).

            BTW, you can take a look to wiki example in the Seam distribution, it uses action + DAO on POJO components (maybe you want something like that).


            • 3. Re: Layering the code in Seam
              Miguel Angel Goicochea Newbie

              First of all sorry to BUMP this topic again, is just that I had been busy the last month and I forgot to check it.

              I tried to look on the wiki, but I did not find the example fernando_jmt mentioned. I don't think that I need a DAO (after all I have read that they don't seem to be useful on Seam although you can use them)

              For example for a person maintenance page I would like to have this classes (plus the interfaces that may be needed)

              Presentation Layer: Manages all the presentation logic for this page.

              Business Layer: Manages all the business logic for the class

              Data Layer: Manages all the logic related to storing the object on the database.

              I think that maybe I could use the manager bean as the presentation layer object and create there the entity manager and send it as a parameter to the other layers when invoking the methods along with the entity bean, so I would end up having something like this:


              public void savePerson(EntityManager em,Person person)
              //apply presentation logic


              public void savePerson(EntityManager em,Person person)
              //apply business logic


              public void savePerson(EntityManager em,Person person)
              //apply data access logic

              I like how the code ends up separated because I think it is easier to maintain, however, my intuition tells me that this is not the right way to do this on Seam and that this may end up being inefficient. For example, when developing with hibernate I create the session on the business layer, not on presentation and I think that there may also be some features from Seam that makes this easier.

              At the end of the day what I don't like about the examples I saw was that they tend to mix this three layers in just one class, which seems a little messy to me on large projects. If someone could provide me with a simple CRUD example for this I would be very thankful.

              • 4. Re: Layering the code in Seam
                Daniel Kimmig Newbie

                If you create the EntityManager in the presentation layer and pass it down to the DAO layer, then I dont see no difference to invoking the method of EntityManager right inside you presentation bean.

                Truely layered code would mean that you business code would not even know about an Entity Manager let alone your presentation code. Truely layered means, that the presentation bean calls a business method and gets a result and does not care about how this result is produced.

                In my view, only the DAO class should have a dependency to the EntityManager IF you want to truely structure your code into independent layers. Thats great for maintenance and testing since each layer can swapped out or replaced by a different implementation without breaking e.g. the layers above.

                But this flexibility in the long term is bought by a higher complexity and more code in the short term. Maybe you will even have to deal with "LazyInitializationException" again depending on how you work with the Entitymanager/Seam components.

                • 5. Re: Layering the code in Seam
                  Miguel Angel Goicochea Newbie

                  I know what you mean Grandfatha, usually when working with hibernate the session was opened on the business facade layer, but I don't know how to replicate this on seam.

                  • 6. Re: Layering the code in Seam
                    Monkey Den Master

                    I just inject stateless business object into my Actions. Those business objects just use SLSBs, who have the persistence knowledge. How is that fundamentally different than a typical Struts CRUD application?

                    • 9. Re: Layering the code in Seam
                      Daniel Kimmig Newbie

                      I think the question is: "Do we get the stateful/contextual behaviour aka 'no LazyInitializationException' when separating the persistence logic from view actions." I think it is possible if the EntityManager is managed by Seam and therfor injected by "@In".

                      if that assumption is true, then you would probably be able to introduce as much layers as you want. But it is true that most if the Seam-examples are written using the "one-big-Stateful-Session-Bean"-Pattern. I have heard that the Wiki example has some more layering, but I dont have the time in this moment to look after it.

                      • 10. Re: Layering the code in Seam
                        Miguel Angel Goicochea Newbie

                        I someone would tell me which of the examples is the one that is layered I would appreciate it a lot, I downloaded a couple of them, but they were made like the ones on the tutorial.

                        • 11. Re: Layering the code in Seam
                          Daniel Kimmig Newbie

                          There is a not so trivial example delivered in Seam 2 CR1 called "wiki" under the "examples" folder in your seam distribution.

                          I think you can learn a lot from that beast.

                          • 12. Re: Layering the code in Seam
                            Miguel Angel Goicochea Newbie

                            That's great I'll check it later at home.